Java Tesseract dyld: Symbol not found: - java

im trying to do simple OCR application but i get errors like this:
Code:
import org.bytedeco.javacpp.*;
import static org.bytedeco.javacpp.lept.*;
import static org.bytedeco.javacpp.tesseract.*;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.UnsupportedEncodingException;
public class Main {
public void tesseract(String filename){
BytePointer outText;
tesseract.TessBaseAPI api = new tesseract.TessBaseAPI();
// Initialize tesseract-ocr with English, without specifying tessdata path
if (api.Init("/Users/Marcel/tesseract-ocr", "ENG") != 0) {
System.err.println("Could not initialize tesseract.");
System.exit(1);
}
// Open input image with leptonica library
PIX image = pixRead(filename);
api.SetImage(image);
// Get OCR result
outText = api.GetUTF8Text();
String string = outText.getString();
assertTrue(!string.isEmpty());
System.out.println("OCR output:\n" + string);
// Destroy used object and release memory
api.End();
outText.deallocate();
pixDestroy(image);
}
public static void main(String[] args) {
Main main = new Main();
String fileName = "src/main/resources/test.png";
main.tesseract(fileName);
}
}
I have tried some solutions from google, but it didnt solve this. I using Inteliji (with maven) on Mac OSX. Before i had problem with TESTDATA_PREFIX, but i changed api.init first parameter and now i get this:
dyld: lazy symbol binding failed: Symbol not found: __ZN9tesseract11TessBaseAPI8SetImageEPK3Pix
Referenced from: /private/var/folders/lq/3mb8s_jj1ql0klqzznm1j1tm0000gn/T/javacpp33697284992581/libjnitesseract.dylib
Expected in: /usr/local/lib/libtesseract.3.dylib
dyld: Symbol not found: __ZN9tesseract11TessBaseAPI8SetImageEPK3Pix
Referenced from: /private/var/folders/lq/3mb8s_jj1ql0klqzznm1j1tm0000gn/T/javacpp33697284992581/libjnitesseract.dylib
Expected in: /usr/local/lib/libtesseract.3.dylib

This is because of 2 conflicting libtesseract.3.dylib.
If you have installed tesseract on mac using brew then it is referring to the libtesseract.3.dylib inside tesseract folder /usr/local/Cellar/tesseract/3.04.01_1/lib/
instead of /usr/local/lib/libtesseract.3.dylib

Related

Java - CodeName One: not finding symbole fileInput/Output

