android媲美微信扫码库

语言: CN / TW / HK

之前使用的是zxing封装的库,但是识别率和识别速度没法和微信比较,现在使用的Google开源识别库完全可以和微信媲美

github:https://github.com/DyncKathline/ZxingScan

强烈推荐MIKit Barcode Scanning

识别速度超快,基本上camera抓取到二维码就能识别到其内容(这是重点)。
基于MIKit Barcode Scanning的识别库进行封装,操作简单。
支持识别多个二维码,条形码。
支持任意比例展示,可以1:2,1.5:2等,不会发生像拉伸变形。
使用camera,不是cameraX哦。

效果图

在这里插入图片描述在这里插入图片描述
第一个是Google开源的,第二个是zxing开源的

使用方式

build.gradle引用

implementation 'com.github.dynckathline:barcode:2.5'

初始化和监听结果回调

``` //构造出扫描管理器 configViewFinderView(viewfinderView); mlKit = new MLKit(this, preview, graphicOverlay); //是否扫描成功后播放提示音和震动 mlKit.setPlayBeepAndVibrate(true, true); //仅识别二维码 BarcodeScannerOptions options = new BarcodeScannerOptions.Builder() .setBarcodeFormats( Barcode.FORMAT_QR_CODE, Barcode.FORMAT_AZTEC) .build(); mlKit.setBarcodeFormats(null); mlKit.setOnScanListener(new MLKit.OnScanListener() { @Override public void onSuccess(List barcodes, @NonNull GraphicOverlay graphicOverlay, InputImage image) { showScanResult(barcodes, graphicOverlay, image); }

        @Override
        public void onFail(int code, Exception e) {

        }
    });

```

展示结果

``` private void showScanResult(List barcodes, @NonNull GraphicOverlay graphicOverlay, InputImage image) { if (barcodes.isEmpty()) { return; }

    mlKit.setAnalyze(false);
    CustomDialog.Builder builder = new CustomDialog.Builder(context);
    CustomDialog dialog = builder
            .setContentView(R.layout.barcode_result_dialog)
            .setLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            .setOnInitListener(new CustomDialog.Builder.OnInitListener() {
                @Override
                public void init(CustomDialog customDialog) {
                    Button btnDialogCancel = customDialog.findViewById(R.id.btnDialogCancel);
                    Button btnDialogOK = customDialog.findViewById(R.id.btnDialogOK);
                    TextView tvDialogContent = customDialog.findViewById(R.id.tvDialogContent);
                    ImageView ivDialogContent = customDialog.findViewById(R.id.ivDialogContent);

                    Bitmap bitmap = null;
                    ByteBuffer byteBuffer = image.getByteBuffer();
                    if (byteBuffer != null) {
                        FrameMetadata.Builder builder = new FrameMetadata.Builder();
                        builder.setWidth(image.getWidth())
                                .setHeight(image.getHeight())
                                .setRotation(image.getRotationDegrees());
                        bitmap = BitmapUtils.getBitmap(byteBuffer, builder.build());
                    } else {
                        bitmap = image.getBitmapInternal();
                    }
                    if (bitmap != null) {
                        graphicOverlay.add(new CameraImageGraphic(graphicOverlay, bitmap));
                    } else {
                        ivDialogContent.setVisibility(View.GONE);
                    }
                    SpanUtils spanUtils = SpanUtils.with(tvDialogContent);
                    for (int i = 0; i < barcodes.size(); ++i) {
                        Barcode barcode = barcodes.get(i);
                        BarcodeGraphic graphic = new BarcodeGraphic(graphicOverlay, barcode);
                        graphicOverlay.add(graphic);
                        Rect boundingBox = barcode.getBoundingBox();
                        spanUtils.append(String.format("(%d,%d)", boundingBox.left, boundingBox.top))
                                .append(barcode.getRawValue())
                                .setClickSpan(i % 2 == 0 ? getResources().getColor(R.color.colorPrimary) : getResources().getColor(R.color.colorAccent), false, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(getApplicationContext(), barcode.getRawValue(), Toast.LENGTH_SHORT).show();
                            }
                        })
                                .setBackgroundColor(i % 2 == 0 ? getResources().getColor(R.color.colorAccent) : getResources().getColor(R.color.colorPrimary))
                                .appendLine()
                                .appendLine();
                    }
                    spanUtils.create();
                    Bitmap bitmapFromView = loadBitmapFromView(graphicOverlay);
                    ivDialogContent.setImageBitmap(bitmapFromView);

                    btnDialogCancel.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            customDialog.dismiss();
                            finish();
                        }
                    });
                    btnDialogOK.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            customDialog.dismiss();
                            mlKit.setAnalyze(true);
                        }
                    });
                }
            })
            .build();
}

```