Making an Android OCR Application with Tesseract

Tesseract is a well-known open source OCR engine that released under the Apache License 2.0. In this tutorial, I’d like to share how to build the OCR library for Android, as well as how to implement a simple Android OCR application with it.

Dynamsoft Barcode Reader SDK
Ads Powered by Dynamsoft

ocr_img

do_ocr_select

Tesseract Android Tools

To build the Tesseract OCR library for Android, we can use the tesseract-android-tools provided by Google.

Get the source code:

git clone https://code.google.com/p/tesseract-android-tools/

Open README, and take the following steps:

cd <project-directory>
curl -O https://tesseract-ocr.googlecode.com/files/tesseract-ocr-3.02.02.tar.gz
curl -O http://leptonica.googlecode.com/files/leptonica-1.69.tar.gz
tar -zxvf tesseract-ocr-3.02.02.tar.gz
tar -zxvf leptonica-1.69.tar.gz
rm -f tesseract-ocr-3.02.02.tar.gz
rm -f leptonica-1.69.tar.gz
mv tesseract-3.02.02 jni/com_googlecode_tesseract_android/src
mv leptonica-1.69 jni/com_googlecode_leptonica_android/src
ndk-build -j8
android update project --target 1 --path .
ant debug (release)

Note: if you are using NDK r9, the building will fail with the error:

format not a string literal and no format arguments [-Werror=format-security]

To solve it, open Application.mk, and add the following line:

APP_CFLAGS += -Wno-error=format-security

After successfully building the OCR library, you will get the class.jar in folder bin and relevant *.so in folder libs.

If you can’t successfully build the source code, please download the jni.zip and copy all source code to your project folder.

Android OCR Application

Create an Android project, and import the relevant libraries.

To do OCR, we can create a class named TessOCR:

public class TessOCR {
	private TessBaseAPI mTess;

	public TessOCR() {
		// TODO Auto-generated constructor stub
		mTess = new TessBaseAPI();
		String datapath = Environment.getExternalStorageDirectory() + "/tesseract/";
		String language = "eng";
		File dir = new File(datapath + "tessdata/");
		if (!dir.exists()) 
			dir.mkdirs();
		mTess.init(datapath, language);
	}

	public String getOCRResult(Bitmap bitmap) {

		mTess.setImage(bitmap);
		String result = mTess.getUTF8Text();

		return result;
    }

	public void onDestroy() {
		if (mTess != null)
			mTess.end();
	}

}

In the constructor, we need to check the directory tessdata. If it doesn’t exist, an exception will be thrown in init(). If you want to know why, read the source code:

public boolean init(String datapath, String language) {
        if (datapath == null) {
            throw new IllegalArgumentException("Data path must not be null!");
        }
        if (!datapath.endsWith(File.separator)) {
            datapath += File.separator;
        }

        File tessdata = new File(datapath + "tessdata");
        if (!tessdata.exists() || !tessdata.isDirectory()) {
            throw new IllegalArgumentException("Data path must contain subfolder tessdata!");
        }

        return nativeInit(datapath, language);
    }

Pretty simple! Now we can use three different ways to load images and do OCR:

Browsing images in gallery, and sending one image to the OCR application

In AndroidManifest.xml, add the following intent filter:

<intent-filter>
                <action android:name="android.intent.action.SEND" />

                <category android:name="android.intent.category.DEFAULT" />
				<data android:mimeType="text/plain" />
                <data android:mimeType="image/*" />
</intent-filter>

Decode the image URI:

