Skip to content

Commit

Permalink
fix bad language files
Browse files Browse the repository at this point in the history
  • Loading branch information
renard314 committed Mar 17, 2014
1 parent ea94f31 commit cd3137b
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 25 deletions.
20 changes: 18 additions & 2 deletions textfairy/src/com/renard/ocr/BaseDocumentActivitiy.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,13 @@ protected void startCamera() {
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = null;
try {
image = File.createTempFile(imageFileName, ".jpg", storageDir);
if (!storageDir.exists()){
storageDir.mkdirs();
}
image = new File(storageDir,imageFileName + ".jpg");
if (image.exists()){
image.createNewFile();
}
cameraPicUri = Uri.fromFile(image);
intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraPicUri);
startActivityForResult(intent, REQUEST_CODE_MAKE_PHOTO);
Expand Down Expand Up @@ -212,6 +218,17 @@ private void onTakePhotoActivityResult(int requestCode, int resultCode, Intent i
if (requestCode == REQUEST_CODE_MAKE_PHOTO) {
Cursor myCursor = null;
Date dateOfPicture = null;
//check if there is a file at the uri we specified
if (cameraPicUri!=null){
File f = new File(cameraPicUri.getPath());
if (f.isFile() && f.exists() && f.canRead()){
//all is well
loadBitmapFromContentUri(cameraPicUri);
return;
}

}
//try to look up the image by querying the media content provider
try {
// Create a Cursor to obtain the file Path for the large
// image
Expand Down Expand Up @@ -329,7 +346,6 @@ protected Pair<Pix, PixLoadStatus> doInBackground(Void... params) {
return Pair.create(null, PixLoadStatus.IMAGE_DOES_NOT_EXIST);
}
} else if (cameraPicUri.toString().startsWith("content")) {
//TODO test
InputStream stream = getContentResolver().openInputStream(cameraPicUri);
p = ReadFile.readMem(Util.toByteArray(stream));
if (p==null){
Expand Down
11 changes: 8 additions & 3 deletions textfairy/src/com/renard/ocr/LayoutQuestionDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,14 @@ public static AlertDialog createDialog(final Context context,
final LayoutChoseListener listener) {

mLayout = LayoutKind.SIMPLE;
Pair<String, String> language = PreferencesUtils
.getOCRLanguage(context);
mLanguage = language.first;
Pair<String, String> language = PreferencesUtils.getOCRLanguage(context);

if (!OCRLanguageActivity.isLanguageInstalled(language.first,context)){
final String defaultLanguage = context.getString(R.string.default_ocr_language);
final String defaultLanguageDisplay = context.getString(R.string.default_ocr_display_language);
language = Pair.create(defaultLanguage,defaultLanguageDisplay);
}
mLanguage = language.first;

AlertDialog.Builder builder;

Expand Down
25 changes: 13 additions & 12 deletions textfairy/src/com/renard/ocr/OCRActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,33 +232,34 @@ private void saveDocument(final Pix pix, final String hocrString, final String u
public void run() {
File imageFile = null;
Uri documentUri = null;
try {
if (checkSd) {
imageFile = saveImage(pix);
}

documentUri = saveDocumentToDB(imageFile, hocrString, utf8String);
Util.createThumbnail(OCRActivity.this, imageFile, Integer.valueOf(documentUri.getLastPathSegment()));
} catch (RemoteException e) {
e.printStackTrace();

try {
imageFile = saveImage(pix);
} catch (IOException ignore) {
runOnUiThread(new Runnable() {

@Override
public void run() {
Toast.makeText(getApplicationContext(), getText(R.string.error_create_file), Toast.LENGTH_LONG).show();
}
});
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}

try {

documentUri = saveDocumentToDB(imageFile, hocrString, utf8String);
Util.createThumbnail(OCRActivity.this, imageFile, Integer.valueOf(documentUri.getLastPathSegment()));
} catch (RemoteException e) {
e.printStackTrace();

runOnUiThread(new Runnable() {

@Override
public void run() {
Toast.makeText(getApplicationContext(), getText(R.string.error_create_file), Toast.LENGTH_LONG).show();
}
});
} finally {
} finally {
if (pix != null) {
pix.recycle();
}
Expand Down
36 changes: 29 additions & 7 deletions textfairy/src/com/renard/ocr/help/OCRLanguageActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.renard.util.Util;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -201,28 +202,49 @@ private OCRLanguageAdapter initLanguageList() {
return adapter;
}

public static boolean isLanguageInstalled(final String ocrLang,Context appContext){
final File tessDir = Util.getTrainingDataDir(appContext);
if (!tessDir.exists()) {
return false;
}
final File[] languageFiles = tessDir.listFiles(new FileFilter() {


@Override
public boolean accept(File pathname) {
if (pathname.getName().equalsIgnoreCase(ocrLang + ".traineddata") && pathname.isFile()) {
return true;
}
return false;
}
});

return languageFiles!=null && languageFiles.length>=1;

}

private static final List<Pair<String, Long>> getInstalledLanguages(Context appContext) {
final List<Pair<String, Long>> result = new ArrayList<Pair<String, Long>>();
final File tessDir = Util.getTrainingDataDir(appContext);
if (!tessDir.exists()) {
return result;
}
final String[] languageFiles = tessDir.list(new FilenameFilter() {
final File[] languageFiles = tessDir.listFiles(new FileFilter() {


@Override
public boolean accept(File dir, String filename) {
if (filename.endsWith(".traineddata")) {
public boolean accept(File pathname) {
if (pathname.getName().endsWith(".traineddata") && pathname.isFile()) {
return true;
}
return false;
}
});

for (final String val : languageFiles) {
final int dotIndex = val.indexOf('.');
for (final File val : languageFiles) {
final int dotIndex = val.getName().indexOf('.');
if (dotIndex > -1) {
File f = new File(tessDir, val);
result.add(Pair.create(val.substring(0, dotIndex), f.length()));
result.add(Pair.create(val.getName().substring(0, dotIndex), val.length()));
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,13 @@ protected void onHandleIntent(Intent intent) {
}
if (entry != null) {
File tessDir = Util.getTrainingDataDir(this);
tessDir.mkdirs();
final String currentLangName = entry.getName().substring("tesseract-ocr/tessdata/".length());
File trainedData = new File(tessDir, currentLangName);
trainedData.mkdirs();
if (trainedData.isDirectory()){
trainedData.delete();
}
tessDir.mkdirs();
FileOutputStream fout = new FileOutputStream(trainedData);
int len;
while ((len = tarIn.read(buffer)) != -1) {
Expand Down

0 comments on commit cd3137b

Please sign in to comment.