android - OCR,将识别的文本放在相机 View 上

标签 android tesseract

我正在尝试在相机 View 上打印已识别的文本。我收到此错误:

无法在未调用 Looper.prepare() 的线程内创建处理程序

我对这个问题做了一些研究,但即使我不能解决它可能是因为经验的运气(我第一次尝试使用 openCV 和 tesseract 构建应用程序,我遇到了许多以前从未见过的错误) .请宽大处理。下面添加了我想在其中使用 Toast 的代码:

import android.content.Context;
import android.graphics.Bitmap;
import org.opencv.core.Mat;
import android.os.Environment;

import android.util.Log;
import android.widget.Toast;

import com.googlecode.tesseract.android.TessBaseAPI;

public class justOCR {
    Bitmap bitmap;
    protected Context context;

    public justOCR(Context context){
        this.context = context;
    }

        public static final String DATA_PATH = Environment
                .getExternalStorageDirectory().toString() + "/sdcard/CameraTest/";
        private static final String lang = null;

    public void numPlateOCR(Mat plate){ 

        bitmap=Bitmap.createBitmap(plate.width(), plate.height(), Bitmap.Config.ARGB_8888);
        org.opencv.android.Utils.matToBitmap(plate, bitmap);
        bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
        // Convert to ARGB_8888, required by tess
        //byteArray = byteArray.copy(Bitmap.Config.ARGB_8888, true);


    // _image.setImageBitmap( bitmap );

    Log.v("baseApi", "Before baseApi");

    TessBaseAPI baseApi = new TessBaseAPI();
    baseApi.setDebug(true);
    //baseApi.init(DATA_PATH, lang);
    baseApi.init("/sdcard/CameraTest/", "eng");
    baseApi.setVariable("tessedit_char_whitelist", "1234567890");
    baseApi.setImage(bitmap);



    String recognizedText = baseApi.getUTF8Text();

    baseApi.end();

    // You now have the text in recognizedText var, you can do anything with it.
    // We will display a stripped out trimmed alpha-numeric version of it (if lang is eng)
    // so that garbage doesn't make it to the display.

    Log.v("NUMBER", "OCRED TEXT: " + recognizedText);

    //if ( lang.equalsIgnoreCase("eng") ) {
    //  recognizedText = recognizedText.replaceAll("[^a-zA-Z0-9]+", " ");
    //}

    recognizedText = recognizedText.trim();

    if ( recognizedText.length() != 0 ) {
        //TODO print or save to xml//

        Toast.makeText(this.context, "***"+recognizedText+"***", Toast.LENGTH_SHORT).show();

    }
}

}

感谢您的帮助。

最佳答案

另一个人说,你正试图在不在 UI 线程上做一个 Toast。

但是看这个,对你很重要:How to ask

关于android - OCR,将识别的文本放在相机 View 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14530411/

相关文章:

imagemagick - 使用 ImageMagick 和 Tesseract 从图像中获取文本

ios - 如何将 TesseractOCRiOS 与西类牙语一起使用?

java - Android gson 无效 ClassCastException

android - Android的位图不为空,但总是使nullpointerexception

android - 从资源角度来看,使用 context.getString() 中的字符串无限循环刷新 TextView(每 n 秒)是否可以?

java - Eclipse logcat 调试

android - android中的slider和seekbar有什么区别

Python Tesseract 段错误 11

ios - Xcode 5 llvm-g++ 和 llvm-gcc 路径?

text - 如何向 Tesseract 4.0 添加新字体?