Skip to content

Commit

Permalink
Merge pull request #89 from p-davide/master
Browse files Browse the repository at this point in the history
Fixed #88 and removed unnecessary preference
  • Loading branch information
SubhamTyagi authored Jan 23, 2024
2 parents d599c8b + 582527d commit 67f6580
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 201 deletions.
32 changes: 32 additions & 0 deletions app/src/main/java/io/github/subhamtyagi/ocr/Language.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.github.subhamtyagi.ocr;

import android.content.Context;

import androidx.annotation.NonNull;

public class Language {
public String getCode() {
return code;
}

public String getName() {
return name;
}

private String code, name;
public Language(@NonNull Context c, @NonNull String seed) {
String[] languagesNames = c.getResources().getStringArray(R.array.ocr_engine_language);
String[] languagesCodes = c.getResources().getStringArray(R.array.key_ocr_engine_language_value);
for (int i = 0; i < languagesCodes.length; i++) {
if (languagesCodes[i].equals(seed) || languagesNames[i].equals(seed)) {
this.code = languagesCodes[i];
this.name = languagesNames[i];
}
}
}

@NonNull
@Override public String toString() {
return getCode();
}
}
191 changes: 85 additions & 106 deletions app/src/main/java/io/github/subhamtyagi/ocr/MainActivity.java

Large diffs are not rendered by default.

30 changes: 0 additions & 30 deletions app/src/main/java/io/github/subhamtyagi/ocr/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@
import androidx.preference.SwitchPreference;


/**
* Settings for App: I think all codes are self explanatory
*/
public class SettingsActivity extends AppCompatActivity {

private static final String TAG = "SettingActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -24,37 +19,12 @@ protected void onCreate(Bundle savedInstanceState) {
.beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.commit();
/*ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}*/
}

public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);

SwitchPreference enableMultipleLang = findPreference(getString(R.string.key_enable_multiple_lang));
ListPreference listPreference = findPreference(getString(R.string.key_language_for_tesseract));
MultiSelectListPreference multiSelectListPreference = findPreference(getString(R.string.key_language_for_tesseract_multi));

if (enableMultipleLang.isChecked()) {
multiSelectListPreference.setVisible(true);
listPreference.setVisible(false);
} else {
multiSelectListPreference.setVisible(false);
listPreference.setVisible(true);
}

enableMultipleLang.setOnPreferenceChangeListener((preference, newValue) -> {
boolean b = (boolean) newValue;
multiSelectListPreference.setVisible(b);
listPreference.setVisible(!b);

return true;
});

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

import com.googlecode.tesseract.android.TessBaseAPI;

import java.util.Set;
import java.util.stream.Collectors;

import io.github.subhamtyagi.ocr.Language;

