skip to Main Content

I’m using ZXing API.

I have the following code:

button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v){
                IntentIntegrator integrator = new IntentIntegrator(activity);
                integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
                integrator.setCameraId(0);
                integrator.setOrientationLocked(true);
                integrator.setPrompt("Para usar o flash utilizar os botões do volume");
                integrator.initiateScan();

However, I don’t know how to publish the initiateScan using the ActivityResultLauncher mGetContent = registerForActivityResult(new ActivityResultContracts.GetContent().

Could you please help me?
Kind regards,

2

Answers


  1. Chosen as BEST ANSWER

    I add:

    ActivityResultLauncher<Intent> activityResultLaucher;
    
    activityResultLaucher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
                @Override
                public void onActivityResult(ActivityResult result) {
                    if(result.getResultCode() == RESULT_OK && result.getData() != null){
                        Intent data = result.getData();
                    }
                }
            });
    

    and:

    button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v){
                    IntentIntegrator integrator = new IntentIntegrator(activity);
                    integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
                    integrator.setCameraId(0);
                    integrator.setOrientationLocked(true);
                    integrator.setPrompt("Para usar o flash utilizar os botões do volume");
                    integrator.initiateScan();
                    ActivityResultLauncher.launch(integrator);
                    }
    
                 });
    

    But the ActivityResultLauncher.launch(integrator) doesn't work.


  2. You can launch scanner with ‘integrator.createScanIntent()’.

    activityResultLaucher.launch(integrator.createScanIntent());

    button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v){
                    IntentIntegrator integrator = new IntentIntegrator(activity);
                    integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
                    integrator.setCameraId(0);
                    integrator.setOrientationLocked(true);
                    integrator.setPrompt("Para usar o flash utilizar os botões do volume");
                    // integrator.initiateScan(); <- remove this line
    
                    // launch scanner with 'integrator.createScanIntent()'
                    activityResultLaucher.launch(integrator.createScanIntent());
                  }
    
                 });
    
    
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search