journeyapps / zxing-android-embedded

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

OnActivityResult is deprecate ... how to launch scan from registerForActivityResult #653

Closed needhelp17 closed 2 years ago

needhelp17 commented 2 years ago

since onActivityResult is deprecate we should use registerForActivityResult but I can find a way to start you scan and handle the result in the ActivityResultCallback

have you an idea to how I can implement this ?

monteirohat commented 2 years ago

I think this can help you: https://stackoverflow.com/questions/68520524/how-to-return-a-result-in-activityresultlauncher

bayazidsustami commented 2 years ago

after i saw the hint by @monteirohat , i create custom ActivityResultContract for handle intent result.

class QrCodeScannerContract: ActivityResultContract<IntentIntegrator, IntentResult>() {

    override fun createIntent(context: Context, input: IntentIntegrator): Intent {
        return input.apply {
            setDesiredBarcodeFormats(IntentIntegrator.QR_CODE)
            setPrompt("Point the Scanner at the QR Code")
            setBeepEnabled(false)
            setBarcodeImageEnabled(true)
            setOrientationLocked(true)
        }.createScanIntent()
    }

    override fun parseResult(resultCode: Int, intent: Intent?): IntentResult {
        return IntentIntegrator.parseActivityResult(resultCode, intent)
    }
}

and the launcher :

private val qrCodeScannerLauncher = registerForActivityResult(QrCodeScannerContract()){
        if (!it.contents.isNullOrEmpty()){
            Log.d("RESULT_INTENT", it.contents)
        }else{
            Log.d("RESULT_INTENT", "CANCELED")
        }
    }

and then you can call launcher like this: qrCodeScannerLauncher.launch(IntentIntegrator.forSupportFragment(this))

I hope this can help you

rkistner commented 2 years ago

Merging into #628.