android tesseract ocr force close - android

I am developing an app that using tesseract android library functions. I succesfully compile the tesseract library and imported to my workspace. then I add the library reference to my app.But this method that I wrote causes the force close:
public static String BitmapOku (Bitmap image){
Bitmap image2=image.copy(Bitmap.Config.ARGB_8888, true);
TessBaseAPI baseApi = new TessBaseAPI();
baseApi.init("/mnt/sdcard/tessdata/eng.traineddata", "eng");
baseApi.setImage(image2);
String recognizedText = baseApi.getUTF8Text();
baseApi.end();
return recognizedText;
my question is---> what issues do I have to consider with a tesseract android ocr app and do I have to use leptonica tool in my app for image processing functios -for example rotating- (because I use Opencv 2.4.0)?

sometimes , even if the language file is inserted in the sdcard, there is a possibility that it wont be detected properly . So for efficiently establishing the file in the sdcard , following code may be useful.
String dirc= Environment.getExternalStorageDirectory().getPath().toString();
String[] paths = new String[] { dirc,dirc + "/tessdata/" };
for (String path :paths ) {
File dir = new File(path);
if (!dir.exists()) {
if (!dir.mkdirs()) {
System.out.println("Creation of directory on sdcard failed");
} else {
System.out.println("Creation of directory on sdcard successful");
}
}
}
// lang.traineddata file with the app (in assets folder)
// You can get them at:
// http://code.google.com/p/tesseract-ocr/downloads/list
// This area needs work and optimization
if (!(new File(dirc + "/tessdata/" + "eng.traineddata")).exists()) {
try {
AssetManager assetManager = getAssets();
InputStream in = assetManager.open("tessdata/eng.traineddata");
//GZIPInputStream gin = new GZIPInputStream(in);
OutputStream out = new FileOutputStream(dirc + "/tessdata/eng.traineddata");
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
//while ((lenf = gin.read(buff)) > 0) {
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
//gin.close();
out.close();
System.out.println("copied eng.traineddata");;
} catch (IOException e) {
e.printStackTrace();
}
}
here , the language file eng.traineddata is added to the assets folder in the project .
in assets folder, create a tessdata folder and put eng.traineddata in the folder ..
hope this solves your problem.

Related

Solution for updating Assets folder

i am making a quiz app and i have stored images in assets folder say assets/pictures.I retrieve images from assets folder using AssetsManager.But after i add few more images to assets folder i found that the application do not fetch updated image from assets folder unless i uninstall the previous app.I also learned that its isn't possible to update assets folder unless i uninstall old app.Am i correct? I would like to know if its possible to added assets folder images to a database and use database to retrieve images to app?Also will my new image files will be shown when i release an updated version of my app?If so how?
This is what i use to rad from assets folder
String[] getImagesFromAssets() {
AssetManager assetManager = getAssets();
String[] img_files = null;
try {
// img_files = getAssets().list("pictures");
img_files = assetManager.list("pictures");
} catch (IOException ex) {
Logger.getLogger(GameActivity.class
.getName()).log(Level.SEVERE, null, ex);
}
return img_files;
}
void loadImage(String name) {
AssetManager assetManager = getAssets();
System.out.println("File name => "+name);
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open("pictures/"+name); // if files resides inside the "Files" directory itself
out = new FileOutputStream(Environment.getExternalStorageDirectory() +"/" +"pictures/"+ name);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
Drawable d=Drawable.createFromPath(Environment.getExternalStorageDirectory() +"/" +"pictures/" + name);
image.setImageDrawable(d);
//Drawable d = Drawable.createFromStream(in, null);
//image.setImageDrawable(d);
} catch(Exception e) {
Log.e("tag", e.getMessage());
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
The Assets folder is a build-time resource that is read-only for the Application. Once the files are specified in the APK, they cannot be changed at run time. You will need to use local storage to work with new files that your application creates.

File copying: what am I doing wrong?

Sorry,
I have no experience with the Android file system, I am struggling to understand it via the documentation and the tutorials.
I am trying to copy a file from a location to the external storage of my app.
final File filetobecopied =item.getFile();
File path=getPrivateExternalStorageDir(mContext);
final File destination = new File(path,item.getName());
try
{copy(filetobecopied,destination);
}
catch (IOException e) {Log.e("",e.toString());}
public void copy(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
Toast.makeText(mContext,"COPIED",Toast.LENGTH_SHORT).show();
}
public File getPrivateExternalStorageDir(Context context) {
File file = context.getExternalFilesDir(null);
if (!file.mkdirs()) {
Log.e("", "Directory not created");
}
return file;
}
I get the following error:
09-18 10:14:04.260: E/(7089): java.io.FileNotFoundException: /storage/emulated/0/Android/data/org.openintents.filemanager/files/2013-08-24 13.18.14.jpg: open failed: EISDIR (Is a directory)
http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String) use this example of code and use standart examples of google)
Try
final File destination = new File(path + "/" + item.getName());
instead.
I assume, folder directory is not created or your external storage state is not mounted.
Before you perform file operations, you should sanitize your path. Following code is a sample code that I often use.
public File sanitizePath(Context context) {
String state = android.os.Environment.getExternalStorageState();
File folder = null;
if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
folder = new File(context.getFilesDir() + path);
// path is the desired location that must be specified in your code.
}else{
folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + path);
}
if (!folder.exists()){
folder.mkdirs();
}
return folder;
}
And be sure about that, when your directory has to be created before you perform file operations.
Hope this will help.
EDIT: By the way, you have to add following permission to your manifest.xml file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Tesseract OCR Android tessdata directory not found

