journeyapps / zxing-android-embedded

Barcode scanner library for Android, based on the ZXing decoder
https://journeyapps.com/
Apache License 2.0
5.77k stars 1.27k forks source link

How to get an image of a scanned QR Code? #667

Open Lukas-Werner opened 3 years ago

Lukas-Werner commented 3 years ago

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:

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'

Thanks for the great library guys!

rkistner commented 3 years ago

Older versions of the library are not supported - please use the latest version (currently 4.3.0).

Lukas-Werner commented 3 years ago

Hi @rkistner , thanks really much! Where can I see what the latest version for embedded and core is right now? Thanks in advance. Best regards,

rkistner commented 3 years ago

Latest versions are in the Readme and the Releases page.

For zxing:core, the releases are listed on https://github.com/zxing/zxing. But you also don't need to specifically include this unless you set { transitive = false }.

Lukas-Werner commented 3 years ago

Alright thanks very much for your support! Now it's recommended to use

// Register the launcher and result handler
private final ActivityResultLauncher<ScanOptions> barcodeLauncher = registerForActivityResult(new ScanContract(),
        result -> {
            if(result.getContents() == null) {
                Toast.makeText(MyActivity.this, "Cancelled", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(MyActivity.this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
            }
        });

// Launch
public void onButtonClick(View view) {
    barcodeLauncher.launch(new ScanOptions());
}

this method instead of Intents right?

rkistner commented 3 years ago

Yes, that's recommended now, in line with onActivityResult being deprecated. But the previous IntentIntegrator / onActivityResult workflow should still continue working.