ML Kit Android Studio java,如何进行多图像文本识别?

发布于 2025-01-09 17:43:20 字数 2689 浏览 0 评论 0原文

我有一个包含大约 10 张图像的文件夹,我喜欢用它们 OCR 提取文本。 这对于一张图片来说效果很好,但我的 java 技能还不足以实现多张图片。

如果有人能向我展示一个干净的解决方案,我真的很感激。

多谢 br卢卡斯

TextView output1;
ArrayList<Bitmap> bitmapArray = new ArrayList<Bitmap>();
TextRecognizer recognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS);



private void OCR_list()
{
    String path = Environment.getExternalStorageDirectory().toString()+"/folder_with_images";
        File directory = new File(path);
        File[] files = directory.listFiles();
        for (int i = 0; i < files.length; i++) {
            output1.setText(output1.getText() + ", " + files[i].getName());

            File imgFile = files[i];

            if (imgFile.exists()) {
                bitmapArray.add(BitmapFactory.decodeFile(imgFile.getAbsolutePath()));
            } else {
                output1.setText(output1.getText()+"\n Bitmap not found!");
                return;
            }
        }

    InputImage image = InputImage.fromBitmap(bitmapArray.get(0), 0);
    recognizer.process(image)
            .addOnSuccessListener(
                    new OnSuccessListener<Text>() {
                        @Override
                        public void onSuccess(Text texts) {
                            processTextRecognitionResult(texts);
                        }
                    })
            .addOnFailureListener(
                    new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            e.printStackTrace();
                        }
                    });

编辑:

我现在用这种方式解决了它,但看起来很糟糕:

private void new_Recognition(InputImage image) {

    recognizer.process(image)
            .addOnSuccessListener(
                    new OnSuccessListener<Text>() {
                        @Override
                        public void onSuccess(Text texts) {
                            processTextRecognitionResult(texts);
                            bitmapArray.remove(0);
                            if (!bitmapArray.isEmpty()) {
                                InputImage image = InputImage.fromBitmap(bitmapArray.get(0), 0);
                                new_Recognition(image);
                            }

                        }
                    })
            .addOnFailureListener(
                    new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            e.printStackTrace();
                        }
                    });
}

I have a folder with about 10 images which I like to OCR extract text.
That works excellent for 1 picture, but my java skills are not good enough to implement that for multiple images.

I'm really appreciate if someone could show me a clean solution for that.

Thanks a lot
br Lukas

TextView output1;
ArrayList<Bitmap> bitmapArray = new ArrayList<Bitmap>();
TextRecognizer recognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS);



private void OCR_list()
{
    String path = Environment.getExternalStorageDirectory().toString()+"/folder_with_images";
        File directory = new File(path);
        File[] files = directory.listFiles();
        for (int i = 0; i < files.length; i++) {
            output1.setText(output1.getText() + ", " + files[i].getName());

            File imgFile = files[i];

            if (imgFile.exists()) {
                bitmapArray.add(BitmapFactory.decodeFile(imgFile.getAbsolutePath()));
            } else {
                output1.setText(output1.getText()+"\n Bitmap not found!");
                return;
            }
        }

    InputImage image = InputImage.fromBitmap(bitmapArray.get(0), 0);
    recognizer.process(image)
            .addOnSuccessListener(
                    new OnSuccessListener<Text>() {
                        @Override
                        public void onSuccess(Text texts) {
                            processTextRecognitionResult(texts);
                        }
                    })
            .addOnFailureListener(
                    new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            e.printStackTrace();
                        }
                    });

Edit:

I solved it now this way, but looks awful:

private void new_Recognition(InputImage image) {

    recognizer.process(image)
            .addOnSuccessListener(
                    new OnSuccessListener<Text>() {
                        @Override
                        public void onSuccess(Text texts) {
                            processTextRecognitionResult(texts);
                            bitmapArray.remove(0);
                            if (!bitmapArray.isEmpty()) {
                                InputImage image = InputImage.fromBitmap(bitmapArray.get(0), 0);
                                new_Recognition(image);
                            }

                        }
                    })
            .addOnFailureListener(
                    new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            e.printStackTrace();
                        }
                    });
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴骨ら 2025-01-16 17:43:20

您可以直接迭代输入,识别任务将排队,然后在内部按顺序处理。

for (Bitmap input : inputs) {
  recognizer.process(input)
      .addOnSuccessListener(text -> ...)
}

You can iterate on inputs directly, and recognition tasks will be queued up and then processed in order internally.

for (Bitmap input : inputs) {
  recognizer.process(input)
      .addOnSuccessListener(text -> ...)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文