I'm currently developing an Android app using OCR and I've reached the point where I'm calling the BaseAPI.init() method. I keep getting errors stating that the directory must contain tessdata as a subfolder. I've checked that the file directory contains the folder with the trainingdata file inside, and made sure I'm pointing to the right directory. I would really like to fix this.
The directory i'm pointing to is /mnt/sdcard/Image2Text/ . I've made sure that tessdata is a subfolder with the necessary language file inside.
Here is the code:
public static final String DATA_PATH = Environment.getExternalStorageDirectory().toString() +
"/Image2Text/";
....
File dir = new File(DATA_PATH + "tessdata");
dir.mkdirs();
if (!(new File(DATA_PATH + "tessdata/" + lang + ".traineddata")).exists()) {
try {
AssetManager assetManager = getAssets();
InputStream in = assetManager.open("eng.traineddata");
OutputStream out = new FileOutputStream(DATA_PATH
+ "tessdata/eng.traineddata");
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
} catch (IOException e) {}
}
TessBaseAPI baseAPI = new TessBaseAPI();
baseAPI.init(DATA_PATH, lang);
baseAPI.setImage(new File(path));
Like you say, the DATA_PATH directory must contain tessdata as a subfolder. So, if your tessdata folder was /data/data/tessdata, DATA_PATH would be /data/data
I hope that this helps!
EDIT: ak, I think I missunderstood!

Copying file from a Samba drive to an Android sdcard directory

I am new to Android and Samba. I am trying to use the JCIFS copy. To method to copy a file from a Samba directory to the 'Download' directory under sdcard on an Android 3.1 device. Following is my code:
from = new SmbFile("smb://username:password#a.b.c.d/sandbox/sambatosdcard.txt");
File root = Environment.getExternalStorageDirectory();
File sourceFile = new File(root + "/Download", "SambaCopy.txt");
to = new SmbFile(sourceFile.getAbsolutePath());
from.copyTo(to);
I am getting a MalformedURLException on the 'to' file. Is there a way to get around this problem using the copyTo method, or is there an alternate way to copy a file from the samba folder to the sdcard folder using JCIFS or any other way? Thanks.
The SmbFile's copyTo() method lets you copy files from network to network. To copy files between your local device and the network you need to use streams. E.g.:
try {
SmbFile source =
new SmbFile("smb://username:password#a.b.c.d/sandbox/sambatosdcard.txt");
File destination =
new File(Environment.DIRECTORY_DOWNLOADS, "SambaCopy.txt");
InputStream in = source.getInputStream();
OutputStream out = new FileOutputStream(destination);
// Copy the bits from Instream to Outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Maybe in.close();
out.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Android: How to create a directory on the SD Card and copy files from /res/raw to it?

I am trying to create a folder and several subdirectory within it on the SD Card... I then want to transfer files that I have stored in /res/raw to that folder... I addition, I want this to only happen once, the first time the program is ever run. I realize that this is ridiculously open-ended, and that I am asking a lot... but any help would be greatly appreciated.
This will copy all files in the "clipart" subfolder of the .apk assets folder to the "clipart" subfolder of your app's folder on the SD card:
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
String basepath = extStorageDirectory + "/name of your app folder on the SD card";
//...
// in onCreate
File clipartdir = new File(basepath + "/clipart/");
if (!clipartdir.exists()) {
clipartdir.mkdirs();
copyClipart();
}
private void copyClipart() {
AssetManager assetManager = getResources().getAssets();
String[] files = null;
try {
files = assetManager.list("clipart");
} catch (Exception e) {
Log.e("read clipart ERROR", e.toString());
e.printStackTrace();
}
for(int i=0; i<files.length; i++) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open("clipart/" + files[i]);
out = new FileOutputStream(basepath + "/clipart/" + files[i]);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(Exception e) {
Log.e("copy clipart ERROR", e.toString());
e.printStackTrace();
}
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
I experienced a similar problem when using mkdirs(), however because running the command:
mkdir one/two
fails on Linux, then the method http://download.oracle.com/javase/1.4.2/docs/api/java/io/File.html#mkdirs() subsequently fails too. I guess this means there is no way to use mkdirs on Android? My (probably rather hacky) work-around was to create each necessary directory separately:
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
new File(extStorageDirectory + "/one/").mkdirs();
new File(extStorageDirectory + "/one/two/).mkdirs();

Categories

Resources