【问题标题】:Barcode scan on picture from gallery对画廊中的图片进行条码扫描
【发布时间】:2014-04-24 22:13:17
【问题描述】:
关于我想分步实现的一点点想法:
- 从相机拍摄图像或从图库中选择图片
- 将结果图像加载到
ImageView(可能)中
现在在第 1 步和第 2 步之间,我想从第 1 步开始扫描图像上的条形码。
根据扫描结果:
- 如果成功,请显示
Toast,其中包含格式和内容等条形码信息。
- 如果没有,请显示
Toast,说明该图像没有任何条形码。
我(在某种程度上)探索了 ZXing 以实现我的目标。
但我无法得到的是:
- 如何从图库中扫描图片上的条形码?
- 条码扫描成功后如何返回包含内容和格式信息的图片?
【问题讨论】:
标签:
android
zxing
barcode-scanner
【解决方案1】:
我终于有办法让它与 zxing 条码扫描器库一起使用。
从任何已保存的图库或文件夹中获取图像时要记住的几件事,
- 图像质量应该很好。
- 图像不应旋转。(有时会导致问题)。
- 最后,确保zxing条码扫描器支持您扫描的条码。
这里是代码。
try {
Bitmap bMap = BitmapFactory.decodeResource(MyActivity.this.getResources(),
R.drawable.barcode_dummy /* ANY DUMMY IMAGE WITH BARCODE */);
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();// use this otherwise
// ChecksumException
try {
Hashtable<DecodeHintType, Object> decodeHints = new Hashtable<DecodeHintType, Object>();
decodeHints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
decodeHints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
Result result = reader.decode(bitmap, decodeHints);
Utility.ShowToastShort(MyActivity.this,
result.getText());
} catch (NotFoundException e) {
e.printStackTrace();
} catch (ChecksumException e) {
e.printStackTrace();
} catch (FormatException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
希望对您有所帮助。它对我有用。谢谢