We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Re: Null Pointer Exception in using OCR for Java

Hi Aashish,


Thank you for contacting Aspose support.

We may require additional information in order to prepare an equivalent environment to test your presented scenario. Please confirm the following,

  • JDK version installed on your machine
  • Aspose.OCR for Java version

Please note, the latest version of Aspose.OCR for Java is 1.9.0 (Resources), so we would suggest you to use the latest build in case you are using an older version of the API. Moreover, each release of Aspose.OCR for Java uses a specific resource file, in case you are using an incompatible resource file, you may encounter undesired results.

Dear Raza


Sorry for Delay response I have checked your reply and providing these information.

OCR version: 1.5
Resource version 1.9
JDK version : jdk1.6.0_20


Code:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import com.aspose.ocr.ILanguage;
import com.aspose.ocr.ImageStream;
import com.aspose.ocr.Language;
import com.aspose.ocr.OcrEngine;
//import com.aspose.ocr.ResourcesSource;
public class OCR {
public static String myDir = “c:/temp/”;
public static void main(String args[])
{
// Set the paths
String imagePath = “C:/Documents and Settings/aashish.kumar/Desktop/3.jpg”;
String resourcesFolderPath = “C:/Documents and Settings/aashish.kumar/Desktop/resources2.zip”;

// Create an instance of OcrEngine
OcrEngine ocr = new OcrEngine();
// Set Resources for OcrEngine
try {
long heapSize = (Runtime.getRuntime().totalMemory())/10241024;
System.out.println("–total heap size–"+heapSize);
long freeheapSize = (Runtime.getRuntime().freeMemory())/10241024;
System.out.println("—free memory-"+freeheapSize);
ocr.setResource(new FileInputStream(resourcesFolderPath));

} catch (FileNotFoundException e1) {
e1.printStackTrace();
System.out.println("----1----"+e1);
}
// Set NeedRotationCorrection property to false
ocr.getConfig().setNeedRotationCorrection(false);

// Set image file
ocr.setImage(ImageStream.fromFile(imagePath));

// Add language
ILanguage language = Language.load(“english”);
ocr.getLanguages().addLanguage(language);

// Perform OCR and get extracted text
try {
if (ocr.process()) {
System.out.println("\ranswer -> " + ocr.getText());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}


Error:
–total heap size–518979584
—free memory-516116480
Exception in thread “main” java.lang.OutOfMemoryError: Java heap space
at java.nio.HeapByteBuffer.(Unknown Source)
at java.nio.ByteBuffer.allocate(Unknown Source)
at com.aspose.internal.ocr.N.a.(Unknown Source)
at com.aspose.internal.ocr.N.a.a(Unknown Source)
at com.aspose.internal.ocr.a.H.a(Unknown Source)
at com.aspose.internal.ocr.r.D.a(Unknown Source)
at com.aspose.internal.ocr.r.D.c(Unknown Source)
at com.aspose.internal.ocr.r.D.write(Unknown Source)
at com.aspose.internal.ocr.ai.c.a(Unknown Source)
at com.aspose.internal.ocr.r.M.fromJava(Unknown Source)
at com.aspose.ocr.OcrEngine.setResource(Unknown Source)
at OCR.main(OCR.java:25)

Hi Aashish,


Thank you for providing the requested details. First of all, please download and use the latest version of Aspose.OCR for Java 1.9.0 (link shared in my previous post). Hopefully the exception will not occur again because you are currently using incompatible Jar and resource archive. In case the exception appears again, please set the Java Heap size to at least 512M. Please note, the resource file is of 86MB in size therefore you need ample memory to load the resource file as well as perform other tasks in memory.

Please keep us posted with your test results.

Dear Raza


You are right I used the necessary jar and increase the heap size but there is issue in accuracy the file read

Code:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import com.aspose.ocr.ILanguage;
import com.aspose.ocr.ImageStream;
import com.aspose.ocr.Language;
import com.aspose.ocr.OcrEngine;
//import com.aspose.ocr.ResourcesSource;
public class OCR {
public static String myDir = “c:/temp/”;
public static void main(String args[])
{
// Set the paths
String imagePath = “C:/Documents and Settings/aashish.kumar/Desktop/3.jpg”;
String resourcesFolderPath = “C:/Documents and Settings/aashish.kumar/Desktop/resources3.zip”;

// Create an instance of OcrEngine
OcrEngine ocr = new OcrEngine();
// Set Resources for OcrEngine
try {
long heapSize = (Runtime.getRuntime().totalMemory())/10241024;
System.out.println("–total heap size–"+heapSize);
long freeheapSize = (Runtime.getRuntime().freeMemory())/10241024;
System.out.println("—free memory-"+freeheapSize);
ocr.setResource(new FileInputStream(resourcesFolderPath));

} catch (FileNotFoundException e1) {
e1.printStackTrace();
System.out.println("----1----"+e1);
}
// Set NeedRotationCorrection property to false
ocr.getConfig().setNeedRotationCorrection(false);

// Set image file
ocr.setImage(ImageStream.fromFile(imagePath));

// Add language
ILanguage language = Language.load(“english”);
ocr.getLanguages().addLanguage(language);

// Perform OCR and get extracted text
try {
if (ocr.process()) {
System.out.println("\ranswer -> " + ocr.getText());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Out put is

–total heap size–518979584
—free memory-507528192


answer -> ^rv ecs
yTR !Oodhzu Lbuama<tw7 Rrwtm7kimwI v tM

Please help me to make OCR of that file

The file 3.jpg is attached in attachment.


Hi Aashish,


Thank you for providing your sample image.

We are able to observe the problem with your provided image. We have noticed that the latest version of Aspose.OCR for Java 1.9.0 is unable to correctly recognize the text. The aforesaid problem has been logged in out bug tracking system under the ticket Id OCR-33791. Please spare us little time to properly analyze the problem cause on our end. In the meanwhile, we will keep you posted with updates in this regard.

Please accept our sincere apologies for the inconvenience caused to you.

Dear Raza


I am desperately waiting for your reply, for the above given problem, The Image is not correctly read by OCR As I mention Earlier I used the necessary jar and increase the heap size.

Please give me response as early as possible

Regards

Aashish Kumar

Hi Aashish,


Please note, the Java Heap size has to be increased in order to avoid the OutOfMemoryException. However, increasing the Java Heap does not have any effect on the accuracy of the recognized text. The accuracy of the recognized data relies on the algorithms used for the OcrEngine, and the ticket OCR-33791 has been logged in the same reference. Unfortunately, we haven’t yet received any updates regarding the aforesaid ticket, therefore we have recorded a note for the concerned development team member to share the insight of this issue and possibly an estimated release schedule for the fix. As soon as we receive updates in this regard, we will post here for your kind reference.

Thank you for your patience and cooperation.