如何将Google Vision QR扫描仪设置为仅检测一个值? [英] How to set Google Vision QR Scanner to detect only one value?

查看:44
本文介绍了如何将Google Vision QR扫描仪设置为仅检测一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Google Vision API实现了QR扫描仪(QRScanner类).一旦检测到一个值,它就会使用Intents传递给另一个活动(Info类).问题是,一旦扫描了QR码,Info类就会被多次打开.我想限制QRScanner类仅获取一个QR值,而Info分类只能打开一次.

I have implemented a QR scanner(QRScanner class) using Google Vision API. Once a value is detected it is passed to another activity(Info class) using Intents. The problem is that once a QR code is scanned the Info class gets opened several times.I want to limit the QRScanner class to get only one QR value and Info classed to be opened only once.

QRScanner类

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qr_scanner);

cameraPreview = (SurfaceView) findViewById(R.id.camera_surface);
qrResult = (TextView) findViewById(R.id.scannerResult);
setupCamera();
}

private void setupCamera() {

BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.QR_CODE).build();
final CameraSource cameraSource = new CameraSource.Builder(this, barcodeDetector)
        .setAutoFocusEnabled(true)
        .setRequestedPreviewSize(1600, 1024)
        .build();

cameraPreview.getHolder().addCallback(new SurfaceHolder.Callback() {
    @Override
    public void surfaceCreated(SurfaceHolder holder) {

        if (ActivityCompat.checkSelfPermission(QrScanner.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        try {
            cameraSource.start(cameraPreview.getHolder());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        cameraSource.stop();
    }
});


barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
    @Override
    public void release() {

    }

    @Override
    public void receiveDetections(Detector.Detections<Barcode> detections) {

        final SparseArray<Barcode> qrCodes = detections.getDetectedItems();

        if(qrCodes.size()>0)
        {
            qrResult.post(new Runnable() {
                @Override
                public void run() {

                    qrResult.setText(qrCodes.valueAt(0).displayValue);
                    Intent intent = new Intent(QrScanner.this,Info.class);
                    intent.putExtra(QR_CODE,qrCodes.valueAt(0).displayValue);
                    startActivity(intent);
                }
            });
        }
    }
});
}

信息类

Intent intent = getIntent();
QRCODE = (String) intent.getStringExtra(QrScanner.QR_CODE);

DB = FirebaseDatabase.getInstance();
ref = DB.getReference().child("Animals").child(QRCODE);
ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

        String DBAnimalClass = dataSnapshot.child("class").getValue().toString();
        String DBAnimalFamily = dataSnapshot.child("family").getValue().toString();
        String DBAnimalOrder = dataSnapshot.child("order").getValue().toString();

    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
});

当前,一旦检测到QR,就会多次调用Info类.我希望QRScanner仅获得一个值,而Info类仅获得一次调用.

Currently once a QR is detected the Info class gets called several times. I want the QRScanner to get only one value and Info class to get called only once.

推荐答案

经过研究,我发现了解决方法.但是,在通过意图加载的活动中按后退"按钮返回到此活动后,扫描程序将不再起作用,因此应重新加载onCreate()方法.

After some research I found out how to fix this. However after coming back to this activity by pressing the back button from the activity loaded through intent, the scanner wont work anymore therefore the onCreate() method should be reloaded.

工作代码

if(qrCodes.size()>0)
    {

        barcodeDetector.release(); //stops the scanner from reading multiple values
        qrResult.post(new Runnable() {
            @Override
            public void run() {

                qrResult.setText(qrCodes.valueAt(0).displayValue);
                Intent intent = new Intent(QrScanner.this,Info.class);
                intent.putExtra(QR_CODE,qrCodes.valueAt(0).displayValue);
                startActivity(intent);
            }
        });
    }

cameraSource.stop()不起作用的原因在此链接中进行了解释; https://stackoverflow.com/a/41024780/6737536

The reason cameraSource.stop() won't work is explained in this link; https://stackoverflow.com/a/41024780/6737536

希望有帮助!

这篇关于如何将Google Vision QR扫描仪设置为仅检测一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