如何使用Android中的Zxing库从摄像头预览中解码条形码?

我想在我的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’添加到您的依赖项中.

上一篇:java – 在itext中向表中的单元格添加更多文本


下一篇:Sanic二十二:Sanic + tortoise-orm 之使用aerich执行数据库迁移