如何去code从相机preVIEW使用的Android zxing库酒吧code? [英] How to decode a barcode from camera preview using zxing library in android?
问题描述
我想用zxing的core.jar添加库在我的项目实施在我的Android application.I'm独立的扫描仪。
I want to implement standalone scanner in my android application.I'm using zxing's core.jar library in my project.
我需要取消code从相机preview.But酒吧code,我不知道如何实现it.Because没有官方的文档。
I need to decode a barcode from camera preview.But i don't know how to implement it.Because there is no official documentation.
你能为我提供以下有关的事情一个简单的例子?
1.Initialize相机并获得preVIEW。
2.De code从preVIEW酒吧code。
Can you provide me a simple example on followings things? 1.Initialize the camera and getting preview. 2.Decode the barcode from preview.
或
是否有任何示例项目做到这一点?
Is there any example project to do this?
推荐答案
在我简单的实现请看下图:
https://github.com/piobab/$c$c-scanner
Take a look at my simple implementation: https://github.com/piobab/code-scanner
我用ZBar库,但如果你想你可以改变ZBarScannerView.java实施ZXingScannerView(在code的其余部分是确定):
I use ZBar library but if you want you can change ZBarScannerView.java implementation to ZXingScannerView (the rest of the code is ok):
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;
}
@Override
public 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:核心:2.2。你的依赖
If you use gradle add 'com.google.zxing:core:2.2' to your dependencies.
这篇关于如何去code从相机preVIEW使用的Android zxing库酒吧code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!