/**
* This class convert the image to text and return the text on image
*/
Expand All @@ -21,14 +26,17 @@ public class ImageTextReader {
* initialize and train the tesseract engine
*
* @param path a path to training data
* @param language language code i.e. selected by user
* @param languages language code i.e. selected by user
* @return the instance of this class for later use
*/
public static ImageTextReader geInstance(String path, String language,int pageSegMode, TessBaseAPI.ProgressNotifier progressNotifier) {
public static ImageTextReader getInstance(String path, Set<Language> languages, int pageSegMode, TessBaseAPI.ProgressNotifier progressNotifier) {
try {
ImageTextReader imageTextReader=new ImageTextReader();
api = new TessBaseAPI(progressNotifier);
imageTextReader.success = api.init(path, language);
imageTextReader.success = api.init(path, languages
.stream()
.map(Language::getCode)
.collect(Collectors.joining("+")));
api.setPageSegMode(pageSegMode);
return imageTextReader;
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ public class Constants {

public static final String LANGUAGE_CODE = "%s.traineddata";

public static final String KEY_LANGUAGE_FOR_TESSERACT = "language_for_tesseract";
public static final String KEY_ENABLE_MULTI_LANG = "key_enable_multiple_lang";
public static final String KEY_TESS_TRAINING_DATA_SOURCE = "tess_training_data_source";
public static final String KEY_LANGUAGE_FOR_TESSERACT_MULTI = "multi_languages";
public static final String KEY_GRAYSCALE_IMAGE_OCR = "grayscale_image_ocr";
public static final String KEY_LAST_USE_IMAGE_LOCATION = "last_use_image_location";
public static final String KEY_LAST_USE_IMAGE_TEXT = "last_use_image_text";
Expand Down
10 changes: 8 additions & 2 deletions app/src/main/java/io/github/subhamtyagi/ocr/utils/SpUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.Set;
Expand Down Expand Up @@ -84,12 +85,12 @@ public boolean getBoolean(String key, boolean def) {
return mPref.getBoolean(key, def);
}

@Nullable
@NonNull
public String getString(String key) {
return mPref.getString(key, "");
}

@Nullable
@NonNull
public String getString(String key, String def) {
return mPref.getString(key, def);
}
Expand Down Expand Up @@ -132,4 +133,9 @@ public void clear() {
}


public void putStringSet(String key, Set<String> value) {
Editor editor = mPref.edit();
editor.putStringSet(key, value);
editor.apply();
}
}
79 changes: 37 additions & 42 deletions app/src/main/java/io/github/subhamtyagi/ocr/utils/Utils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package io.github.subhamtyagi.ocr.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;

import androidx.annotation.NonNull;

import com.googlecode.leptonica.android.AdaptiveMap;
import com.googlecode.leptonica.android.Binarize;
Expand All @@ -13,7 +17,13 @@
import com.googlecode.leptonica.android.Skew;
import com.googlecode.leptonica.android.WriteFile;

import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;

import io.github.subhamtyagi.ocr.Language;
import io.github.subhamtyagi.ocr.R;
import kotlin.Triple;

public class Utils {

Expand Down Expand Up @@ -66,36 +76,14 @@ public static boolean isPersistData() {
return SpUtil.getInstance().getBoolean(Constants.KEY_PERSIST_DATA, true);
}

public static String getTesseractStringForMultipleLanguages(Set<String> langs) {
if (langs == null) return DEFAULT_LANGUAGE;
StringBuilder rLanguage = new StringBuilder();
for (String lang : langs) {
rLanguage.append(lang);
rLanguage.append("+");
}
return rLanguage.subSequence(0, rLanguage.toString().lastIndexOf('+')).toString();
}

public static String getTrainingDataType() {
return SpUtil.getInstance().getString(Constants.KEY_TESS_TRAINING_DATA_SOURCE, "best");
}

public static String getTrainingDataLanguage() {
if (SpUtil.getInstance().getBoolean(Constants.KEY_ENABLE_MULTI_LANG)) {
return getTesseractStringForMultipleLanguages(SpUtil.getInstance().getStringSet(Constants.KEY_LANGUAGE_FOR_TESSERACT_MULTI, null));
} else {
return SpUtil.getInstance().getString(Constants.KEY_LANGUAGE_FOR_TESSERACT, DEFAULT_LANGUAGE);
}

}

public static String setTrainingDataLanguage(String language) {
if (SpUtil.getInstance().getBoolean(Constants.KEY_ENABLE_MULTI_LANG)) {
return getTesseractStringForMultipleLanguages(SpUtil.getInstance().getStringSet(Constants.KEY_LANGUAGE_FOR_TESSERACT_MULTI, null));
} else {
return SpUtil.getInstance().getString(Constants.KEY_LANGUAGE_FOR_TESSERACT, DEFAULT_LANGUAGE);
}

public static @NonNull Set<Language> getTrainingDataLanguages(Context c) {
return allLangs(c,SpUtil.getInstance().getStringSet(
c.getString(R.string.key_language_for_tesseract_multi),
Collections.singleton("eng")));
}

public static int getPageSegMode() {
Expand All @@ -110,27 +98,34 @@ public static String getLastUsedText() {
return SpUtil.getInstance().getString(Constants.KEY_LAST_USE_IMAGE_TEXT, "");
}

public static String[] getLast3UsedLanguage() {
return new String[]{
SpUtil.getInstance().getString(Constants.KEY_LAST_USED_LANGUAGE_1, "eng"),
SpUtil.getInstance().getString(Constants.KEY_LAST_USED_LANGUAGE_2, "hin"),
SpUtil.getInstance().getString(Constants.KEY_LAST_USED_LANGUAGE_3, "deu")
};
public static Triple<Set<Language>, Set<Language>, Set<Language>> getLast3UsedLanguage(Context c) {
return new Triple<>(
allLangs(c, SpUtil.getInstance().getStringSet(c.getString(R.string.key_language_for_tesseract_multi), Collections.singleton("eng"))),
allLangs(c, SpUtil.getInstance().getStringSet(Constants.KEY_LAST_USED_LANGUAGE_2, Collections.singleton("hin"))),
allLangs(c, SpUtil.getInstance().getStringSet(Constants.KEY_LAST_USED_LANGUAGE_3, Collections.singleton("deu")))
);
}

private static Set<Language> allLangs(Context c, Set<String> codes) {
return codes.stream().map(code -> new Language(c, code)).collect(Collectors.toSet());
}

public static void setLastUsedLanguage(String lastUsedLanguage) {
String l1 = SpUtil.getInstance().getString(Constants.KEY_LAST_USED_LANGUAGE_1, "eng");
if (lastUsedLanguage.contentEquals(l1)) {
public static void setLastUsedLanguage(Context c, Set<Language> lastUsedLanguage) {
Set<Language> l1 = getLast3UsedLanguage(c).getFirst();
Set<Language> l2 = getLast3UsedLanguage(c).getSecond();
if (lastUsedLanguage.equals(l1)) {
return;
}
String l2 = SpUtil.getInstance().getString(Constants.KEY_LAST_USED_LANGUAGE_2, "hin");
if (l2.contentEquals(lastUsedLanguage)) {
SpUtil.getInstance().putString(Constants.KEY_LAST_USED_LANGUAGE_2, l1);
SpUtil.getInstance().putString(Constants.KEY_LAST_USED_LANGUAGE_1, lastUsedLanguage);
Set<String> lastCodes = lastUsedLanguage.stream().map(Language::getCode).collect(Collectors.toSet());
Set<String> l1Codes = l1.stream().map(Language::getCode).collect(Collectors.toSet());
Set<String> l2Codes = l2.stream().map(Language::getCode).collect(Collectors.toSet());
if (l2.equals(lastUsedLanguage)) {
SpUtil.getInstance().putStringSet(Constants.KEY_LAST_USED_LANGUAGE_2, l1Codes);
SpUtil.getInstance().putStringSet(c.getString(R.string.key_language_for_tesseract_multi), lastCodes);
} else {
SpUtil.getInstance().putString(Constants.KEY_LAST_USED_LANGUAGE_3, l2);
SpUtil.getInstance().putString(Constants.KEY_LAST_USED_LANGUAGE_2, l1);
SpUtil.getInstance().putString(Constants.KEY_LAST_USED_LANGUAGE_1, lastUsedLanguage);
SpUtil.getInstance().putStringSet(Constants.KEY_LAST_USED_LANGUAGE_3, l2Codes);
SpUtil.getInstance().putStringSet(Constants.KEY_LAST_USED_LANGUAGE_2, l1Codes);
SpUtil.getInstance().putStringSet(c.getString(R.string.key_language_for_tesseract_multi), lastCodes);
}

}
Expand Down
15 changes: 0 additions & 15 deletions app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,6 @@
android:title="@string/select_tesseract_data_type"
app:icon="@drawable/ic_baseline_more_horiz_32" />

<SwitchPreference
android:defaultValue="false"
android:key="@string/key_enable_multiple_lang"
android:summary="@string/multiple_lang_summary"
android:title="@string/select_multi_languages_title" />

<ListPreference
android:defaultValue="eng"
android:entries="@array/ocr_engine_language"
android:entryValues="@array/key_ocr_engine_language_value"
android:key="@string/key_language_for_tesseract"
android:summary="@string/language"
android:title="@string/tess_language_title"
app:icon="@drawable/ic_baseline_language_32" />

<MultiSelectListPreference

android:entries="@array/ocr_engine_language"
Expand Down

0 comments on commit 67f6580

Please sign in to comment.