Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to scan QR from gallery Image #734

Open
Jaysinh001 opened this issue Mar 7, 2023 · 2 comments
Open

how to scan QR from gallery Image #734

Jaysinh001 opened this issue Mar 7, 2023 · 2 comments

Comments

@Jaysinh001
Copy link

I want to Scan QR code from Gallery but I don't know how to do it.... can any one help me

Below is my code to fetch Image from gallery .....

btnUploadFromGallery.setOnClickListener(new View.OnClickListener() {
@OverRide
public void onClick(View v) {

            Intent iGallery = new Intent(Intent.ACTION_PICK);
            iGallery.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(iGallery,200);
        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK){
        if (requestCode == 200){
            Uri ImageUri = data.getData();
            scannerView.setResultHandler((ZXingScannerView.ResultHandler) ImageUri);
        }
    }
}
@angelix
Copy link

angelix commented Jul 26, 2023

This discussion can help you.

@sachin-varshney
Copy link

sachin-varshney commented Sep 14, 2023

private val openGalleryRequest =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            if (it.resultCode == AppCompatActivity.RESULT_OK) {
                it.data?.data?.let { uri -> decodeQRCode(uri) }
            }
        }

    private fun openGallery() {
        val intent = Intent()
        intent.type = "image/*"
        intent.action = Intent.ACTION_GET_CONTENT
        openGalleryRequest.launch(Intent.createChooser(intent, "Scan Gallery"))
    }

    private fun decodeQRCode(imageUri: Uri) {
        try {
            val inputStream = context?.contentResolver?.openInputStream(imageUri)
            val bitmap = BitmapFactory.decodeStream(inputStream)

            val intArray = IntArray(bitmap.width * bitmap.height)
            bitmap.getPixels(intArray, 0, bitmap.width, 0, 0, bitmap.width, bitmap.height)

            val source: LuminanceSource = RGBLuminanceSource(bitmap.width, bitmap.height, intArray)
            val binaryBitmap = BinaryBitmap(HybridBinarizer(source))

            val reader = QRCodeReader()
            val result = reader.decode(binaryBitmap)

            // The QR code content is in result.text
            val qrCodeContent = result.text
            proceedQrData(qrCodeContent)

        } catch (e: Exception) {
            // Handle exceptions (e.g., QR code not found)
           
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants