简体   繁体   中英

Can't read barcodes contents with the new ML kit

So far, updating my app to use the new ML kit for barcode scanning is a failure. I'm using the Camera2 API and processing my images in this way:

imageToProcess = InputImage.fromMediaImage(image, 0);

Task<List<Barcode>> result = scannerCodeBarre.process(imageToProcess)
                    .addOnSuccessListener(codesBarres -> {
                         // my code to exploit the result given by the ML
                        }
                    })
                    .addOnFailureListener(e -> {
                        // my code to inform finding a barcode content failed
                    });

I can add I start to process a new image only when the result from the previous processed image has been obtained, but no matter if the previewed image contains a 2D or 3D barcode, the Task always triggers the onFailure function of it's OnFailureListener .

Can anyone tell me what I could be missing?

public void onImageAvailable(ImageReader lecteurImage) {
    Image image;
    Object objetImage;

    image = lecteurImage.acquireNextImage();

    if (image != null) {
            objetImage = transformeImage(image);
            traitementImage(objetImage);
        }
        image.close();
    }
}

protected Object transformeImage(Image image) {
    return (InputImage.fromMediaImage(image, rotation));
}

protected void traitementImage(Object image) {
    Task<List<Barcode>> résultat = scannerCodeBarre.process((InputImage) image)
            .addOnSuccessListener(codesBarres -> {
                AlertDialog.Builder constructeur;
                View vueBoite;
                CharBuffer cb;

                constructeur = new AlertDialog.Builder(activite);
                vueBoite = activite.getLayoutInflater().inflate(R.layout.resultat_lecture, null);
                constructeur.setView(vueBoite);
                constructeur.setTitle("Résultat de la lecture du code barre...");
                constructeur.setCancelable(false);
                constructeur.setNeutralButton(R.string.OK, (boite, i) -> {
                    boite.dismiss();
                });

                for (Barcode codeBarres : codesBarres) {
                    byte[] donnéeBrute;

                    donnéeBrute = codeBarres.getRawBytes();

                    switch (codeBarres.getFormat()) {
                        case FORMAT_CODE_128: {
                            ((TextView) vueBoite.findViewById(R.id.TypeCodeBarre)).setText(R.string.Code128);
                            break;
                        }
                        case FORMAT_CODE_39: {
                            ((TextView) vueBoite.findViewById(R.id.TypeCodeBarre)).setText(R.string.Code39);
                            break;
                        }
                        case FORMAT_CODE_93: {
                            ((TextView) vueBoite.findViewById(R.id.TypeCodeBarre)).setText(R.string.Code93);
                            break;
                        }
                        case FORMAT_CODABAR: {
                            ((TextView) vueBoite.findViewById(R.id.TypeCodeBarre)).setText(R.string.Codabar);
                            break;
                        }
                        case FORMAT_EAN_13: {
                            ((TextView) vueBoite.findViewById(R.id.TypeCodeBarre)).setText(R.string.EAN13);
                            break;
                        }
                        case FORMAT_EAN_8: {
                            ((TextView) vueBoite.findViewById(R.id.TypeCodeBarre)).setText(R.string.EAN8);
                            break;
                        }
                       case FORMAT_ITF: {
                            ((TextView) vueBoite.findViewById(R.id.TypeCodeBarre)).setText(R.string.ITF);
                            break;
                        }
                        case FORMAT_UPC_A: {
                            ((TextView) vueBoite.findViewById(R.id.TypeCodeBarre)).setText(R.string.UPCA);
                            break;
                        }
                        case FORMAT_UPC_E: {
                            ((TextView) vueBoite.findViewById(R.id.TypeCodeBarre)).setText(R.string.UPCE);
                            break;
                        }
                        case FORMAT_QR_CODE: {
                            ((TextView) vueBoite.findViewById(R.id.TypeCodeBarre)).setText(R.string.QRCode);
                            break;
                        }
                        case FORMAT_PDF417: {
                            ((TextView) vueBoite.findViewById(R.id.TypeCodeBarre)).setText(R.string.PDF417);
                            break;
                        }
                        case FORMAT_AZTEC: {
                            ((TextView) vueBoite.findViewById(R.id.TypeCodeBarre)).setText(R.string.AZTEC);
                            break;
                        }
                        case FORMAT_DATA_MATRIX: {
                            ((TextView) vueBoite.findViewById(R.id.TypeCodeBarre)).setText(R.string.DataMatrix);
                            break;
                        }

                        default: {
                            ((TextView) vueBoite.findViewById(R.id.TypeCodeBarre)).setText(R.string.Inconnu);
                        }
                    }

                    try {
                        cb = csd.decode(ByteBuffer.wrap(donnéeBrute));

                        ((TextView) vueBoite.findViewById(R.id.MessageCodeBarre)).setText(cb);
                        constructeur.create().show();
                    } catch (CharacterCodingException e) {
                        e.printStackTrace();
                    }
                }
            })
            .addOnFailureListener(e -> {
                Log.d("Lecteur code-barres", "Traitement terminé en échec...");
            });
}

Test the following code.

Task<List<Barcode>> result = scannerCodeBarre.process(imageToProcess);
while (!result.isComplete()) {
    anytextView.setText("");
}
if (result.isComplete() & result.isSuccessful())  yourResultProcessingMethod(result.getResult());// my code to exploit the result given by the ML
else yourFailureHandlingMethod();// my code to inform finding a barcode content failed

scannerCodeBarre.close();   

Notice that the two listeners are not coded!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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