简体   繁体   English

无法使用新的 ML 套件读取条形码内容

[英]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.到目前为止,更新我的应用程序以使用新的 ML 套件进行条形码扫描是失败的。 I'm using the Camera2 API and processing my images in this way:我正在使用Camera2 API 并以这种方式处理我的图像:

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 .我可以添加只有在获得上一个处理图像的结果时才开始处理新图像,但无论预览图像是否包含 2D 或 3D 条形码, Task总是触发它的OnFailureListeneronFailure function 。

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!请注意,这两个侦听器没有编码!

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

相关问题 如何从 Android firebase ML-Kit BarcodeScannerProcessor onSuccess 找到上下文并启动新活动 - How can I find context and start a new Activity from Android firebase ML-Kit BarcodeScannerProcessor onSuccess 无法使用 Firebase ML Kit 读取更多 30 个字符的条形码 - Not able to read more 30 chars barcode using Firebase ML Kit 我可以阅读,但不能编辑主课内容 - I can read but Can't edit Main class contents 内容更改后如何重新读取文本文件的新内容? - How can I re-read the new contents of a text file after the contents have changed? Java无法读取字符是换行符 - Java can't read that the character is a new line 我怎样才能只从 firebase ML Kit 人脸检测中获得情绪? - How can I only get the emotion from firebase ML Kit face detection? 如何在不让用户首先关注文本框的情况下阅读条形码? - How can I read barcodes without having the user focus a text box first? JPA的新功能-可以写入,但不能从数据库读取 - New to JPA - can write, but can't read from database 我用JAVA FOR LOOP创建的条形码似乎无法将每个图像都放在MS Word中 - the barcodes i created with JAVA FOR LOOP can't seem to put every image in MS Word 如何使用Firebase ML套件识别地标? - How to recognize landmarks using Firebase ML kit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM