java - 如何在 Java 中使用 ZXING 获取扫描的 QR 码的图像?#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);
}
}
所以我看到我通过扫描 QR 码收到的结果是正确的,但我无法使用 getBitmap() 保存图片,如 Zxing 的已关闭 Github-Issue 143 所示(链接到 GitHub 问题 143)。
我在 build.gradle 文件中使用这些依赖项:
implementation 'com.journeyapps:zxing-android-embedded:3.0.3'
implementation 'com.google.zxing:core:3.4.1'
解决方案
result.getContents()
您可以简单地使用and生成扫描的代码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);
}
}
推荐阅读
- php - 从后端安装或更新插件时出现 10 月 cms 错误
- javascript - 编辑列表中的项目
- c# - 对 UseHttps 扩展方法的模糊调用
- sql - 在sql中的情况下如何执行多个操作?
- sql - 如何合并和显示 2 个表格的列?
- powerbi - 计算表累计
- php - 为什么我的 HTML 代码在用户名输入字段中打印根?
- git - 将 Spring Cloud Config Server 与我的 git 本地存储库连接
- java - 用 JLabel 的 setIcon 方法替换后 Gif 不再动画
- python - 为什么 Multiprocessing 会产生多个 PyGame 实例?