Skip to content

Instantly share code, notes, and snippets.

@jesselima
Created September 19, 2019 03:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesselima/e3334703df6782da1024b2fb056fdbf4 to your computer and use it in GitHub Desktop.
Save jesselima/e3334703df6782da1024b2fb056fdbf4 to your computer and use it in GitHub Desktop.
@Override
protected Result doInBackground(Void... params)
{
try {
InputStream inputStream = activity.getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
if (bitmap == null) {
Log.e(TAG, "uri is not a bitmap," + uri.toString());
return null;
}
int width = bitmap.getWidth(), height = bitmap.getHeight();
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
bitmap.recycle();
bitmap = null;
RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
BinaryBitmap bBitmap = new BinaryBitmap(new HybridBinarizer(source));
MultiFormatReader reader = new MultiFormatReader();
try {
Result result = reader.decode(bBitmap);
return result;
} catch (NotFoundException e) {
Log.e(TAG, "decode exception", e);
return null;
}
} catch (FileNotFoundException e) {
Log.e(TAG, "can not open file" + uri.toString(), e);
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment