Feature suggestion: Import from Gallery #685
Replies: 2 comments
-
I found a way to implement it: I open the gallery this way: (this function is called when a button is pressed) private fun openGallery() {
val intent = Intent()
intent.type = "image/*"
intent.action = Intent.ACTION_GET_CONTENT
openGalleryRequest.launch(Intent.createChooser(intent, getString(R.string.scanner_gallery)))
} Using this request: private val openGalleryRequest =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode == RESULT_OK) {
it.data?.data?.let { uri -> handleImage(uri) }
}
} And then I handle the result with this function: private fun handleImage(uri: Uri) {
try {
val image = if (Build.VERSION.SDK_INT < 28) {
MediaStore.Images.Media.getBitmap(this.contentResolver, uri)
} else {
val source = ImageDecoder.createSource(this.contentResolver, uri)
ImageDecoder.decodeBitmap(source).copy(Bitmap.Config.RGBA_F16, true)
}
val intArray = IntArray(image.width * image.height)
image.getPixels(intArray, 0, image.width, 0, 0, image.width, image.height)
val source = RGBLuminanceSource(image.width, image.height, intArray)
val reader = MixedDecoder(MultiFormatReader())
var result = reader.decode(source)
if (result == null) {
result = reader.decode(source)
}
val intent = CaptureManager.resultIntent(BarcodeResult(result, null), null)
setResult(RESULT_OK, intent)
finish()
} catch (e: Exception) {
e.printStackTrace()
}
} Note that I decode twice the result to allow to get inverted codes as well (white codes on a colored background) with the What do you think of this implementation? Do you have any suggestion or feedback? And also, now we found how to do it, any path to implement it in the library? (in which class, how it would be called, ...) Does adding a function to |
Beta Was this translation helpful? Give feedback.
-
Here is the solution
|
Beta Was this translation helpful? Give feedback.
-
Description of the problem:
I would like to add a method to trigger the recognition from a file imported from Gallery/Camera Roll.
That way, when embedding the scanner in a custom Activity, we can place a button to allow the user to import a QR code from Gallery instead of scanning one.
Beta Was this translation helpful? Give feedback.
All reactions