如何从图像Android应用程序中提取文本

问题描述 投票:9回答:3

我正在为我的Android应用程序开发一项功能。我想从图片中读取文本,然后将该文本保存在数据库中。使用OCR是最好的方法吗?还有另外一种方法吗? Google在其文档中建议NDK只应在严格必要时使用,但究竟是什么垮台?

任何帮助都会很棒。

Image to be OCR'd

enter image description here

enter image description here

enter image description here

image text ocr tesseract
3个回答
8
投票

您可以使用谷歌视觉库将图像转换为文本,它将提供更好的图像输出。在构建gradle中添加以下库:

   compile 'com.google.android.gms:play-services-vision:10.0.0+'

    TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();

Frame imageFrame = new Frame.Builder()

        .setBitmap(bitmap)                 // your image bitmap
        .build();

String imageText = "";


SparseArray<TextBlock> textBlocks = textRecognizer.detect(imageFrame);

for (int i = 0; i < textBlocks.size(); i++) {
    TextBlock textBlock = textBlocks.get(textBlocks.keyAt(i));
    imageText = textBlock.getValue();                   // return string
}

1
投票

在这个Simple example of OCRReader in Android教程中,您可以从图像中读取文本,也可以使用非常简单的代码使用相机扫描文本。

该库是使用Mobile Vision Text API开发的

用于从相机扫描文本

OCRCapture.Builder(this)
        .setUseFlash(true)
        .setAutoFocus(true)
        .buildWithRequestCode(CAMERA_SCAN_TEXT);

用于从图像中提取文本

String text = OCRCapture.Builder(this).getTextFromUri(pickedImage);
//You can also use getTextFromBitmap(Bitmap bitmap) or getTextFromImage(String imagePath) buplic APIs from OCRLibrary library.

0
投票

有一个不同的选择。您可以将图像上传到服务器,从服务器OCR,然后获得结果。

© www.soinside.com 2019 - 2024. All rights reserved.