if (Intent.ACTION_SEND.equals(intent.getAction())) {
    Uri uri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
    uriOCR(uri);
}
private void uriOCR(Uri uri) {
		if (uri != null) {
			InputStream is = null;
			try {
				is = getContentResolver().openInputStream(uri);
				Bitmap bitmap = BitmapFactory.decodeStream(is);
				mImage.setImageBitmap(bitmap);
				doOCR(bitmap);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				if (is != null) {
					try {
						is.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
}

Picking an image from gallery

Send the Intent for picking images, and decode the returned URI in onActivityResult:

Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQUEST_PICK_PHOTO);

Taking a picture from camera

To get high-quality images, attach the file path to the Intent:

private void dispatchTakePictureIntent() {
		Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		// Ensure that there's a camera activity to handle the intent
		if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
			// Create the File where the photo should go
			File photoFile = null;
			try {
				photoFile = createImageFile();
			} catch (IOException ex) {
				// Error occurred while creating the File

			}
			// Continue only if the File was successfully created
			if (photoFile != null) {
				takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
						Uri.fromFile(photoFile));
				startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
			}
		}
}

Before running the Android OCR app, do not forget to download the relevant language data packages and push them to your phone storage.

Source Code

https://github.com/yushulx/android-tesseract-ocr

  • Enif Labs

    Hi Matheus
    I think you may mannually set the permissions for this application
    for that go to Settings —> Application or Apps — Select the installed application —–> Permissions and give the permissions to camera

    I think it may help you

    Thank you

  • http://www.dynamsoft.com/ Xiao Ling

    You have to request the permission you need. Here is the tutorial https://developer.android.com/training/permissions/requesting.html

  • Salah Eddine Ait Youssef

    How can i do that ? i have android 7

  • Geminie Ali

    Hi
    I use your code but the app is crashes i don’t understand whats the problem is??

  • http://www.dynamsoft.com/ Xiao Ling

    Download NDK from developer.android.com. Build the native code using the command ‘ndk-build’.

  • keyur

    how to build NDK????

  • http://www.dynamsoft.com/ Xiao Ling

    Extract jni.zip to jni folder that located under the root directory of your project. Then build the native code with NDK.

  • keyur

    jni.zip file copy where folder in project that i have created..???????
    plz reply it on urgrnt…..

  • http://www.dynamsoft.com/ Xiao Ling

    Do you have any Logcat information? If you are running the app on Android 6.0 or above, the code won’t work because you have to programmatically get the storage permission.

  • Ayşegül Gürmeriç

    Hi Xiao,
    I didn’t change the code. Setting the resource code is below

    public TessOCR() {
    // TODO Auto-generated constructor stub
    mTess = new TessBaseAPI();
    String datapath = Environment.getExternalStorageDirectory() + “/tesseract/”;
    String language = “eng”;
    File dir = new File(datapath + “tessdata/”);
    if (!dir.exists())
    dir.mkdirs();
    mTess.init(datapath, language);
    }

    You can see datapath’s value in the picture. what am I doing wrong?

  • http://www.dynamsoft.com/ Xiao Ling

    adb push /sdcard/

  • http://www.dynamsoft.com/ Xiao Ling

    Please check whether you have set the resource path correctly.

  • Ayşegül Gürmeriç

    hi,
    I imported project to android studio ide. But I couldnt make it work well. OCR didn’t work. I downloaded language package of english and put it to storage folder of my phone. (as below). How can I fix this ?

  • siddharth

    I am getting this error when I build
    D:FSLNEWtess-two-mastertess-two>ndk-build -j8
    [armeabi] Compile thumb : lept <= bilateral.c
    [armeabi] Compile thumb : lept libs/armeabi/libjpgt.so
    [armeabi] Compile thumb : lept <= boxbasic.c
    [armeabi] Compile thumb : lept <= bmpiostub.c
    [armeabi] Compile thumb :[armeabi] lept <= bootnumgen1.c Compile thumb :
    lept <= bmpio.c
    [armeabi] Compile thumb : lept <= bootnumgen2.c
    [armeabi] Compile thumb : lept <= boxfunc1.c
    jni/com_googlecode_leptonica_android/src/src/bootnumgen1.c: In function 'l_bootn
    um_gen1':
    jni/com_googlecode_leptonica_android/src/src/bootnumgen1.c:262:11: warning: assi
    gnment makes pointer from integer without a cast
    data1 = decodeBase64(l_bootnum1, strlen(l_bootnum1), &size1);
    ^
    jni/com_googlecode_leptonica_android/src/src/bootnumgen2.c: In function 'l_bootn
    um_gen2':
    jni/com_googlecode_leptonica_android/src/src/bootnumgen2.c:278:11: warning: assi
    gnment makes pointer from integer without a cast
    data1 = decodeBase64(l_bootnum2, strlen(l_bootnum2), &size1);
    ^
    [armeabi] Compile thumb : lept <= boxfunc3.c
    [armeabi] Compile thumb : lept <= boxfunc2.c
    jni/com_googlecode_leptonica_android/src/src/bilateral.c: In function 'pixBilate
    ral':
    jni/com_googlecode_leptonica_android/src/src/bilateral.c:176:9: warning: return
    makes pointer from integer without a cast
    return pixBilateralGray(pixs, spatial_stdev, range_stdev,
    ^
    jni/com_googlecode_leptonica_android/src/src/bilateral.c:180:10: warning: assign
    ment makes pointer from integer without a cast
    pixr = pixBilateralGray(pixt, spatial_stdev, range_stdev, ncomps,
    ^
    jni/com_googlecode_leptonica_android/src/src/bilateral.c:184:10: warning: assign
    ment makes pointer from integer without a cast
    pixg = pixBilateralGray(pixt, spatial_stdev, range_stdev, ncomps,
    ^
    jni/com_googlecode_leptonica_android/src/src/bilateral.c:188:10: warning: assign
    ment makes pointer from integer without a cast
    pixb = pixBilateralGray(pixt, spatial_stdev, range_stdev, ncomps,
    ^
    jni/com_googlecode_leptonica_android/src/src/bilateral.c: At top level:
    jni/com_googlecode_leptonica_android/src/src/bilateral.c:214:1: error: conflicti
    ng types for 'pixBilateralGray'
    pixBilateralGray(PIX *pixs,
    ^
    jni/com_googlecode_leptonica_android/src/src/bilateral.c:176:16: note: previous
    implicit declaration of 'pixBilateralGray' was here
    return pixBilateralGray(pixs, spatial_stdev, range_stdev,
    ^
    jni/com_googlecode_leptonica_android/src/src/bilateral.c: In function 'pixBilate
    ralExact':
    jni/com_googlecode_leptonica_android/src/src/bilateral.c:590:9: warning: return
    makes pointer from integer without a cast
    return pixBilateralGrayExact(pixs, spatial_kel, range_kel);
    ^
    jni/com_googlecode_leptonica_android/src/src/bilateral.c:593:14: warning: assign
    ment makes pointer from integer without a cast
    pixr = pixBilateralGrayExact(pixt, spatial_kel, range_kel);
    ^
    jni/com_googlecode_leptonica_android/src/src/bilateral.c:596:14: warning: assign
    ment makes pointer from integer without a cast
    pixg = pixBilateralGrayExact(pixt, spatial_kel, range_kel);
    ^
    jni/com_googlecode_leptonica_android/src/src/bilateral.c:599:14: warning: assign
    ment makes pointer from integer without a cast
    pixb = pixBilateralGrayExact(pixt, spatial_kel, range_kel);
    ^
    jni/com_googlecode_leptonica_android/src/src/bilateral.c: At top level:
    jni/com_googlecode_leptonica_android/src/src/bilateral.c:623:1: error: conflicti
    ng types for 'pixBilateralGrayExact'
    pixBilateralGrayExact(PIX *pixs,
    ^
    jni/com_googlecode_leptonica_android/src/src/bilateral.c:590:16: note: previous
    implicit declaration of 'pixBilateralGrayExact' was here
    return pixBilateralGrayExact(pixs, spatial_kel, range_kel);
    ^
    jni/com_googlecode_leptonica_android/src/src/bilateral.c: In function 'pixBlockB
    ilateralExact':
    jni/com_googlecode_leptonica_android/src/src/bilateral.c:744:15: warning: assign
    ment makes pointer from integer without a cast
    range_kel = makeRangeKernel(range_stdev);
    ^
    jni/com_googlecode_leptonica_android/src/src/bilateral.c: At top level:
    jni/com_googlecode_leptonica_android/src/src/bilateral.c:770:1: error: conflicti
    ng types for 'makeRangeKernel'
    makeRangeKernel(l_float32 range_stdev)
    ^
    jni/com_googlecode_leptonica_android/src/src/bilateral.c:744:17: note: previous
    implicit declaration of 'makeRangeKernel' was here
    range_kel = makeRangeKernel(range_stdev);
    ^
    make: *** [obj/local/armeabi/objs/lept/src/src/bilateral.o] Error 1

  • Bernardo

    Hi man thank you for your tutorial. I am having several problems and would appreciate if you helped me. It seems that the program is unable to find the english language. I can run the application and take a picture, but when it is processing it the application crashes.

  • Justin Nguyen

    Hi, I’m using android SDK. could you please tell me how can I follow those steps?

  • Ahmad ELkhwaga

    plz tell me how to do this… i run the on my device and i have this erorr “Could not initialize Tesseract API with language=eng!”

  • Vishal Thakkar

    is there any solution for recognize high resolution image from gallery ?
    because when i pick some image it says bitmap to large .. i cant get any text . Thanks in advance

  • Vishal Thakkar

    i have solved it. Thank you For Your Response :)

  • Vishal Thakkar

    i Solved it . Thank you so much . your tutorial help me a lot. :)

  • http://www.dynamsoft.com/ Xiao Ling

    See this line? String datapath = Environment.getExternalStorageDirectory() + “/tesseract/”;

  • Vishal Thakkar

    Hey Bro can u tell me where should i place tessdata/eng.traineddata ??

    i m geting this error because of that.!!

  • http://www.dynamsoft.com Desmond Shaw

    It’s NullPointerException. Have you debugged your code to locate the null reference?

  • Vishal Thakkar

    i got this error log when i take photo from camera or pick photo from gallery

    01-27 15:40:32.041 11381-11381/com.example.vish.myapplication I/Timeline: Timeline: Activity_launch_request time:299392003
    01-27 15:40:41.681 11381-11785/com.example.vish.myapplication W/dalvikvm: threadid=13: thread exiting with uncaught exception (group=0x416fad58)
    01-27 15:40:41.681 11381-11785/com.example.vish.myapplication E/AndroidRuntime: FATAL EXCEPTION: Thread-17993
    01-27 15:40:41.681 11381-11785/com.example.vish.myapplication E/AndroidRuntime: Process: com.example.vish.myapplication, PID: 11381
    01-27 15:40:41.681 11381-11785/com.example.vish.myapplication E/AndroidRuntime: java.lang.NullPointerException
    01-27 15:40:41.681 11381-11785/com.example.vish.myapplication E/AndroidRuntime: at com.example.vish.myapplication.OCRActivity1$1.run(OCRActivity1.java:227)
    01-27 15:40:41.681 11381-11785/com.example.vish.myapplication E/AndroidRuntime: at java.lang.Thread.run(Thread.java:841)
    01-27 15:40:41.911 11381-11381/com.example.vish.myapplication D/AndroidRuntime: Shutting down VM
    01-27 15:40:41.911 11381-11381/com.example.vish.myapplication W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x416fad58)
    01-27 15:40:41.911 11381-11381/com.example.vish.myapplication I/Process: Sending signal. PID: 11381 SIG: 9

    Any Solution?
    Thanks

  • http://www.dynamsoft.com Desmond Shaw

    You don’t need to explicitly load the two libraries. They are loaded in class.jar – the OCR library that built with your Android project.

  • http://www.dynamsoft.com Desmond Shaw

    You can read to http://www.codepool.biz/migrate-projects-to-android-studio.html to learn how to migrate Eclipse project to Android Studio.

  • Heera Bhadrecha

    I have problem with OCRActivity, this library used in TessBaseAPI class.
    How to load this lept and pngt library in android studio???

    System.loadLibrary(“pngt”);
    System.loadLibrary(“lept”);

  • Aris

    I think IntelliJ was a funtion on Android studio. I use android studio but not create the gradle. Im new and dont know how create my gradle. I was reading, eclipse can export the gradle about this project, and then import in android studio?

  • http://www.dynamsoft.com Desmond Shaw

    I suggest that you can switch to Android Studio, which is almost same to IntelliJ using Gradle. Android Studio is the IDE recommended by Google, and it can automatically convert eclipse projects.

  • Aris

    I rebuild and nothing. Im using IntelliJ and was reading i need a gradle export from this project right? The problem is the project is old and the structure is not the same like new project, and dont know how do it.

  • http://www.dynamsoft.com Desmond Shaw

    Have you tried to clean the project and rebuild it? Seems you are using IntelliJ. Have you tried to import the code to Eclipse or Android Studio? If still not work, create a new project and make sure you can successfully build it. Then copy the source code to the new project.

  • Aris
  • http://www.dynamsoft.com Desmond Shaw
  • Aris

    Thank you for the guide. I have a problem on manifest xml, in this line: “android:name=”com.dynamsoft.tessocr.OCRActivity”

    I have problem with OCRActivity, cannot resolve symbol “OCRActivity”, can you help me?

  • http://www.dynamsoft.com Desmond Shaw

    Any crash log?

  • Alex Appadurai

    Hi Desmond,

    Please help me…..

    public TessOCR() {

    // TODO Auto-generated constructor stub
    mTess = new TessBaseAPI();
    String datapath = Environment.getExternalStorageDirectory() + “/tesseract/”;
    String language = “eng”;
    File dir = new File(datapath + “tessdata/”);
    if (!dir.exists())
    dir.mkdirs();
    mTess.init(datapath, language);
    }

    DEBUG mode working Perfect.. but Release apk crash… ?

    I am waiting for your replay …:)

  • http://www.dynamsoft.com Desmond Shaw

    If Android project contains a jni project, the jni folder should locate as follows:

    – jni
    – *.cpp
    You just need to extract all JNI source code and copy them to your Android project with a folder named “jni”. And then use NDK to build the JNI project under .

  • http://www.dynamsoft.com/ Xiao Ling

    You can create a new module for TessOCR classes. Please refer to the post – Time to Migrate Android Projects to Android Studio http://www.codepool.biz/migrate-projects-to-android-studio.html

  • David Cooper

    Got an error message when trying to execute tar command: Cannot fork: Function not implemented. So I downloaded the jni.zip. When you say to “add all source code to your project folder” do you mean to the newly created android project? If so should I add them in a sub library folder then? I’m trying to do the tutorial in Android Studio. Thanks for any help.

  • David Cooper

    Hi, I’m using Android Studio and I’m at the stage of creating the tessOCR class. Where should I put the class file? My structure looks like the attached image. Thanks for any help.

  • http://www.dynamsoft.com/ Xiao Ling

    1. Run cmd.exe
    2. mkdir
    3. cd

  • kaaaaa

    im very new to this
    where should i enter this? cause its not working on cmd or android studio terminal

    cd
    curl -O https://tesseract-ocr.googlecode.com/files/tesseract-ocr-3.02.02.tar.gz
    curl -O http://leptonica.googlecode.com/files/leptonica-1.69.tar.gz
    tar -zxvf tesseract-ocr-3.02.02.tar.gz
    tar -zxvf leptonica-1.69.tar.gz
    rm -f tesseract-ocr-3.02.02.tar.gz
    rm -f leptonica-1.69.tar.gz
    mv tesseract-3.02.02 jni/com_googlecode_tesseract_android/src
    mv leptonica-1.69 jni/com_googlecode_leptonica_android/src
    ndk-build -j8
    android update project –target 1 –path .
    ant debug (release)

  • http://www.dynamsoft.com Desmond Shaw

    Any crash log you can share?

  • Matheus Tristão

    Hi, everytime I choose a picture or run the camera using the app from your github the APP closes. Are you sure this code is right? Thanks

  • http://www.dynamsoft.com Desmond Shaw

    You can import the ADT project by clicking File->Import Project. Or create a new Android Studio project and import the source code.

  • Bincy baby

    Where should I put the code in ANDROID STUDIO. Do I create a new folder inside project ?I need to include it in an existing project.Please help..

  • Hagar

    I have a building tess-two followed this https://coderwall.com/p/eurvaq/tesseract-with-andoird-and-gradle
    but when running this app it stops by making “Unfortunatly, TessOCR has stopped”
    what is the problem here ?

  • http://www.dynamsoft.com Desmond Shaw

    I didn’t implement the downloading function. You can check whether the data package exists. If not, pop up an alert dialog for data downloading. Once confirmed, download the package and write it to sdcard.

  • Nguyen Thanh Hung

    sorry guys. I were successfull

    But I want to ask, how to deploy this app on an other smart phone?? because I must copy the tessdata to phone card. as guidline the “Before running the Android OCR app, do not forget to download the relevantlanguage data packages and push them to your phone storage.”

  • Nguyen Thanh Hung

    it also raise eror

  • Nguyen Thanh Hung

    my issues
    I build on the ndk-rd10

  • Nguyen Thanh Hung

    my problem.
    help me??

  • http://www.dynamsoft.com Desmond Shaw

    I never tested this fork. Anyway, congratulations!

  • grantclepper

    I used NDK 9 but I got the same error. I am using the Eclipse IDE for android development. I found a fork for Tesseract Android Tools https://github.com/rmtheis/tess-two. I was able to build it successfully. Will it work with your code?

  • http://www.dynamsoft.com Desmond Shaw

    I built the source code with NDK 9 on Windows. Would you like to try out NDK 9? If it fails again, probably you have to check whether you missed some dependencies in Xcode. Have you installed Xcode command line tool?

  • grantclepper

    Also I opened the Applications.mk file which had the following lines:

    APP_STL := gnustl_static

    APP_ABI := armeabi armeabi-v7a x86 mips

    APP_OPTIM := release

    APP_CPPFLAGS += -fexceptions -frtti -Wno-error=format-security

    I changed the last line to

    APP_CFLAGS += -Wno-error=format-security

    But still got the same error.
    I don’t think I mentioned that that I am running the code on a mac

  • grantclepper

    I replaced my jni folder with yours. I got the following error message:

    jni/com_googlecode_leptonica_android/stdio/open_memstream.c:188: error: undefined reference to ‘__sfp’

    jni/com_googlecode_leptonica_android/stdio/fopencookie.c:154: error: undefined reference to ‘__sflags’

    jni/com_googlecode_leptonica_android/stdio/fopencookie.c:161: error: undefined reference to ‘__sfp’

    jni/com_googlecode_leptonica_android/stdio/fmemopen.c:209: error: undefined reference to ‘__sflags’

    jni/com_googlecode_leptonica_android/stdio/fmemopen.c:215: error: undefined reference to ‘__sfp’

    collect2: error: ld returned 1 exit status

    make: *** [obj/local/armeabi/liblept.so] Error 1

    make: *** Waiting for unfinished jobs….

    [armeabi-v7a] Compile thumb : lept <= fmorphauto.c

  • http://www.dynamsoft.com Desmond Shaw

    I have packed my jni folder. You can download http://www.codepool.biz/wp-content/uploads/2014/12/jni.zip, and have a try.

  • grantclepper

    Sorry, the image doesn’t seem to be clear. Here is the error message

    jni/com_googlecode_tesseract_android/src/ccmain/cube_reco_context.cpp:20:18: fatal error: string: No such file or directory

    #include
    ^
    compilation terminated.

    In file included from jni/com_googlecode_tesseract_android/src/cube/cube_object.h:76:0,

    from jni/com_googlecode_tesseract_android/src/ccmain/cube_control.cpp:17:
    jni/com_googlecode_tesseract_android/src/cube/char_samp.h:33:18: fatal error: string: No such file or directory

    #include
    ^
    compilation terminated.
    make: *** [obj/local/armeabi/objs/tess/src/ccmain/cube_reco_context.o] Error 1

    make: *** Waiting for unfinished jobs….
    make: *** [obj/local/armeabi/objs/tess/src/ccmain/cube_control.o] Error 1

    In file included from jni/com_googlecode_tesseract_android/src/cube/altlist.h:27:0,
    from jni/com_googlecode_tesseract_android/src/cube/char_altlist.h:28,
    from jni/com_googlecode_tesseract_android/src/ccmain/cubeclassifier.cpp:24:
    jni/com_googlecode_tesseract_android/src/cube/cube_utils.h:26:18: fatal error: vector: No such file or directory

    #include
    ^
    compilation terminated.
    make: *** [obj/local/armeabi/objs/tess/src/ccmain/cubeclassifier.o] Error 1

  • grantclepper

    I deleted all the files and restarted. The allheaders issues doesn’t seem to appear. But I’ve added a screenshot of the error message.

  • http://www.dynamsoft.com Desmond Shaw

    No, it’s fine. Your problem is there’s no allheaders.h in your project directory. Have you successfully downloaded leptonica? Please try to download it again via curl, or download it directly from its website. See my screenshot.

  • grantclepper

    or am I suppose to use v9 specifically?

  • grantclepper

    yes, I am using the latest.

  • http://www.dynamsoft.com Desmond Shaw

    Are you using NDK v10?

  • grantclepper

    I followed the steps up to

    ndk-build -j8

    The build wasn’t successful, I kept getting the following errors.

    In file included from jni/com_googlecode_leptonica_android/pixa.cpp:17:0:
    jni/com_googlecode_leptonica_android/common.h:22:24: fatal error: allheaders.h: No such file or directory
    #include
    ^
    In file included from jni/com_googlecode_leptonica_android/pix.cpp:17:0:
    jni/com_googlecode_leptonica_android/common.h:22:24: fatal error: allheaders.h: No such file or directory
    #include
    ^
    compilation terminated.
    In file included from jni/com_googlecode_leptonica_android/readfile.cpp:17:0:
    jni/com_googlecode_leptonica_android/common.h:22:24: fatal error: allheaders.h: No such file or directory
    #include
    ^
    compilation terminated.
    compilation terminated.
    In file included from jni/com_googlecode_leptonica_android/jni.cpp:17:0:
    jni/com_googlecode_leptonica_android/common.h:22:24: fatal error: allheaders.h: No such file or directory
    #include
    ^
    compilation terminated.
    In file included from jni/com_googlecode_leptonica_android/utilities.cpp:17:0:
    jni/com_googlecode_leptonica_android/common.h:22:24: fatal error: allheaders.h: No such file or directory
    #include
    ^
    compilation terminated.
    In file included from jni/com_googlecode_leptonica_android/writefile.cpp:17:0:
    jni/com_googlecode_leptonica_android/common.h:22:24: fatal error: allheaders.h: No such file or directory
    #include
    ^
    compilation terminated.
    In file included from jni/com_googlecode_leptonica_android/box.cpp:17:0:
    jni/com_googlecode_leptonica_android/common.h:22:24: fatal error: allheaders.h: No such file or directory
    #include
    ^
    compilation terminated.
    make: *** [obj/local/armeabi/objs/lept/pix.o] Error 1
    make: *** Waiting for unfinished jobs….
    make: *** [obj/local/armeabi/objs/lept/readfile.o] Error 1
    make: *** [obj/local/armeabi/objs/lept/utilities.o] Error 1
    make: *** [obj/local/armeabi/objs/lept/jni.o] Error 1
    make: *** [obj/local/armeabi/objs/lept/writefile.o] Error 1
    make: *** [obj/local/armeabi/objs/lept/pixa.o] Error 1
    make: *** [obj/local/armeabi/objs/lept/box.o] Error 1

    Also I didn’t run the following command because the path didn’t exist.
    mv tesseract-3.02.02 jni/com_googlecode_tesseract_android/src
    mv leptonica-1.69 jni/com_googlecode_leptonica_android/src

  • http://www.dynamsoft.com Desmond Shaw

    Did you follow the steps provided by Google?
    cd
    curl -O https://tesseract-ocr.googlecode.com/files/tesseract-ocr-3.02.02.tar.gz
    curl -O http://leptonica.googlecode.com/files/leptonica-1.69.tar.gz
    tar -zxvf tesseract-ocr-3.02.02.tar.gz
    tar -zxvf leptonica-1.69.tar.gz
    rm -f tesseract-ocr-3.02.02.tar.gz
    rm -f leptonica-1.69.tar.gz
    mv tesseract-3.02.02 jni/com_googlecode_tesseract_android/src
    mv leptonica-1.69 jni/com_googlecode_leptonica_android/src
    ndk-build -j8
    android update project –target 1 –path .
    ant debug (release)

  • grantclepper

    I am also having the following errors

  • grantclepper

    There are no src folders in

    jni/com_googlecode_tesseract_android/src
    jni/com_googlecode_leptonica_android/src

    Should I move the files to
    src/com/googlecode/tesseract/android
    src/com/googlecode/leptonica/android

    or should I rather move them to

    jni/com_googlecode_tesseract_android/src
    jni/com_googlecode_leptonica_android/src

    Also I keep getting the following error when I run ndk-build

    [armeabi] Compile++ thumb: lept <= box.cpp
    In file included from jni/com_googlecode_leptonica_android/box.cpp:17:0:
    jni/com_googlecode_leptonica_android/common.h:22:24: fatal error: allheaders.h: No such file or directory
    #include
    ^
    compilation terminated.
    make: *** [obj/local/armeabi/objs/lept/box.o] Error 1

  • Pandaman B&W

    Thanks! That is key!

  • http://www.dynamsoft.com Desmond Shaw

    Have you pushed tessdata (language package) to your sdcard?

  • Pandaman B&W

    Hi, I ran the project in the github and get it built on my Nexus 7. But after I took a picture, the program crashed with an error message “Could not initialize Tesseract API with language=eng!”. Would mind telling me what does it mean here and how to solve it? Thanks!

  • Isa

    I saw that somtimes it may be an issue with the traineddate file, where would I be finding that in this project?

  • http://www.dynamsoft.com Desmond Shaw

    No, you don’t. You can try another way to OCR an image. Open gallery -> choose an image -> send or share -> select the OCR app -> check the result.

  • Isa

    If the code is this https://github.com/DynamsoftRD/android-tesseract-ocr do I need to do any of the steps explained at the begining in the cmd?

  • Isa

    I put some logs and in doOCR function it crashes at getting the text(final String result = mTessOCR.getOCRResult(bitmap);) it should have log “afterResult”

  • http://www.dynamsoft.com Desmond Shaw

    Crashed in native library. Did the line “Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);” work? What are the values of bitmap width and bitmap height?

  • Isa

    I did that and it still crashes and the only thing I get is this:

  • lucy

    Help me! Where should I put https://code.gugl.com/p/teseračt-ocr/dovnloads/list in Eclipse. Do I create a new folder inside project (OCRActivity) ? thanks 😉

  • http://www.dynamsoft.com Desmond Shaw

    Please check why targetW or targetH is 0. You can hardcode the values. Set targetW = 640, targetH = 480. And run the app again to check whether it will crash.

  • Isa

    Here:

    int scaleFactor = Math.min(photoW / targetW, photoH / targetH);
    I haven’t changed anything in the code, just got it from github and ran it in eclipse.

  • http://www.dynamsoft.com Desmond Shaw

    Have you checked the logcat? Which line your app crashed at?

  • Isa

    When I run the source code posted here, after I take the picture, the app crashes saying something about divide by zero. Why is that? How can I fix it?