[英]android scan barcode from camera with google barcode api (what is missing?)
[英]How to make an application, that is able to scan barcode from the image of the Barcode which is taken from the sdcard of the android mobile itself?
我的要求是,打開應用程序后,將有一個按鈕將打開相機應用程序並拍攝兩個條形碼的圖片,然后將位圖提供給我的應用程序,條形碼解碼器將解碼這兩個條形碼並顯示結果! 可能嗎? 如果是,那怎么辦?
當然可以。
有很多條形碼解碼庫,例如https://github.com/zxing/zxing/集成該庫,遵循其教程並將位圖傳遞給它。 然后在解碼后顯示結果。
您可以嘗試ZXing Librery的MultiFormatReader
樣例代碼
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.