Open Jaysinh001 opened 1 year ago
This discussion can help you.
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)
}
}
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) {