简体   繁体   English

Firebase ML 套件的 QR 码扫描仪可多次扫描每个 QR 码

[英]Firebase ML kit's QR Code Scanner Scans each QR Code Multiple Times

My scanner scans single QR Code multiple times thats why my createDialog method runs multiple time where i get the info regarding QR code and the user who is using it and the agent who posted it and store data into users node in Db and because it run multiple time my Db cant keep track of the no.我的扫描仪多次扫描单个 QR 码,这就是为什么我的 createDialog 方法多次运行的原因,在该方法中我获取有关 QR 码和使用它的用户以及发布它并将数据存储到 Db 中的用户节点的代理的信息,并且因为它运行多个时间我的 Db 无法跟踪编号。 of times the qr code scanned for each user..为每个用户扫描 qr 码的次数..

 private void setupCamera() {
    startAgain.setEnabled(isDetected);
    startAgain.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            isDetected = !isDetected;
        }
    });
    cameraView.setLifecycleOwner(this);
    cameraView.addFrameProcessor(new FrameProcessor() {
        @Override
        public void process(@NonNull Frame frame) {
            processorImage((FirebaseVisionImage) getVisionImageFromFrame(frame));
        }
    });
    options = new FirebaseVisionBarcodeDetectorOptions.Builder()
            .setBarcodeFormats(FirebaseVisionBarcode.FORMAT_QR_CODE)
            .build();
    detector = FirebaseVision.getInstance().getVisionBarcodeDetector(options);
}

private Object getVisionImageFromFrame(Frame frame) {
    byte[] data = frame.getData();
    FirebaseVisionImageMetadata metadata = new FirebaseVisionImageMetadata.Builder()
            .setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21)
            .setHeight(frame.getSize().getHeight())
            .setWidth(frame.getSize().getWidth())
            .build();
    return FirebaseVisionImage.fromByteArray(data, metadata);
}

private void processorImage(FirebaseVisionImage image) {
    if (!isDetected) {
        detector.detectInImage(image)
                .addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionBarcode>>() {
                    @Override
                    public void onSuccess(List<FirebaseVisionBarcode> firebaseVisionBarcodes) {
                        processResult(firebaseVisionBarcodes);
                    }
                }).addOnCompleteListener(new OnCompleteListener<List<FirebaseVisionBarcode>>() {
            @Override
            public void onComplete(@NonNull Task<List<FirebaseVisionBarcode>> task) {

            }
        })
                .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(StoreScanQR.this, e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}

private void processResult(List<FirebaseVisionBarcode> firebaseVisionBarcodes) {
    if (firebaseVisionBarcodes.size() > 0) {
        isDetected = true;
        startAgain.setEnabled(isDetected);
        for (FirebaseVisionBarcode item : firebaseVisionBarcodes) {
                    try {
                        createDialog(item.getRawValue());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
        }
    }
}

Detection is an asynchronous call, so it may be triggered multiple times with different inputs before you can get the first result.检测是一个异步调用,所以在你得到第一个结果之前,它可能会被不同的输入触发多次。 If you only care about the first detected result, you can check your isDetected flag at the result processing side (ie in #onSuccess callback) rather than detection triggering side.如果您只关心第一个检测到的结果,您可以在结果处理端(即在#onSuccess 回调中)检查您的 isDetected 标志,而不是检测触发端。

@Override
public void onSuccess(List<FirebaseVisionBarcode> firebaseVisionBarcodes) {
    if (!isDetected) {
        processResult(firebaseVisionBarcodes);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM