我的扫描仪多次扫描单个 QR 码,这就是为什么我的 createDialog 方法运行多次的原因,我在其中获取有关 QR 码和使用它的用户以及发布它并将数据存储到 Db 中的用户节点的代理的信息,并且因为它运行多个时间我的 Db 无法跟踪编号。为每个用户扫描 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();
}
}
}
}