Search code examples
javaandroidzxing

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:

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).

I use these dependencies in my build.gradle file:

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

Solution

  • you can simply generate the scanned code using result.getContents() and result.getFormatName(), your final code should look like this:

    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);
        }
    
    }