I'm trying to copy an image with java in a codename One project , this is the code which gives correct copy of the image:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.mycompany.gui;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
*
* #author Emel
*/
public class NewMain {
/**
* #param args the command line arguments
*/
public static void main(String[] args)
throws FileNotFoundException, IOException
{
// TODO code application logic here
InputStream is = null;
OutputStream os = null;
is = new FileInputStream(new File("C:/Users/Emel/AppData/Local/Temp/temp8255862222083205111..png"));
os = new FileOutputStream(new File("C:/wamp64/www/PiWeb1/TeamFlags/mpmppp.png"));
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
}
}
the code works perfectly when i put it in a main java class (it only works when i run the mainclass ) but when i build the project the build fails and the output show this error :
`C:\Users\Emel\Documents\NetBeansProjects\PIMOBILE\Mobile\src\com\mycompany\gui\NewMain.java:8:
error: cannot find symbol import java.io.File; symbol: class File
location: package java.io
C:\Users\Emel\Documents\NetBeansProjects\PIMOBILE\Mobile\src\com\mycompany\gui\NewMain.java:9:
error: cannot find symbol import java.io.FileInputStream; symbol:
class FileInputStream location: package java.io
C:\Users\Emel\Documents\NetBeansProjects\PIMOBILE\Mobile\src\com\mycompany\gui\NewMain.java:10:
error: cannot find symbol import java.io.FileNotFoundException;
symbol: class FileNotFoundException location: package java.io
C:\Users\Emel\Documents\NetBeansProjects\PIMOBILE\Mobile\src\com\mycompany\gui\NewMain.java:11:
error: cannot find symbol import java.io.FileOutputStream; symbol:
class FileOutputStream location: package java.io
C:\Users\Emel\Documents\NetBeansProjects\PIMOBILE\Mobile\src\com\mycompany\gui\NewMain.java:25:
error: cannot find symbol
throws FileNotFoundException, IOException symbol: class FileNotFoundException location: class NewMain
C:\Users\Emel\Documents\NetBeansProjects\PIMOBILE\Mobile\src\com\mycompagny\Service\ServiceEquipe.java:38:
error: cannot find symbol
is = new FileInputStream("C:/Users/Emel/AppData/Local/Temp/temp8255862222083205111..png");
symbol: class FileInputStream location: class ServiceEquipe
C:\Users\Emel\Documents\NetBeansProjects\PIMOBILE\Mobile\src\com\mycompagny\Service\ServiceEquipe.java:39:
error: cannot find symbol
os = new FileOutputStream( symbol: class FileOutputStream location: class ServiceEquipe
C:\Users\Emel\Documents\NetBeansProjects\PIMOBILE\Mobile\src\com\mycompany\gui\NewMain.java:31:
error: cannot find symbol
is = new FileInputStream(new File("C:/Users/Emel/AppData/Local/Temp/temp8255862222083205111..png"));
symbol: class FileInputStream location: class NewMain
C:\Users\Emel\Documents\NetBeansProjects\PIMOBILE\Mobile\src\com\mycompany\gui\NewMain.java:31:
error: cannot find symbol
is = new FileInputStream(new File("C:/Users/Emel/AppData/Local/Temp/temp8255862222083205111..png"));
symbol: class File location: class NewMain
C:\Users\Emel\Documents\NetBeansProjects\PIMOBILE\Mobile\src\com\mycompany\gui\NewMain.java:32:
error: cannot find symbol
os = new FileOutputStream(new File("C:/wamp64/www/PiWeb1/TeamFlags/mpmppp.png")); symbol: class
FileOutputStream location: class NewMain
C:\Users\Emel\Documents\NetBeansProjects\PIMOBILE\Mobile\src\com\mycompany\gui\NewMain.java:32:
error: cannot find symbol
os = new FileOutputStream(new File("C:/wamp64/www/PiWeb1/TeamFlags/mpmppp.png")); symbol: class
File location: class NewMain Note:
C:\Users\Emel\Documents\NetBeansProjects\PIMOBILE\Mobile\src\com\mycompagny\Service\ServiceEquipe.java
uses or overrides a deprecated API. Note: Recompile with
-Xlint:deprecation for details. Note: C:\Users\Emel\Documents\NetBeansProjects\PIMOBILE\Mobile\src\com\mycompagny\Service\ServiceEquipe.java
uses unchecked or unsafe operations. Note: Recompile with
-Xlint:unchecked for details. 11 errors
`
Now i need to put that code block into a method to recall it, i tried to do that in different ways but i failed it only works when i use it in a main class.
PS1: when i delete that main class from my project the build succeed.
PS2: the solution works perfectly in a normal java project so i think the problem is due to codename One.
Im using netbeans.
java.io.File, java.io.FileInputStream, java.io.FileNotFoundException &
java.io.FileOutputStream don't exist in Codename One.
There is a long explanation here.
There are several reasons but these specific API's won't act correctly on a mobile device where your app needs to run in isolation from other processes and has limited/restricted access. E.g. in your case it's pretty obvious the C: path won't exist on Android or iOS both of which are Unix derivatives (Linux/BSD).
You need to use FileSystemStorage or Storage there is a section about those in the developer guide.

Even though I added .jar of JAI to Eclipse buildpath , getting "JAI cannot be resolved" error

I'm new to java.
I am trying to use Java Advanced Imaging to read an Image file as explained in here http://www.oracle.com/technetwork/java/iio-141084.html
import java.awt.image.RenderedImage;
import javax.media.jai.JAI;
public class ImageGetterJAI {
public static void main(String[] args)
{
//image source
String imagedir = "C:\\Users\\Emre\\Desktop\\Image\\Grass.tif";
//get the image
RenderedImage image = JAI.create("imageload", imagedir);
}
}
But I get JAI cannot be resolved error. Am I doing a fundamental mistake.
Screenshot of the project is below, hope this helps.

How to get text of a toaster message in mobile app using Appium

I am trying to verify a toaster message in Android Mobile app but not able to get text of toaster message as it doesn't show in uiautomatorviewer.
Got some information that by the help of OCR it can be done taking screenshots and fetching the text from that screenshots
Can anyone help me out how to do this step by step using java in Appium project?
You can follow the information on the below links to install the Tesseract on your machine:
For Mac: http://emop.tamu.edu/Installing-Tesseract-Mac
For Windows: http://emop.tamu.edu/Installing-Tesseract-Windows8
After installing the TessEract on your machine you need to add the dependency of TessEract Java library in your project. If you are using Maven for it, adding below dependency will work:
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>tesseract</artifactId>
<version>3.04-1.1</version>
</dependency>
Also the 'Step 3' which is mentioned by Ivan need not to be followed.
If you are using 'TestNG' the TessEract API needs to be initialised only once so instead of initialising it every time, as per your framework you can initialise it either in the 'BeforeTest' or 'BeforeSuite' or 'BeforeClass' method and accordingly close the API either in 'AfterTest' or 'AfterSuite' or 'AfterClass' method.
Below is the code that I have written to achieve it.
import static org.bytedeco.javacpp.lept.pixDestroy;
import static org.bytedeco.javacpp.lept.pixRead;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.bytedeco.javacpp.lept.PIX;
import org.bytedeco.javacpp.tesseract.TessBaseAPI;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
public class BaseTest {
static TessBaseAPI api = new TessBaseAPI();
#BeforeSuite
public void beforeSuit() throws IOException {
File screenshotsDirec = new File("target/screenshots");
if (screenshotsDirec.exists())
FileUtils.forceDelete(screenshotsDirec);
FileUtils.forceMkdir(screenshotsDirec);
System.out.println("Initializing TessEract library");
if (api.Init("/opt/local/share", "eng") != 0) {
System.err.println("Could not initialize tesseract.");
}
}
public synchronized boolean verifyToastMessage(String msg)
throws IOException {
TakesScreenshot takeScreenshot = ((TakesScreenshot) driver);
File[] screenshots = new File[5];
for (int i = 0; i < screenshots.length; i++) {
screenshots[i] = takeScreenshot.getScreenshotAs(OutputType.FILE);
}
String outText;
Boolean isMsgContains = false;
for (int i = 0; i < screenshots.length; i++) {
PIX image = pixRead(screenshots[i].getAbsolutePath());
api.SetImage(image);
outText = api.GetUTF8Text().getString().replaceAll("\\s", "");
System.out.println(outText);
isMsgContains = outText.contains(msg);
pixDestroy(image);
if (isMsgContains) {
break;
}
}
return isMsgContains;
}
#AfterSuite()
public void afterTest() {
try {
api.close();
} catch (Exception e) {
api.End();
e.printStackTrace();
}
}
}
I would also like to add that writing tests to read and verify the Toast messages in this way is not very much reliable as in one of my tests this code successfully captures the Toast message while in another test it fails to capture the toast message because the capturing of the screenshots starts when the toast message disappears. That was the reason I tried to write this code very much efficiently. However that also does not serve the purpose.
Follow this discussion on Appium forum: https://discuss.appium.io/t/verifying-toast/3676.
Basic steps to verify a Toaster are:
Perform action to trigger the toast message to appear on screen
Take x number of screenshots
Increase resolutions of all screenshots
Use tessearct OCR to detect the toast message.
Refer this repo to use Java OCR library (see at the bottom):
import org.bytedeco.javacpp.*;
import static org.bytedeco.javacpp.lept.*;
import static org.bytedeco.javacpp.tesseract.*;
public class BasicExample {
public static void main(String[] args) {
BytePointer outText;
TessBaseAPI api = new TessBaseAPI();
// Initialize tesseract-ocr with English, without specifying tessdata path
if (api.Init(null, "eng") != 0) {
System.err.println("Could not initialize tesseract.");
System.exit(1);
}
// Open input image with leptonica library
PIX image = pixRead(args.length > 0 ? args[0] : "/usr/src/tesseract/testing/phototest.tif");
api.SetImage(image);
// Get OCR result
outText = api.GetUTF8Text();
System.out.println("OCR output:\n" + outText.getString());
// Destroy used object and release memory
api.End();
outText.deallocate();
pixDestroy(image);
}
}

Loading image for JAI API

I am trying to load an image from my computer into a code to produce a color histogram. My code is compiling but it says that the image is not found, although it is on the Home part of my laptop as 'me.jpg.' Below is the first part of my code, Any tips?
import java.io.*;
import java.awt.image.renderable.ParameterBlock;
import javax.media.jai.*;
public class test {
public test() {
}
public static void main(String[] args) {
PlanarImage image = JAI.create("fileload", "me.jpg"); // Load Image
int [][] imageHistogram = getHistogram(image);
FileWriter writer = null;
File outputFile = new File("test2.txt");
I recommend you store your code and your data (images) in different, proper, places.
Then, open the terminal and set the data directory as the current directory. And invoke the JVM specifying the code directry into the classpath:
java -classpath <directory-of-code> my.class <parameters...>
Update
Also, you could pass the absolute path as a parameter and receive it in your code:
public static void main(String[] args) {
PlanarImage image = JAI.create("fileload", args[0]);
...
And the command line:
java -classpath <directory-of-code> my.class my-home/me.jpg

Crawljax - Require jars file for dynamic webpage crawling

I am trying to crawl the javascript webpages(content present within IFrame html tag) using Crawljax. I have added slf4j, crawljax 2.1 and Guava 18.0 jar to the application.
Error Message displayed in popup:
cannot find symbol
import com.crawljax.core.configuration.CrawljaxConfiguration.CrawljaxConfigurationBuild‌​er;
symbol: class CrawljaxConfigurationBuilder
location: class CrawljaxConfiguration.
Code:
import com.crawljax.core.CrawlerContext;
import com.crawljax.core.CrawljaxRunner;
import com.crawljax.core.configuration.CrawljaxConfiguration;
import com.crawljax.core.configuration.CrawljaxConfiguration.CrawljaxConfigurationBuilder;
import com.crawljax.core.plugin.OnNewStatePlugin;
import com.crawljax.core.state.StateVertex;
public class CrawljaxExamples {
public static void main(String[] args) {
CrawljaxConfigurationBuilder builder
= CrawljaxConfiguration.builderFor("http://help.syncfusion.com/ug/wpf/default.htm#!documents/overview.htm");
builder.addPlugin(new OnNewStatePlugin() {
#Override
public void onNewState(CrawlerContext context, StateVertex newState) {
}
#Override
public String toString() {
return "Our example plugin";
}
});
CrawljaxRunner crawljax = new CrawljaxRunner(builder.build());
crawljax.call();
}
}
Error Message:
java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: class CrawljaxConfigurationBuilder
location: class com.crawljax.core.configuration.CrawljaxConfiguration
at crawljaxexamples.CrawljaxExamples.<clinit>(CrawljaxExamples.java:12)
Exception in thread "main" Java Result: 1
Same code could be found in below Link,
https://github.com/crawljax/crawljax/blob/master/examples/src/main/java/com/crawljax/examples/PluginExample.java
Can someone please tell what are jars files required to run this program? Or is there any settings to be changed in IDE?
Thanks
It seems you are using old version of crawljax.
Download latest version crawljax-cli-3.5.1.zip
Add all jars from lib folder and crawljax-cli-3.5.1.jar from the main folder as lib path.
Tested and now it works well.

Categories

Resources