我想在我的android应用程序中实现独立扫描仪.我在项目中使用zxing的core.jar库.
我需要从相机预览中解码条形码.但是我不知道如何实现.因为没有官方文档.
您能为我提供一个关于跟随事物的简单示例吗?
1.初始化相机并获取预览.
2.从预览中解码条形码.
要么
是否有任何示例项目可以做到这一点?
解决方法:
看一下我的简单实现:
https://github.com/piobab/code-scanner
我使用Zbar库,但是如果您愿意,可以将ZbarScannerVIEw.java实现更改为ZXingScannerVIEw(其余代码可以):
public class ZXingScannerVIEw extends ScannerVIEw {public interface ResultHandler { public voID handleResult(Result result);}private MultiFormatReader mMultiFormatReader;private ResultHandler mResultHandler;public ZXingScannerVIEw(Context context) { super(context); setupScanner(null);}public ZXingScannerVIEw(Context context, AttributeSet attributeSet) { super(context, attributeSet); setupScanner(null);}/** * Specify recognized codes types. * @param codeTypes List of codes types from ZXing library */public voID setCodeTypes(List<com.Google.zxing.barcodeFormat> codeTypes) { setupScanner(codeTypes);}private voID setupScanner(List<com.Google.zxing.barcodeFormat> symbols) { Map<DecodeHintType,Object> hints = new EnumMap<DecodeHintType,Object>(DecodeHintType.class); // Add specific formats hints.put(DecodeHintType.POSSIBLE_FORMATS, symbols); mMultiFormatReader = new MultiFormatReader(); mMultiFormatReader.setHints(hints);}/** * Register callback in order to receive data from scanner. * @param resultHandler */public voID setResultHandler(ResultHandler resultHandler) { mResultHandler = resultHandler;}@OverrIDepublic voID onPrevIEwFrame(byte[] data, Camera camera) { Camera.Parameters parameters = camera.getParameters(); Camera.Size size = parameters.getPrevIEwSize(); int wIDth = size.wIDth; int height = size.height; Result rawResult = null; PlanarYUVluminanceSource source = buildluminanceSource(data, wIDth, height); if(source != null) { BinaryBitmap bitmap = new BinaryBitmap(new HybrIDBinarizer(source)); try { rawResult = mMultiFormatReader.decodeWithState(bitmap); } catch (ReaderException re) { } catch (NullPointerException npe) { } catch (Arrayindexoutofboundsexception aoe) { } finally { mMultiFormatReader.reset(); } } if (rawResult != null) { stopCamera(); if(mResultHandler != null) { mResultHandler.handleResult(rawResult); } } else { camera.setoneshotPrevIEwCallback(this); }}public PlanarYUVluminanceSource buildluminanceSource(byte[] data, int wIDth, int height) { Rect rect = getFramingRectInPrevIEw(wIDth, height); if (rect == null) { return null; } // Go ahead and assume it's YUV rather than dIE. PlanarYUVluminanceSource source = null; try { source = new PlanarYUVluminanceSource(data, wIDth, height, rect.left, rect.top, rect.wIDth(), rect.height(), false); } catch(Exception e) { } return source;}
}
如果您使用gradle,请将’com.Google.zxing:core:2.2’添加到您的依赖项中.
总结以上是内存溢出为你收集整理的如何使用Android中的Zxing库从摄像头预览中解码条形码?全部内容,希望文章能够帮你解决如何使用Android中的Zxing库从摄像头预览中解码条形码?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)