Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin : Compatible with TaskerPlugin #110

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This App is based on <a href="https://github.com/tesseract-ocr/tesseract/blob/ma

## 🚀<font color="#2467d6"><b>Features</b></font>

* Compatible with Tasker plugin [TaskerPlugin](https://github.com/joaomgcd/TaskerPluginSample/blob/master/README.md).
* Extract Text From Images.
* Copy data to Clipboard.
* Select any part of Text.
Expand Down Expand Up @@ -37,6 +38,7 @@ This App is based on <a href="https://github.com/tesseract-ocr/tesseract/blob/ma

## <font color="#2467d6"><b>Code Contributors </b></font>

* Sauvio
* Shubham Tyagi
* New UI is designed by [Hannes Gehrold](https://github.com/h4n23s)
* [urlordjames](https://github.com/urlordjames)
Expand Down
22 changes: 15 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 33
compileSdkVersion 34
def gitUrl = getRepositoryURL()

defaultConfig {
applicationId "io.github.subhamtyagi.ocr"
minSdkVersion 18
targetSdkVersion 33
minSdkVersion 19
targetSdkVersion 34
versionCode 12
versionName "4.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down Expand Up @@ -65,21 +66,28 @@ android {
compileOptions {
coreLibraryDesugaringEnabled true

sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility "$java_version"
targetCompatibility "$java_version"
}
namespace 'io.github.subhamtyagi.ocr'
buildFeatures {
viewBinding true
}

}

dependencies {

implementation project(':taskerpluginlibrary')
implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
implementation project(':cropper')

implementation 'androidx.appcompat:appcompat:1.6.0'
implementation 'androidx.preference:preference:1.2.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2'

// implementation 'cz.adaptech:tesseract4android:4.1.1'
// implementation 'cz.adaptech:tesseract4android:4.1.1'
implementation 'cz.adaptech.tesseract4android:tesseract4android-openmp:4.3.0'
implementation 'com.google.android.material:material:1.9.0-alpha01'

Expand Down
28 changes: 28 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,31 @@
#-obfuscationdictionary
#-classobfuscationdictionary
#-packageobfuscationdictionary

-keepattributes SourceFile,LineNumberTable
-keepattributes *Annotation*
-keep public class com.joaomgcd.taskerpluginlibrary.output.TaskerOutputObject { *; }
-keep public class com.joaomgcd.taskerpluginlibrary.output.TaskerOutputVariable { *; }

-keepclasseswithmembers class * {
@com.joaomgcd.taskerpluginlibrary.input.TaskerInputField <fields>;
}
-keep @com.joaomgcd.taskerpluginlibrary.input.TaskerInputRoot public class *
-keep @com.joaomgcd.taskerpluginlibrary.input.TaskerInputObject public class *
-keep @com.joaomgcd.taskerpluginlibrary.output.TaskerOutputObject public class *
-keepclassmembers class * {
@com.joaomgcd.taskerpluginlibrary.output.TaskerOutputObject *;
}
-keepclassmembers class * {
@com.joaomgcd.taskerpluginlibrary.output.TaskerOutputVariable *;
}
-keepclassmembers @com.joaomgcd.taskerpluginlibrary.output.TaskerOutputObject class * { *; }
-keep public class * extends com.joaomgcd.taskerpluginlibrary.runner.TaskerPluginRunner { *; }


-keep public class net.dinglisch.android.tasker.PluginResultReceiver { *; }

-dontwarn android.**
-dontwarn com.google.**

-keep public class kotlin.Unit { *; }
11 changes: 10 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.github.subhamtyagi.ocr">

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

<queries>
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
Expand Down Expand Up @@ -65,6 +65,15 @@
android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
android:exported="true"
android:theme="@style/Base.Theme.AppCompat" />
<activity
android:name=".tasker.ActivityConfigOCR"
android:exported="true"
android:icon="@mipmap/ic_launcher"
android:label="OCR">
<intent-filter>
<action android:name="com.twofortyfouram.locale.intent.action.EDIT_SETTING" />
</intent-filter>
</activity>
</application>

</manifest>
53 changes: 40 additions & 13 deletions app/src/main/java/io/github/subhamtyagi/ocr/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
Expand Down Expand Up @@ -33,6 +32,7 @@
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.progressindicator.LinearProgressIndicator;
import com.googlecode.tesseract.android.TessBaseAPI;
import com.theartofdev.edmodo.cropper.CropFileProvider;
import com.theartofdev.edmodo.cropper.CropImage;
import com.theartofdev.edmodo.cropper.CropImageView;

Expand All @@ -53,6 +53,7 @@
import io.github.subhamtyagi.ocr.spinner.SpinnerDialog;
import io.github.subhamtyagi.ocr.utils.Constants;
import io.github.subhamtyagi.ocr.utils.CrashUtils;
import io.github.subhamtyagi.ocr.utils.LanguageUtil;
import io.github.subhamtyagi.ocr.utils.SpUtil;
import io.github.subhamtyagi.ocr.utils.Utils;

Expand All @@ -72,6 +73,7 @@ public class MainActivity extends AppCompatActivity implements TessBaseAPI.Progr
* A spinner dialog shown on share menu
*/
private SpinnerDialog spinnerDialog;
private ArrayList<String> countryCodes;
private ArrayList<String> languagesNames;
private ArrayList<String> languagesCodes;
private CrashUtils crashUtils;
Expand Down Expand Up @@ -122,9 +124,22 @@ public class MainActivity extends AppCompatActivity implements TessBaseAPI.Progr
*/
private TextView mLanguageName;

private void setLanguage() {
SpUtil.getInstance().init(this);
languagesCodes = new ArrayList<>(Arrays.asList(getResources().getStringArray(R.array.key_language_entries_value)));
countryCodes = new ArrayList<>(Arrays.asList(getResources().getStringArray(R.array.key_country_entries_value)));
String currentLanguage = SpUtil.getInstance().getString(Constants.CURRENT_LANGUAGE, "");
if (getApplicationContext() == null || currentLanguage.isEmpty()) {
return;
}
String currentCountry = getCountryCodeFromLanguage(currentLanguage);
LanguageUtil.changeLanguage(MainActivity.this, currentLanguage, currentCountry);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setLanguage();
setContentView(R.layout.activity_main);

/*
Expand Down Expand Up @@ -212,7 +227,7 @@ private void initIntent() {
showLanguageSelectionDialog(imageUri);
}
}
} else if (action.equals("screenshot")) {
} else if ("screenshot".equals(action)) {
// uri
}
}
Expand All @@ -233,23 +248,27 @@ private void showLanguageSelectionDialog(Uri imageUri) {
RadioButton radioButton3 = view.findViewById(R.id.rb_language3);
String[] la = Utils.getLast3UsedLanguage();

radioButton1.setText(getLanguageNameFromCode(la[0]));
radioButton2.setText(getLanguageNameFromCode(la[1]));
radioButton3.setText(getLanguageNameFromCode(la[2]));
radioButton1.setText(getLanguageNameFromCode(la[0], false));
radioButton2.setText(getLanguageNameFromCode(la[1], false));
radioButton3.setText(getLanguageNameFromCode(la[2], false));

radioButton1.setOnClickListener(view1 -> startOCRFromShareMenu(la[0], imageUri));
radioButton2.setOnClickListener(view1 -> startOCRFromShareMenu(la[1], imageUri));
radioButton3.setOnClickListener(view1 -> startOCRFromShareMenu(la[2], imageUri));
}

private String getLanguageNameFromCode(String code) {
return languagesNames.get(languagesCodes.indexOf(code));
private String getLanguageNameFromCode(String code, boolean multipleLanguages) {
return multipleLanguages ? code : languagesNames.get(languagesCodes.indexOf(code));
}

private String getCountryCodeFromLanguage(String mLanguage){
return countryCodes.isEmpty() ? "US" : countryCodes.get(languagesCodes.indexOf(mLanguage));
}

@Override
protected void onResume() {
super.onResume();
mLanguageName.setText(getLanguageNameFromCode(mLanguage));
mLanguageName.setText(getLanguageNameFromCode(mLanguage,true));
}

public void startOCRFromShareMenu(String lang, Uri imageUri) {
Expand Down Expand Up @@ -318,7 +337,8 @@ public void run() {
if (mImageTextReader != null) {
mImageTextReader.tearDownEverything();
}
mImageTextReader = ImageTextReader.geInstance(cf.getAbsolutePath(), mLanguage, mPageSegMode, MainActivity.this::onProgressValues);
int mEngineMode = Utils.getEngineMode();
mImageTextReader = ImageTextReader.geInstance(cf.getAbsolutePath(), mLanguage, mPageSegMode, mEngineMode, MainActivity.this::onProgressValues);
//check if current language data is valid
//if it is invalid(i.e. corrupted, half downloaded, tempered) then delete it
if (!mImageTextReader.success) {
Expand Down Expand Up @@ -349,7 +369,7 @@ private void downloadLanguageData(final String dataType, final String lang) {

if (ni != null && ni.isConnected()) {
//region show confirmation dialog, On 'yes' download the training data.
String msg = String.format(getString(R.string.download_description), getLanguageNameFromCode(lang));
String msg = String.format(getString(R.string.download_description), getLanguageNameFromCode(lang, true));
dialog = new AlertDialog.Builder(this)
.setTitle(R.string.training_data_missing)
.setCancelable(false)
Expand Down Expand Up @@ -525,8 +545,12 @@ public void onProgressValues(final TessBaseAPI.ProgressValues progressValues) {

public void saveBitmapToStorage(Bitmap bitmap) {
FileOutputStream fileOutputStream;
File dir;
File file;
try {
fileOutputStream = openFileOutput("last_file.jpeg", Context.MODE_PRIVATE);
dir = CropFileProvider.filesDir(getApplicationContext());
file = new File(dir, "last_file.jpeg");
fileOutputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 30, fileOutputStream);
fileOutputStream.close();
} catch (FileNotFoundException e) {
Expand All @@ -541,11 +565,14 @@ public void saveBitmapToStorage(Bitmap bitmap) {
public Bitmap loadBitmapFromStorage() {
Bitmap bitmap = null;
FileInputStream fileInputStream;
File dir;
File file;
try {
fileInputStream = openFileInput("last_file.jpeg");
dir = CropFileProvider.filesDir(getApplicationContext());
file = new File(dir, "last_file.jpeg");
fileInputStream = new FileInputStream(file);
bitmap = BitmapFactory.decodeStream(fileInputStream);
fileInputStream.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
crashUtils.logException(e);
Expand Down
Loading
Loading