dm77 / barcodescanner

Barcode Scanner Libraries for Android
Other
5.46k stars 1.42k forks source link

Scan image from disk #17

Closed xjrcode closed 10 years ago

xjrcode commented 10 years ago

There is any way to read an image from disk?

I have this code working on other project with zbar, but I don't find the way to do this with your project.

Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), selectedImage);
Image barcode = new Image(bitmap.getWidth(), bitmap.getHeight(), "RGB4");
// get bytes array from image
int bytes = bitmap.getWidth()*bitmap.getHeight()*4;
ByteBuffer buffer = ByteBuffer.allocate(bytes);
bitmap.copyPixelsToBuffer(buffer);
// pass image as bytes to zbar
barcode.setData(buffer.array());
// convert to Y800
barcode = barcode.convert("Y800");
// prepare scanner
createScanner();
// scan image to find a qr in it
mScanner.scanImage(barcode)
xjrcode commented 10 years ago

Done!

I wish this helps to someone.

Bitmap bMap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), selectedImage);
int[] intArray = new int[bMap.getWidth()*bMap.getHeight()];
//copy pixel data from the Bitmap into the 'intArray' array
bMap.getPixels(intArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight());
LuminanceSource source = new RGBLuminanceSource(bMap.getWidth(), bMap.getHeight(), intArray);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new MultiFormatReader();
Result result = reader.decode(bitmap);