如何在 Java 中使用 ZXING 获取扫描的二维码图像? #667
How to get an image of a scanned QR Code with ZXING in Java? #667
扫描二维码后,我想保存与之前扫描的完全相同的二维码。
请在下面的 MainActivity 中查看我的代码示例:
public void onClick(View v) {
if (v.getId() == R.id.btn_1) {
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setBarcodeImageEnabled(true);
integrator.setPrompt("Scan a barcode or QRCode");
integrator.setOrientationLocked(false);
integrator.initiateScan();
}
if(v.getId()==R.id.btn_2) {
// do something else
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result!=null){
if(result.getContents()==null) {
Toast.makeText(getBaseContext(), "Cancelled", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
tvScanFormat.setText((result.getFormatName())); //TextView is set and shows which format the QR code has
tvScanContent.setText(result.getContents()); //TextView with the actual Content is set
result.getBitmap(); //Cannot resolve method 'getBitmap' in 'IntentResult'
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
所以我看到我从扫描二维码收到的结果是正确的,但我无法使用 getBitmap() 保存图片,如关闭的 Github-Issue 143 来自 Zxing ( Link to GitHub Issue 143).
我在 build.gradle 文件中使用了这些依赖项:
implementation 'com.journeyapps:zxing-android-embedded:3.0.3'
implementation 'com.google.zxing:core:3.4.1'
您可以简单地使用 result.getContents()
和 result.getFormatName()
生成扫描代码,您的最终代码应如下所示:
public void onClick(View v) {
if (v.getId() == R.id.btn_1) {
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setBarcodeImageEnabled(true);
integrator.setPrompt("Scan a barcode or QRCode");
integrator.setOrientationLocked(false);
integrator.initiateScan();
}
if(v.getId()==R.id.btn_2) {
// do something else
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result!=null){
if(result.getContents()==null) {
Toast.makeText(getBaseContext(), "Cancelled", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
tvScanFormat.setText((result.getFormatName())); //TextView is set and shows which format the QR code has
tvScanContent.setText(result.getContents()); //TextView with the actual Content is set
com.google.zxing.MultiFormatWriter multiFormatWriter = new com.google.zxing.MultiFormatWriter();
switch(result.getFormatName()) {
case "EAN_13": {
com.google.zxing.common.BitMatrix bitMatrix = multiFormatWriter.encode(result.getContents(), com.google.zxing.BarcodeFormat.EAN_13,500,250);
break;
}
case "EAN_8": {
com.google.zxing.common.BitMatrix bitMatrix = multiFormatWriter.encode(result.getContents(), com.google.zxing.BarcodeFormat.EAN_8,500,250);
break;
}
case "UPC_A": {
com.google.zxing.common.BitMatrix bitMatrix = multiFormatWriter.encode(result.getContents(), com.google.zxing.BarcodeFormat.UPC_A,500,250);
break;
}
case "UPC_E": {
com.google.zxing.common.BitMatrix bitMatrix = multiFormatWriter.encode(result.getContents(), com.google.zxing.BarcodeFormat.UPC_E,500,250);
break;
}
case "QR_CODE": {
com.google.zxing.common.BitMatrix bitMatrix = multiFormatWriter.encode(result.getContents(), com.google.zxing.BarcodeFormat.QR_CODE,500,500);
break;
}
case "DATA_MATRIX": {
com.google.zxing.common.BitMatrix bitMatrix = multiFormatWriter.encode(result.getContents(), com.google.zxing.BarcodeFormat.DATA_MATRIX,500,500);
break;
}
case "AZTEC": {
com.google.zxing.common.BitMatrix bitMatrix = multiFormatWriter.encode(result.getContents(), com.google.zxing.BarcodeFormat.AZTEC,500,500);
break;
}
}
com.journeyapps.barcodescanner.BarcodeEncoder barcodeEncoder = new com.journeyapps.barcodescanner.BarcodeEncoder();
//the code below is the bitmap that you want
Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
//use the code below if you want to display the bitmap into an ImageView
myimageview.setImageBitmap(bitmap);
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
扫描二维码后,我想保存与之前扫描的完全相同的二维码。 请在下面的 MainActivity 中查看我的代码示例:
public void onClick(View v) {
if (v.getId() == R.id.btn_1) {
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setBarcodeImageEnabled(true);
integrator.setPrompt("Scan a barcode or QRCode");
integrator.setOrientationLocked(false);
integrator.initiateScan();
}
if(v.getId()==R.id.btn_2) {
// do something else
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result!=null){
if(result.getContents()==null) {
Toast.makeText(getBaseContext(), "Cancelled", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
tvScanFormat.setText((result.getFormatName())); //TextView is set and shows which format the QR code has
tvScanContent.setText(result.getContents()); //TextView with the actual Content is set
result.getBitmap(); //Cannot resolve method 'getBitmap' in 'IntentResult'
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
所以我看到我从扫描二维码收到的结果是正确的,但我无法使用 getBitmap() 保存图片,如关闭的 Github-Issue 143 来自 Zxing ( Link to GitHub Issue 143).
我在 build.gradle 文件中使用了这些依赖项:
implementation 'com.journeyapps:zxing-android-embedded:3.0.3'
implementation 'com.google.zxing:core:3.4.1'
您可以简单地使用 result.getContents()
和 result.getFormatName()
生成扫描代码,您的最终代码应如下所示:
public void onClick(View v) {
if (v.getId() == R.id.btn_1) {
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setBarcodeImageEnabled(true);
integrator.setPrompt("Scan a barcode or QRCode");
integrator.setOrientationLocked(false);
integrator.initiateScan();
}
if(v.getId()==R.id.btn_2) {
// do something else
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result!=null){
if(result.getContents()==null) {
Toast.makeText(getBaseContext(), "Cancelled", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
tvScanFormat.setText((result.getFormatName())); //TextView is set and shows which format the QR code has
tvScanContent.setText(result.getContents()); //TextView with the actual Content is set
com.google.zxing.MultiFormatWriter multiFormatWriter = new com.google.zxing.MultiFormatWriter();
switch(result.getFormatName()) {
case "EAN_13": {
com.google.zxing.common.BitMatrix bitMatrix = multiFormatWriter.encode(result.getContents(), com.google.zxing.BarcodeFormat.EAN_13,500,250);
break;
}
case "EAN_8": {
com.google.zxing.common.BitMatrix bitMatrix = multiFormatWriter.encode(result.getContents(), com.google.zxing.BarcodeFormat.EAN_8,500,250);
break;
}
case "UPC_A": {
com.google.zxing.common.BitMatrix bitMatrix = multiFormatWriter.encode(result.getContents(), com.google.zxing.BarcodeFormat.UPC_A,500,250);
break;
}
case "UPC_E": {
com.google.zxing.common.BitMatrix bitMatrix = multiFormatWriter.encode(result.getContents(), com.google.zxing.BarcodeFormat.UPC_E,500,250);
break;
}
case "QR_CODE": {
com.google.zxing.common.BitMatrix bitMatrix = multiFormatWriter.encode(result.getContents(), com.google.zxing.BarcodeFormat.QR_CODE,500,500);
break;
}
case "DATA_MATRIX": {
com.google.zxing.common.BitMatrix bitMatrix = multiFormatWriter.encode(result.getContents(), com.google.zxing.BarcodeFormat.DATA_MATRIX,500,500);
break;
}
case "AZTEC": {
com.google.zxing.common.BitMatrix bitMatrix = multiFormatWriter.encode(result.getContents(), com.google.zxing.BarcodeFormat.AZTEC,500,500);
break;
}
}
com.journeyapps.barcodescanner.BarcodeEncoder barcodeEncoder = new com.journeyapps.barcodescanner.BarcodeEncoder();
//the code below is the bitmap that you want
Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
//use the code below if you want to display the bitmap into an ImageView
myimageview.setImageBitmap(bitmap);
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}