简体   繁体   English

如何在 Java 中使用 ZXING 获取扫描的 QR 码的图像? 第667章

[英]How to get an image of a scanned QR Code with ZXING in Java? #667

After scanning a QR Code I want to save the exact same QR-Code as scanned before.扫描二维码后,我想保存与之前扫描的完全相同的二维码。 Please see an example of my code from my MainActivity below:请在下面的 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);
        }

    }

So I'm seeing that the results I receive from scanning QR Codes are correct but I am not able to save the picture with getBitmap() as shown in the closed Github-Issue 143 from Zxing ( Link to GitHub Issue 143 ).所以我看到我从扫描 QR 码中收到的结果是正确的,但我无法使用 getBitmap() 保存图片,如 Zxing 关闭的 Github-Issue 143( 链接到 GitHub 问题 143 )所示。

I use these dependencies in my build.gradle file:我在 build.gradle 文件中使用这些依赖项:

implementation 'com.journeyapps:zxing-android-embedded:3.0.3' 
implementation 'com.google.zxing:core:3.4.1'

you can simply generate the scanned code using result.getContents() and result.getFormatName() , your final code should look like this:您可以简单地使用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);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM