gpt4 book ai didi

android - 如何在 android 中使用 zxing 库从相机预览中解码条码?

转载 作者:行者123 更新时间:2023-11-29 15:17:05 26 4
gpt4 key购买 nike

我想在我的 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;
}

@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:core:2.2”添加到您的依赖项中。

关于android - 如何在 android 中使用 zxing 库从相机预览中解码条码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23156399/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com