Hi guys I am trying to run Tesseract and get the text from an image but I encounter the following error:
Exception in thread "main" java.lang.Error: Invalid memory access
at com.sun.jna.Native.invokePointer(Native Method)
at com.sun.jna.Function.invokePointer(Function.java:477)
at com.sun.jna.Function.invoke(Function.java:411)
at com.sun.jna.Function.invoke(Function.java:323)
at com.sun.jna.Library$Handler.invoke(Library.java:236)
at com.sun.proxy.$Proxy0.TessBaseAPIGetUTF8Text(Unknown Source)
at net.sourceforge.tess4j.Tesseract.getOCRText(Tesseract.java:436)
at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:291)
at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:212)
at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:196)
at Crop_Image.main(Crop_Image.java:98)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Error opening data file ./tessdata/eng.traineddata
Please make sure the TESSDATA_PREFIX environment variable is set to the parent directory of your "tessdata" directory.
Failed loading language 'eng'
Tesseract couldn't load any languages!
I am loading an image file jpg containing english text. This is how I try to load the file and then try to get the text from it:
public static void main(String[] args){
String result = "";
File imageFile = new File("C:\\Users\\user\\Desktop\\Untitled.jpg");
Tesseract instance = new Tesseract();
try {
result = instance.doOCR(imageFile);
result.toString();
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getMessage());
}
}
Also I am also inside my project using Maven and here is my pom file:
<dependencies>
<dependency>
<groupId>nu.pattern</groupId>
<artifactId>opencv</artifactId>
<version>2.4.9-4</version>
</dependency>
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
What could be the cause of this error?
I saw your code and there might be an issue in the way you initialize Tesseract. Now since you are using maven as nguyenq suggested you need to point exactly to the location of the library - tessdata so here is what you should do:
public static String Image_To_Text(String image_path){
String result = "";
File imageFile = new File("your path to your image");
Tesseract instance = Tesseract.getInstance();
//In case you don't have your own tessdata, let it also be extracted for you
File tessDataFolder = LoadLibs.extractTessResources("tessdata");
//Set the tessdata path
instance.setDatapath(tessDataFolder.getAbsolutePath());
try {
result = instance.doOCR(imageFile);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
You need to set instance.setDatapath to the parent directory of tessdata folder.
File tessDataFolder = LoadLibs.extractTessResources("tessdata"); // Maven build bundles English data
instance.setDatapath(tessDataFolder.getParent());
See http://tess4j.sourceforge.net/tutorial.
Related
I have MainClass
public class MainClass extends Application {
#Override
public void start(Stage primaryStage) {
try{
Image img = new Image(getClass().getResourceAsStream(".\\build\\resources\\main\\img\\h1.jpg"));
System.out.println("ok");
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
}
public static void main(String[] args) {
launch(args);
}
}
and my image is in \build\resources\main\img\h1.jpg directory
my project files:
When I run project it gives Input stream must not be null exception.
Resource paths are not separated by \ Furthermore they start at the resource root. In this case the path "/img/h1.jpg" should do the trick assuming your IDE properly includes the resources in the classpath at runtime.
getResource(AsStream) does not access the data via file path; The data may not be available as file at all, but as entry in a JAR file. If you need to refer to a file that is not included in the classpath, use File's functionality to convert to a URI or use a FileInputStream:
new Image(new File(".\\build\\resources\\main\\img\\h1.jpg").toURI().toString())
I added com.googlecode.libphonenumber:libphonenumber:8.8.2 in my project. In debug mode its works normally.But in signed apk its generating the following exception when a library method is called.
Caused by: java.lang.IllegalStateException: missing metadata: /com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_BD
at com.google.i18n.phonenumbers.e.getMetadataFromSingleFileName(SourceFile:188)
at com.google.i18n.phonenumbers.e.getMetadataFromMultiFilePrefix(SourceFile:116)
at com.google.i18n.phonenumbers.g.getMetadataForRegion(SourceFile:64)
at com.google.i18n.phonenumbers.PhoneNumberUtil.getMetadataForRegion(SourceFile:2211)
at com.google.i18n.phonenumbers.PhoneNumberUtil.getMetadataForRegionOrCallingCode(SourceFile:1330)
at com.google.i18n.phonenumbers.PhoneNumberUtil.parseHelper(SourceFile:3197)
at com.google.i18n.phonenumbers.PhoneNumberUtil.parse(SourceFile:3025)
at com.google.i18n.phonenumbers.PhoneNumberUtil.parse(SourceFile:3015)
at com.revesoft.itelmobiledialer.util.aq.b(SourceFile:697)ode here
Probably you have already fixed it, but it may help others. I had the same issue and I have fixed it as the library FAQs - How do I load libphonenumber resources in my Android app?
A possible problem can be that you are loading the metadata from the main thread. If this is not the case, then
you can copy the data folder with the metadata in your app. Create an assets folder src/main/assets/data.
In your application where you first want to read the data, create your own metadata loader that will read the metadata from its new destination. This is described in the link that I posted. The library FAQs suggest to delete the metadata files from the library in order not to duplicate files.
private static PhoneNumberUtil getPhoneNumberUtilInstance()
{
if(mPhoneNumberUtil == null)
{
mPhoneNumberUtil = PhoneNumberUtil.createInstance(new MetadataLoader()
{
#Override
public InputStream loadMetadata(String metadataFileName)
{
try
{
String[] stringPieces = metadataFileName.split("/");
String metadataName = stringPieces[stringPieces.length - 1];
InputStream is = Factory.get().getApplicationContext().getAssets().open("data/" + metadataName);
return is;
}
catch (IOException e)
{
// Handle somehow!
return null;
}
}
});
}
return mPhoneNumberUtil;
}
I need to read a text stream by using StreamReader from file on android platform. File is about 100k lines, so even editor is getting stuck if i try to load it all to TextAsset or if i use WWW.
I simply need to read that file line by line without loading it all to a string. Then i'll do a tree generation from the lines that i got from the file. (But probably that part doesn't matter, i just need help on file reading part.)
I'm giving the code that i wrote down below. It works perfectly on editor, but fails on android.
I would be glad if anyone tell me, what am i missing.
(ps. english is not my native and this is my first question on the site. so sorry for the any mistakes that i may have done.)
private bool Load(string fileName)
{
try
{
string line;
string path = Application.streamingAssetsPath +"/";
StreamReader theReader = new StreamReader(path + fileName +".txt", Encoding.UTF8);
using (theReader)
{
{
line = theReader.ReadLine();
linesRead++;
if (line != null)
{
tree.AddWord(line);
}
}
while (line != null);
theReader.Close();
return true;
}
}
catch (IOException e)
{
Debug.Log("{0}\n" + e.Message);
exception = e.Message;
return false;
}
}
You can't use Application.streamingAssetsPath as a path on Android because streaming assets are stored within the JAR file with the application.
From http://docs.unity3d.com/Manual/StreamingAssets.html:
Note that on Android, the files are contained within a compressed .jar
file (which is essentially the same format as standard zip-compressed
files). This means that if you do not use Unity’s WWW class to
retrieve the file then you will need to use additional software to see
inside the .jar archive and obtain the file.
Use WWW like this in a coroutine:
WWW data = new WWW(Application.streamingAssetsPath + "/" + fileName);
yield return data;
if(string.IsNullOrEmpty(data.error))
{
content = data.text;
}
Or, if you really want to keep it simple (and your file is only a few 100k, stick it in a resource folder:
TextAsset txt = (TextAsset)Resources.Load(fileName, typeof(TextAsset));
string content = txt.text;
i am using a properties file to get the url of various webservices i am calling from my android. I want to provide a congiguration option so that ip address for web service can be modified.
how to proceed ?
i have a resource folder in src folder which have the following values
update=http://10.52.165.226:50000/android/rest/get/updateSfc
ShopOrder=http://10.52.165.226:50000/android/rest/getShopOrder/bySite?site=
i am using resource bundle to use this values in android.?
I am thinking of reading the file and replace all occrence of Ip address. how to rad the properties file and edit it in android
Here is a complete solution for you to use .properties file in your project.
1 Create a file named app.properties in assets folder of your android project
2 edit the file and write with in properties that you want to use for example as
test=success
And Save file
3 Write this Method with in your Activity Class
private Properties loadPropties() throws IOException {
String[] fileList = { "app.properties" };
Properties prop = new Properties();
for (int i = fileList.length - 1; i >= 0; i--) {
String file = fileList[i];
try {
InputStream fileStream = getAssets().open(file);
prop.load(fileStream);
fileStream.close();
} catch (FileNotFoundException e) {
Log.d(TAG, "Ignoring missing property file " + file);
}
}
return prop;
}
4 With in OnCreate Method write some thing like this
Properties prop = null;
try {
prop = loadPropties();
} catch (IOException e) {
Log.e(TAG, "Exception", e);
}
Toast.makeText(getApplicationContext(), "Result " + prop.getProperty("test"),
Toast.LENGTH_LONG).show();
5 add necessary imports
Hope this helps :)
Read about Data Storage in Android and more specifically Shared Preferences. For more complete usage of saving user preferences, read about the PreferenceActivity.
A tutorial on using Shared Preferences can be found here
Resources, Assets and other files/folders that form the part of Apk cannot be modified.You can use a database for depending on nos of rows that you will use
When trying to convert excel file to Html using org.apache.poi.ss.examples.html.ToHtml.create(...), the call crashes with exception
02-28 13:16:06.559: I/dalvikvm(8010): Could not find method org.apache.poi.ss.usermodel.WorkbookFactory.create, referenced from method org.apache.poi.ss.examples.html.ToHtml.create
String xlsxFile = "/sdcard/book1.xlsx";
String htmlFile = "/sdcard/download/out.html";
try {
FileWriter fw = new FileWriter(htmlFile);
org.apache.poi.ss.examples.html.ToHtml.create(xlsxFile, new PrintWriter(fw));
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
Looks like you don't have all the required jars on your classpath. See the POI Components Page for the list of the Jars you need. (If you downloaded a POI binary release, you'll have all of them in there, or if you use Maven it'll download them for you.)
Specifically, in your case, you're missing the poi-ooxml jar, and possibly its dependencies too - see the components page for the list of those.