如何像微信一样扫码自由?

https://github.com/devilsen/CZXing

我在使用zxing的总是想扫码的时候怎么才能够快速校准,多个二维码扫描的时候怎么才能指定二维码呢。于是我在Github上找到了CZxing这个控件。在使用的时候发现了有些小问题。

扫码结果没有回调。
回调没有反应;是因为 ScanLayout 没有实现 ScanListener.onScanSuccess 这个方法。

   Scanner.with(this)
                .setMaskColor(resources.getColor(R.color.mask_color))
                .setBorderColor(resources.getColor(R.color.box_line))
                .setBorderSize(BarCodeUtil.dp2px(this, 200))
//                .setBorderSize(BarCodeUtil.dp2px(this, 200), BarCodeUtil.dp2px(this, 100))
                .setCornerColor(resources.getColor(R.color.corner))
                .setScanLineColors(scanColors)
//                .setHorizontalScanLine()
//                .setBarcodeFormat(BarcodeFormat.EAN_13)
                .setTitle("")
                .showAlbum(true)
                .enableOpenCVDetect(true)
                .setScanNoticeText("扫描二维码")
                .setFlashLightOnText("打开闪光灯")
                .setFlashLightOffText("关闭闪光灯")
//                .setFlashLightInvisible()
                .setFlashLightOnDrawable(R.drawable.ic_highlight_blue_open_24dp)
                .setFlashLightOffDrawable(R.drawable.ic_highlight_white_close_24dp)
                .continuousScan()
                .detectorModel(detectorPrototxtPath, detectorCaffeModelPath)
                .superResolutionModel(superResolutionPrototxtPath, superResolutionCaffeModelPath)
//                .enableOpenCVDetect(true)
                .setOnClickAlbumDelegate(new ScanActivityDelegate.OnClickAlbumDelegate() {
                    @Override
                    public void onClickAlbum(Activity activity) {
                        Intent albumIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        activity.startActivityForResult(albumIntent, CODE_SELECT_IMAGE);
                    }

                    @Override
                    public void onSelectData(int requestCode, Intent data) {
                        if (requestCode == CODE_SELECT_IMAGE) {
                            decodeImage(data);
                        }
                    }
                })
                .setOnScanResultDelegate(new ScanActivityDelegate.OnScanDelegate() {
                    @Override
                    public void onScanResult(@NonNull final Activity activity, @NonNull final String result, @NonNull BarcodeFormat format) {
                        final String showContent = "format: " + format.name() + "  code: " + result;
                      
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                  
                                Toast.makeText(MainActivity.this, showContent, Toast.LENGTH_SHORT).show();
                                finish();
                            }
                        });
                    }
                })
                .start();

ScanLayout.java

    @Override
    public void onReadCodeResult(List<CodeResult> resultList) {
        for (CodeResult result : resultList) {
            BarCodeUtil.d("result : " + result.toString());
        }
        //大于2个二维码,则表两个记号
        if (resultList.size() > 1) {
            showResultPoint(resultList);
        } else {
        	//result<=1的时候则返回结果
            if (mScanListener != null) {
                mScanListener.onScanSuccess(resultList);
            }
        }
    }
	
	//标记扫描点
    private void addPointView(final CodeResult result, int resultSize) {
        int[] points = result.getPoints();
        if (points == null || points.length < 4) return;
        int x = points[0];
        int y = points[1];
        int width = points[2];
        int height = points[3];

        final PointView view = new PointView(getContext());
        if (resultSize > 1) {
            view.drawArrow();
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mScanListener != null) {
                        mScanListener.onClickResult(result);
                    }
                }
            });
        }
        mResultViews.add(view);
        if (mResultColor > 0) {
            view.setColor(mResultColor);
        }

        int xOffset = (width - mPointSize) / 2;
        int yOffset = (height - mPointSize) / 2;

        xOffset = Math.max(xOffset, 0);
        yOffset = Math.max(yOffset, 0);

        final LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.leftMargin = x + xOffset;
        params.topMargin = y + yOffset;

        mHandler.post(new Runnable() {
            @Override
            public void run() {
                addView(view, params);
            }
        });
        //关闭相机扫描
        closeCamera();
        //隐藏扫描线
        if (mScanBox != null) {
           runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mScanBox.setVisibility(View.GONE);
                }
            });
        }
    }
这样写会报错的
       mScanBox.setVisibility(View.GONE);
       

如何像微信一样扫码自由?_第1张图片

你可能感兴趣的:(android,studio,zxing)