├── settings.gradle
├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_circle.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_circle.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_circle.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_circle.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_circle.png
│ │ │ ├── drawable-v24
│ │ │ │ ├── button_bg.xml
│ │ │ │ └── ic_launcher_foreground.xml
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── menu
│ │ │ │ └── menu_main.xml
│ │ │ ├── values
│ │ │ │ ├── colors.xml
│ │ │ │ ├── styles.xml
│ │ │ │ └── strings.xml
│ │ │ ├── layout
│ │ │ │ ├── dialog_header.xml
│ │ │ │ └── activity_main.xml
│ │ │ ├── drawable
│ │ │ │ ├── find_box.xml
│ │ │ │ └── ic_launcher_background.xml
│ │ │ ├── values-es
│ │ │ │ └── strings.xml
│ │ │ └── values-tr-rTR
│ │ │ │ └── strings.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── hasbis
│ │ │ │ └── findonpage
│ │ │ │ ├── features
│ │ │ │ ├── ocr
│ │ │ │ │ ├── OCRActions.java
│ │ │ │ │ └── ImageToText.java
│ │ │ │ └── views
│ │ │ │ │ └── DrawableImageView.java
│ │ │ │ ├── utils
│ │ │ │ └── FirebaseAnalyticsUtils.java
│ │ │ │ └── MainActivity.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── hasbis
│ │ │ └── findonpage
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── hasbis
│ │ └── findonpage
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── gradle.properties
├── README.md
├── gradlew.bat
└── gradlew
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 | google-services.json
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hasbisevinc/Find-on-Page/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hasbisevinc/Find-on-Page/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hasbisevinc/Find-on-Page/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hasbisevinc/Find-on-Page/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hasbisevinc/Find-on-Page/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hasbisevinc/Find-on-Page/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_circle.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hasbisevinc/Find-on-Page/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_circle.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_circle.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hasbisevinc/Find-on-Page/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_circle.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_circle.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hasbisevinc/Find-on-Page/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_circle.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_circle.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hasbisevinc/Find-on-Page/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_circle.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_circle.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hasbisevinc/Find-on-Page/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_circle.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/button_bg.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches/build_file_checksums.ser
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | .DS_Store
9 | /build
10 | /captures
11 | .externalNativeBuild
12 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionBase=GRADLE_USER_HOME
2 | distributionPath=wrapper/dists
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #008577
4 | #00574B
5 | #000000
6 | #d3dbdc
7 | #a9eca2
8 | #bbded6
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/hasbis/findonpage/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.hasbis.findonpage;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/dialog_header.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hasbis/findonpage/features/ocr/OCRActions.java:
--------------------------------------------------------------------------------
1 | package com.hasbis.findonpage.features.ocr;
2 |
3 |
4 | import java.lang.annotation.Retention;
5 | import java.lang.annotation.RetentionPolicy;
6 | import androidx.annotation.IntDef;
7 |
8 |
9 | @IntDef({
10 | OCRActions.PROCESS_STARTED,
11 | OCRActions.PROCESS_FINISHED,
12 | OCRActions.PROCESS_FAIL,
13 | OCRActions.PROCESS_TEXT_NOT_FOUND,
14 | })
15 | @Retention(RetentionPolicy.SOURCE)
16 | public @interface OCRActions {
17 | int PROCESS_STARTED = 0;
18 | int PROCESS_FINISHED = 1;
19 | int PROCESS_FAIL = 2;
20 | int PROCESS_TEXT_NOT_FOUND = 3;
21 | }
22 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 |
15 |
16 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/find_box.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | -
4 |
5 |
-
6 |
7 |
8 |
9 |
10 |
11 | -
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/hasbis/findonpage/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.hasbis.findonpage;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.hasbis.findonpage", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
17 |
18 |
21 |
22 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Find-on-Page
2 | An android application that finds desired words in a hard-copy paper. Just like searching with CTRL + F
3 |
4 | ## Introduction
5 | Have you ever want to search a word in a paper? This application will find any desired word in a hard copy page just like searching with CTRL+F. You just need to take a photo of paper and type the word. We will highlight these words in seconds...
6 |
7 | 
8 | 
9 |
10 |
11 | ## How to Use
12 | Way 1: download from playstore
13 | https://play.google.com/store/apps/details?id=com.hasbis.findonpage
14 |
15 | Way 2: Download apk from releases:
16 | https://github.com/hasbisevinc/Find-on-Page/releases
17 |
18 | Way 3: clone source code and build it
19 |
20 | ## How to Build
21 | - Clone this repo into your local
22 | ```
23 | git clone git@github.com:hasbisevinc/Find-on-Page.git
24 | ```
25 | - Create a firebase project and download google-services.json file into app directory.
26 |
27 | To learn how to create a firebase: https://firebase.google.com/docs/android/setup
28 | - Open project with Android Studio and compile it
29 |
30 | ## Contributions
31 | Currently we need:
32 |
33 | - Translate of string.xml file
34 | - Testing application with different phones
35 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Find On Page
3 | Take a new photo
4 | Find
5 | Select all
6 | Text to search
7 | permission denied
8 | No result found
9 | %1$s result found
10 | Please try again
11 | No text found in image, please try again
12 | Please wait
13 | Show search bar
14 | Hide search bar
15 | Close
16 | Take a photo from camera
17 | Take a photo from gallery
18 | Take new photo from:
19 | Cancel
20 | Capture a photo from your camera and search any words on this photo
21 | Required credits:1
22 | Capture a photo from your gallery and search any words on this photo
23 |
24 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 | apply plugin: 'io.fabric'
3 |
4 | android {
5 | compileSdkVersion 28
6 | defaultConfig {
7 | applicationId "com.hasbis.findonpage"
8 | minSdkVersion 21
9 | targetSdkVersion 28
10 | versionCode 2
11 | versionName "1.1"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 |
21 | android {
22 | packagingOptions {
23 | exclude 'META-INF/proguard/androidx-annotations.pro'
24 | }
25 | }
26 | }
27 |
28 | dependencies {
29 | implementation fileTree(dir: 'libs', include: ['*.jar'])
30 | implementation 'androidx.appcompat:appcompat:1.1.0'
31 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
32 | implementation 'com.google.firebase:firebase-core:17.2.0'
33 | apply plugin: 'com.google.gms.google-services'
34 | implementation 'com.google.firebase:firebase-ml-vision:23.0.0'
35 | implementation 'com.github.d-max:spots-dialog:1.1@aar'
36 | implementation 'io.reactivex.rxjava2:rxjava:2.2.9'
37 | implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
38 | implementation 'com.google.firebase:firebase-core:17.2.0'
39 | implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
40 | implementation 'com.orhanobut:dialogplus:1.11@aar'
41 | }
42 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hasbis/findonpage/features/views/DrawableImageView.java:
--------------------------------------------------------------------------------
1 | package com.hasbis.findonpage.features.views;
2 |
3 | import android.content.Context;
4 | import android.graphics.Canvas;
5 | import android.graphics.Color;
6 | import android.graphics.Paint;
7 | import android.graphics.Rect;
8 | import android.util.AttributeSet;
9 | import android.widget.ImageView;
10 |
11 | import java.util.ArrayList;
12 |
13 | public class DrawableImageView extends ImageView {
14 |
15 | ArrayList rects;
16 | Paint paint = new Paint();
17 |
18 | public DrawableImageView(Context context, AttributeSet attributeSet) {
19 | super(context, attributeSet);
20 | this.setWillNotDraw(false);
21 | rects = new ArrayList<>();
22 | }
23 |
24 | public DrawableImageView(Context context) {
25 | super(context);
26 | this.setWillNotDraw(false);
27 | rects = new ArrayList<>();
28 | }
29 |
30 | public void setRects(ArrayList rects) {
31 | this.rects = rects;
32 | invalidate();
33 | }
34 |
35 | @Override
36 | protected void onDraw(Canvas canvas) {
37 | super.onDraw(canvas);
38 |
39 | // Initialize a new Paint instance to draw the Rectangle
40 | paint.setStyle(Paint.Style.FILL);
41 | paint.setColor(Color.YELLOW);
42 | paint.setAntiAlias(true);
43 | paint.setAlpha(125);
44 |
45 | // Finally, draw the rectangle on the canvas
46 | for (Rect rec : rects) {
47 | canvas.drawRect(rec, paint);
48 | }
49 | }
50 |
51 | }
--------------------------------------------------------------------------------
/app/src/main/res/values-es/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Encuéntralo en la página
3 | Tomar una nueva foto
4 | Encontrar
5 | Seleccionar todo
6 | Texto a buscar
7 | permiso denegado
8 | No se han encontrado resultados
9 | Se encontró %1$s resultado
10 | Inténtalo de nuevo
11 | No se encontró texto en la imagen, intente nuevamente
12 | por favor espera
13 | Mostrar barra de búsqueda
14 | Ocultar barra de búsqueda
15 | Cerrar
16 | Tomar una foto de la cámara
17 | Toma una foto de la galería
18 | Tomar nueva foto de:
19 | Cancelar
20 | Capture una foto de su cámara y busque cualquier palabra en esta foto
21 | Créditos requeridos: 1
22 | Capture una foto de su galería y busque cualquier palabra en esta foto
23 |
24 |
--------------------------------------------------------------------------------
/app/src/main/res/values-tr-rTR/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | Kağıtta Bul
4 | Yeni Fotoğraf
5 | Bul
6 | Hepsini seç
7 | Aranacak kelime
8 | İzin alınamadı
9 | Sonuç bulunamadı
10 | %1$s tane sonuç bulundu
11 | Lütfen tekrar deneyiniz
12 | Resimde kelime bulunamadı, lütfen tekrar deneyiniz
13 | Lütfen bekleyin
14 | Arama barını göster
15 | Arama barını gizle
16 | Kapat
17 | Kameradan görüntü yakala
18 | Galeriden resim seç
19 | Yeni fotoğrafı şuradan al:
20 | İptal
21 | Kameranızdan taratmak istediğiniz belgenin fotoğrafını çekin ve bu belge üzerinde kelime aramaya hemen başlayın
22 | Gerekli kredi:1
23 | Galeriden taratmak istediğiniz belgenin fotoğrafını çekin ve bu belge üzerinde kelime aramaya hemen başlayın
24 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hasbis/findonpage/utils/FirebaseAnalyticsUtils.java:
--------------------------------------------------------------------------------
1 | package com.hasbis.findonpage.utils;
2 |
3 | import android.content.Context;
4 | import android.os.Bundle;
5 |
6 | import com.google.firebase.analytics.FirebaseAnalytics;
7 |
8 | public class FirebaseAnalyticsUtils {
9 | private final static String TAG = "FirebaseAnalyticsUtils";
10 |
11 | private static FirebaseAnalyticsUtils instance = null;
12 | private FirebaseAnalytics mFirebaseAnalytics;
13 |
14 | private FirebaseAnalyticsUtils()
15 | {
16 | }
17 |
18 | public static FirebaseAnalyticsUtils getInstance() {
19 | synchronized (TAG) {
20 | if (instance == null) {
21 | synchronized (TAG) {
22 | instance = new FirebaseAnalyticsUtils();
23 | }
24 | }
25 | }
26 | return instance;
27 | }
28 |
29 | public void init(Context context) {
30 | mFirebaseAnalytics = FirebaseAnalytics.getInstance(context);
31 | }
32 |
33 | private void saveLog(String id, String name) {
34 | Bundle bundle = new Bundle();
35 | bundle.putString(FirebaseAnalytics.Param.ITEM_ID, id);
36 | bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name);
37 | bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, "image");
38 | mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);
39 | }
40 |
41 | public void onNewPhoto() {
42 | saveLog("PHOTO", "NEW_PHOTO");
43 | }
44 |
45 | public void onNewGalleryPhoto() {
46 | saveLog("GALLERY", "NEW_GALLERY");
47 | }
48 |
49 | public void onNewSearch() {
50 | saveLog("SEARCH", "NEW_SEARCH");
51 | }
52 |
53 | public void onSelectAll() {
54 | saveLog("SELECT_ALL", "NEW_SELECT_ALL");
55 | }
56 |
57 | public void onShowFindBoxClicked() {
58 | saveLog("SHOW_FIND_BOX", "NEW_SHOW_FIND_BOX");
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hasbis/findonpage/features/ocr/ImageToText.java:
--------------------------------------------------------------------------------
1 | package com.hasbis.findonpage.features.ocr;
2 |
3 | import android.content.Context;
4 | import android.graphics.Bitmap;
5 | import android.graphics.Matrix;
6 | import android.graphics.Rect;
7 | import android.net.Uri;
8 | import android.text.TextUtils;
9 | import android.util.Log;
10 |
11 | import com.google.android.gms.tasks.OnFailureListener;
12 | import com.google.android.gms.tasks.OnSuccessListener;
13 | import com.google.firebase.ml.vision.FirebaseVision;
14 | import com.google.firebase.ml.vision.common.FirebaseVisionImage;
15 | import com.google.firebase.ml.vision.text.FirebaseVisionText;
16 | import com.google.firebase.ml.vision.text.FirebaseVisionTextRecognizer;
17 |
18 | import java.io.IOException;
19 | import java.util.ArrayList;
20 |
21 | import io.reactivex.Observable;
22 | import io.reactivex.Observer;
23 | import io.reactivex.annotations.NonNull;
24 | import io.reactivex.subjects.PublishSubject;
25 |
26 | public class ImageToText {
27 | private static final String TAG = "ImageToText";
28 |
29 | private Context context;
30 | private FirebaseVisionText firebaseVisionText = null;
31 | private int mlImageHeight = -1;
32 | private int mlImageWidth = -1;
33 |
34 | PublishSubject actionSubject = PublishSubject.create();
35 | PublishSubject resultSubject = PublishSubject.create();
36 |
37 | public Observable getActionObserveable() {
38 | return actionSubject;
39 | }
40 |
41 | public Observable getResultObserveable() {
42 | return resultSubject;
43 | }
44 |
45 | public ImageToText(Context context) {
46 | this.context = context;
47 | }
48 |
49 | public void startOCR(Uri uri) {
50 | FirebaseVisionImage image;
51 | try {
52 | image = FirebaseVisionImage.fromFilePath(context, uri);
53 | mlImageWidth = image.getBitmap().getWidth();
54 | mlImageHeight = image.getBitmap().getHeight();
55 | } catch (IOException e) {
56 | Log.e(TAG, "findText: ", e);
57 | return;
58 | }
59 |
60 | sendAction(OCRActions.PROCESS_STARTED);
61 | runOCR(image);
62 | }
63 |
64 | public void markText(String text) {
65 | if (firebaseVisionText == null) {
66 | sendAction(OCRActions.PROCESS_TEXT_NOT_FOUND);
67 | return;
68 | }
69 |
70 | Log.d(TAG, "markText: all text:"+firebaseVisionText.getText());
71 | ArrayList rects = new ArrayList<>();
72 | for (FirebaseVisionText.TextBlock block: firebaseVisionText.getTextBlocks()) {
73 | for (FirebaseVisionText.Line line: block.getLines()) {
74 | for (FirebaseVisionText.Element element: line.getElements()) {
75 | String elementText = element.getText();
76 | Rect elementFrame = element.getBoundingBox();
77 |
78 | if (elementFrame != null &&
79 | elementText.toLowerCase().contains(text.toLowerCase())) {
80 | rects.add(elementFrame);
81 | }
82 |
83 | }
84 | }
85 | }
86 |
87 | sendResult(rects);
88 | }
89 |
90 | public String getAllText() {
91 | return (firebaseVisionText == null) ? null : firebaseVisionText.getText();
92 | }
93 |
94 | private void sendAction(@OCRActions int action) {
95 | actionSubject.onNext(action);
96 | }
97 |
98 | private void sendResult(ArrayList rects) {
99 | resultSubject.onNext(new OCRResult(rects, mlImageWidth, mlImageHeight));
100 | }
101 | private void runOCR(final FirebaseVisionImage image) {
102 | runOCR(image,false, false);
103 | }
104 |
105 | private void runOCR(final FirebaseVisionImage image, final boolean rotatedLeft, final boolean rotatedRight) {
106 | FirebaseVisionTextRecognizer detector = FirebaseVision.getInstance()
107 | .getOnDeviceTextRecognizer();
108 |
109 | detector.processImage(image)
110 | .addOnSuccessListener(new OnSuccessListener() {
111 | @Override
112 | public void onSuccess(FirebaseVisionText local_firebaseVisionText) {
113 | firebaseVisionText = local_firebaseVisionText;
114 | if (firebaseVisionText == null ||
115 | TextUtils.isEmpty(firebaseVisionText.getText())) {
116 | if (!rotatedLeft) {
117 | // rotate left
118 | runOCR(rotateImage(image, true),
119 | true, false);
120 | } else if (rotatedLeft && !rotatedRight) {
121 | // rotate right
122 | runOCR(rotateImage(image, false),
123 | true, true);
124 | } else {
125 | //fail
126 | sendAction(OCRActions.PROCESS_FAIL);
127 | }
128 | } else {
129 | sendAction(OCRActions.PROCESS_FINISHED);
130 | }
131 | }
132 | })
133 | .addOnFailureListener(
134 | new OnFailureListener() {
135 | @Override
136 | public void onFailure(@NonNull Exception e) {
137 | sendAction(OCRActions.PROCESS_FAIL);
138 | Log.e(TAG, "onFailure: ", e);
139 | }
140 | });
141 | }
142 |
143 | private FirebaseVisionImage rotateImage(final FirebaseVisionImage image, final boolean rotateLeft) {
144 | float angle = 270;
145 | if (rotateLeft) {
146 | angle = 90;
147 | }
148 |
149 | Bitmap bitmap = rotateBitmap(image.getBitmap(), angle);
150 | return FirebaseVisionImage.fromBitmap(bitmap);
151 | }
152 |
153 | private Bitmap rotateBitmap(Bitmap source, float angle)
154 | {
155 | Matrix matrix = new Matrix();
156 | matrix.postRotate(angle);
157 | return Bitmap.createBitmap(source, 0, 0,
158 | source.getWidth(), source.getHeight(), matrix, true);
159 | }
160 |
161 | public class OCRResult {
162 | public ArrayList rects;
163 | public int height;
164 | public int width;
165 |
166 | public OCRResult(ArrayList rects,
167 | int width,
168 | int height) {
169 | this.rects = rects;
170 | this.height = height;
171 | this.width = width;
172 | }
173 | }
174 |
175 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
20 |
21 |
27 |
28 |
29 |
40 |
41 |
50 |
51 |
56 |
57 |
68 |
69 |
70 |
83 |
84 |
94 |
95 |
107 |
108 |
119 |
120 |
121 |
134 |
135 |
145 |
146 |
158 |
159 |
170 |
171 |
172 |
180 |
181 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hasbis/findonpage/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.hasbis.findonpage;
2 |
3 | import android.Manifest;
4 | import android.app.Activity;
5 | import android.app.AlertDialog;
6 | import android.content.DialogInterface;
7 | import android.content.Intent;
8 | import android.content.pm.PackageManager;
9 | import android.graphics.Rect;
10 | import android.net.Uri;
11 | import android.os.Build;
12 | import android.os.StrictMode;
13 | import android.provider.MediaStore;
14 | import android.os.Bundle;
15 | import android.text.TextUtils;
16 | import android.util.Log;
17 | import android.view.Gravity;
18 | import android.view.Menu;
19 | import android.view.MenuItem;
20 | import android.view.View;
21 | import android.view.inputmethod.InputMethodManager;
22 | import android.widget.ArrayAdapter;
23 | import android.widget.Button;
24 | import android.widget.EditText;
25 | import android.widget.LinearLayout;
26 | import android.widget.Toast;
27 |
28 | import com.hasbis.findonpage.features.ocr.ImageToText;
29 | import com.hasbis.findonpage.features.ocr.OCRActions;
30 | import com.hasbis.findonpage.features.views.DrawableImageView;
31 | import com.hasbis.findonpage.utils.FirebaseAnalyticsUtils;
32 | import com.orhanobut.dialogplus.DialogPlus;
33 | import com.orhanobut.dialogplus.ListHolder;
34 | import com.orhanobut.dialogplus.OnItemClickListener;
35 |
36 | import java.io.File;
37 | import java.io.FileNotFoundException;
38 | import java.text.DateFormat;
39 | import java.text.SimpleDateFormat;
40 | import java.util.ArrayList;
41 | import java.util.Date;
42 |
43 | import androidx.appcompat.app.AppCompatActivity;
44 | import androidx.constraintlayout.widget.ConstraintLayout;
45 | import dmax.dialog.SpotsDialog;
46 | import io.reactivex.Observer;
47 | import io.reactivex.android.schedulers.AndroidSchedulers;
48 | import io.reactivex.annotations.NonNull;
49 | import io.reactivex.disposables.CompositeDisposable;
50 | import io.reactivex.disposables.Disposable;
51 | import io.reactivex.functions.Function;
52 | import io.reactivex.schedulers.Schedulers;
53 |
54 | public class MainActivity extends AppCompatActivity {
55 | private static final String TAG = "MainActivity";
56 | private static final int CAMERA_REQUEST = 1888;
57 | private static final int GALLERY_REQUEST = 2888;
58 | private static final int CAMERA_PERMISSION_CODE = 100;
59 |
60 | private DrawableImageView imageView;
61 | private LinearLayout findBox;
62 | private LinearLayout showFindBoxLayout;
63 | private ConstraintLayout photoButton;
64 | private ConstraintLayout galleryButton;
65 | private Button findButton;
66 | private Button selectAllButton;
67 | private EditText textToFind;
68 |
69 | private String mCameraFileName = "";
70 | String textResult = "";
71 |
72 | private AlertDialog spotsDialog = null;
73 | private FirebaseAnalyticsUtils analyticsUtils;
74 | private ImageToText ocr;
75 | private CompositeDisposable compositeDisposable = new CompositeDisposable();;
76 |
77 | @Override
78 | public void onCreate(Bundle savedInstanceState) {
79 | super.onCreate(savedInstanceState);
80 | setContentView(R.layout.activity_main);
81 |
82 | initAnalytic();
83 | initView();
84 | initButtons();
85 | initOCR();
86 | bindToOCR();
87 | }
88 |
89 | @Override
90 | public boolean onCreateOptionsMenu(Menu menu) {
91 | getMenuInflater().inflate(R.menu.menu_main, menu);
92 | return true;
93 | }
94 |
95 | @Override
96 | public boolean onOptionsItemSelected(MenuItem item) {
97 | int id = item.getItemId();
98 | if (id == R.id.new_image) {
99 | ArrayAdapter adapter = new ArrayAdapter<>(
100 | this, android.R.layout.simple_list_item_1, new String[]{getString(R.string.take_a_photo_from_camera),
101 | getString(R.string.take_a_photo_from_gallery), getString(R.string.cancel)}
102 | );
103 | DialogPlus dialog = DialogPlus.newDialog(this)
104 | .setAdapter(adapter)
105 | .setOnItemClickListener(new OnItemClickListener() {
106 | @Override
107 | public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
108 | }
109 | })
110 | .setContentHolder(new ListHolder())
111 | .setGravity(Gravity.CENTER)
112 | .setExpanded(true)
113 | .setHeader(R.layout.dialog_header)
114 | .setOnItemClickListener(new OnItemClickListener() {
115 | @Override
116 | public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
117 | if (position == 0) {
118 | analyticsUtils.onNewPhoto();
119 | onCameraButtonClicked();
120 | } else if (position == 1) {
121 | analyticsUtils.onNewGalleryPhoto();
122 | Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
123 | photoPickerIntent.setType("image/*");
124 | startActivityForResult(photoPickerIntent, GALLERY_REQUEST);
125 | }
126 | dialog.dismiss();
127 | }
128 | })
129 | .create();
130 | dialog.show();
131 | }
132 | return super.onOptionsItemSelected(item);
133 | }
134 |
135 | @Override
136 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
137 | super.onRequestPermissionsResult(requestCode, permissions, grantResults);
138 | if (requestCode == CAMERA_PERMISSION_CODE) {
139 | if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
140 | onCameraButtonClicked();
141 | } else {
142 | showToast(getString(R.string.permission_denied));
143 | }
144 | }
145 | }
146 |
147 | @Override
148 | public void onActivityResult(int requestCode, int resultCode, Intent data) {
149 | super.onActivityResult(requestCode, resultCode, data);
150 | if (resultCode == Activity.RESULT_OK) {
151 | if (requestCode == CAMERA_REQUEST) {
152 | updateUIForImageGained();
153 | if (mCameraFileName != null) {
154 | Uri image = Uri.fromFile(new File(mCameraFileName));
155 | imageView.setImageURI(image);
156 | imageView.setVisibility(View.VISIBLE);
157 | startOCR(image);
158 | }
159 | } else if (requestCode == GALLERY_REQUEST) {
160 | updateUIForImageGained();
161 | final Uri image = data.getData();
162 | imageView.setImageURI(image);
163 | imageView.setVisibility(View.VISIBLE);
164 | startOCR(image);
165 | }
166 | }
167 | }
168 |
169 | private void updateUIForImageGained() {
170 | imageView.setRects(new ArrayList());
171 | findBox.setVisibility(View.VISIBLE);
172 | photoButton.setVisibility(View.GONE);
173 | galleryButton.setVisibility(View.GONE);
174 | }
175 |
176 | private void initAnalytic() {
177 | analyticsUtils = FirebaseAnalyticsUtils.getInstance();
178 | analyticsUtils.init(this);
179 | }
180 |
181 | private void initView() {
182 | this.imageView = this.findViewById(R.id.preview_imageview);
183 | this.findBox = this.findViewById(R.id.find_layout);
184 | this.showFindBoxLayout = this.findViewById(R.id.show_find_box_layout);
185 | this.photoButton = this.findViewById(R.id.camera_container);
186 | this.galleryButton = this.findViewById(R.id.gallery_container);
187 | this.findButton = this.findViewById(R.id.find_button);
188 | this.selectAllButton = this.findViewById(R.id.select_all_button);
189 | this.textToFind = this.findViewById(R.id.word_edittext);
190 | }
191 |
192 | private void initButtons() {
193 | photoButton.setOnClickListener(new View.OnClickListener() {
194 | @Override
195 | public void onClick(View v) {
196 | analyticsUtils.onNewPhoto();
197 | onCameraButtonClicked();
198 | }
199 | });
200 |
201 | galleryButton.setOnClickListener(new View.OnClickListener() {
202 | @Override
203 | public void onClick(View v) {
204 | analyticsUtils.onNewGalleryPhoto();
205 | Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
206 | photoPickerIntent.setType("image/*");
207 | startActivityForResult(photoPickerIntent, GALLERY_REQUEST);
208 | }
209 | });
210 |
211 | selectAllButton.setOnClickListener(new View.OnClickListener() {
212 | @Override
213 | public void onClick(View v) {
214 | if (!TextUtils.isEmpty(textResult)) {
215 | analyticsUtils.onSelectAll();
216 | showSelectAllDialog();
217 | }
218 | }
219 | });
220 |
221 | findButton.setOnClickListener(new View.OnClickListener() {
222 | @Override
223 | public void onClick(View v) {
224 | if (textToFind.getText().toString().length() < 1) {
225 | return;
226 | }
227 |
228 | analyticsUtils.onNewSearch();
229 | markText(textToFind.getText().toString());
230 | hideKeyboard();
231 | hideFindBox();
232 | }
233 | });
234 |
235 | showFindBoxLayout.setOnClickListener(new View.OnClickListener() {
236 | @Override
237 | public void onClick(View v) {
238 | analyticsUtils.onShowFindBoxClicked();
239 | showFindBox();
240 | }
241 | });
242 |
243 | imageView.setOnClickListener(new View.OnClickListener() {
244 | @Override
245 | public void onClick(View v) {
246 | hideKeyboard();
247 | hideFindBox();
248 | }
249 | });
250 | }
251 |
252 | private void initOCR() {
253 | ocr = new ImageToText(this);
254 | }
255 |
256 | private void bindToOCR() {
257 | ocr.getActionObserveable()
258 | .subscribeOn(Schedulers.io())
259 | .observeOn(AndroidSchedulers.mainThread())
260 | .subscribe(new Observer() {
261 | @Override
262 | public void onSubscribe(Disposable d) {
263 | compositeDisposable.add(d);
264 | }
265 |
266 | @Override
267 | public void onNext(Integer action) {
268 | switch (action) {
269 | case OCRActions.PROCESS_STARTED:
270 | showLoadingDialog();
271 | hideFindBox();
272 | break;
273 | case OCRActions.PROCESS_FINISHED:
274 | textResult = ocr.getAllText();
275 | hideLoadingDialog();
276 | showFindBox();
277 | break;
278 | case OCRActions.PROCESS_TEXT_NOT_FOUND:
279 | hideLoadingDialog();
280 | showToast(getString(R.string.no_text_found_try_again));
281 | onCameraButtonClicked();
282 | break;
283 | case OCRActions.PROCESS_FAIL:
284 | hideLoadingDialog();
285 | showToast(getString(R.string.try_again));
286 | onCameraButtonClicked();
287 | break;
288 | }
289 | }
290 |
291 | @Override
292 | public void onError(Throwable e) {
293 | Log.e(TAG, "onError: ", e);
294 | }
295 |
296 | @Override
297 | public void onComplete() {
298 |
299 | }
300 | });
301 |
302 | ocr.getResultObserveable()
303 | .subscribeOn(Schedulers.io())
304 | .observeOn(AndroidSchedulers.mainThread())
305 | .map(new Function>() {
306 | @Override
307 | public ArrayList apply(ImageToText.OCRResult result) throws Exception {
308 | ArrayList convertedRects = new ArrayList<>();
309 | for (Rect rect: result.rects) {
310 | convertedRects.add(convertRect(rect, result.width, result.height));
311 | }
312 | return convertedRects;
313 | }
314 | })
315 | .subscribe(new Observer>() {
316 | @Override
317 | public void onSubscribe(Disposable d) {
318 | compositeDisposable.add(d);
319 | }
320 |
321 | @Override
322 | public void onNext(ArrayList convertedRects) {
323 | if (convertedRects.size() == 0) {
324 | showFindBox();
325 | }
326 |
327 | showResultCount(convertedRects.size());
328 | imageView.setRects(convertedRects);
329 | }
330 |
331 | @Override
332 | public void onError(Throwable e) {
333 | Log.e(TAG, "onError: ", e);
334 | }
335 |
336 | @Override
337 | public void onComplete() {
338 |
339 | }
340 | });
341 | }
342 |
343 | private void markText(String str) {
344 | ocr.markText(str);
345 | }
346 |
347 | private void startOCR(Uri image) {
348 | ocr.startOCR(image);
349 | }
350 |
351 | private void showSelectAllDialog() {
352 | final AlertDialog.Builder builder = new AlertDialog.Builder(this);
353 | builder.setTitle(R.string.select_all);
354 |
355 | final EditText input = new EditText(this);
356 | input.setText(textResult);
357 | builder.setView(input);
358 |
359 | builder.setPositiveButton(R.string.close, new DialogInterface.OnClickListener() {
360 | @Override
361 | public void onClick(DialogInterface dialog, int which) {
362 | dialog.dismiss();
363 | }
364 | });
365 |
366 | builder.show();
367 | }
368 |
369 | private void hideFindBox() {
370 | findBox.animate().translationY(-1 * findBox.getHeight());
371 | showFindBoxLayout.setAlpha(0.7f);
372 | }
373 |
374 | private void showFindBox() {
375 | findBox.animate().translationY(0);
376 | showFindBoxLayout.setAlpha(0);
377 | }
378 |
379 | private void onCameraButtonClicked() {
380 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
381 | if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED ||
382 | checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
383 | requestPermissions(new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE},
384 | CAMERA_PERMISSION_CODE);
385 | } else {
386 | getImageFromCamera();
387 | }
388 | } else {
389 | getImageFromCamera();
390 | }
391 | }
392 |
393 | private void getImageFromCamera() {
394 | StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
395 | StrictMode.setVmPolicy(builder.build());
396 | Intent intent = new Intent();
397 | intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
398 |
399 | Date date = new Date();
400 | DateFormat df = new SimpleDateFormat("-mm-ss");
401 |
402 | String newPicFile = df.format(date) + ".jpg";
403 | String outPath = "/sdcard/" + newPicFile;
404 | File outFile = new File(outPath);
405 |
406 | mCameraFileName = outFile.toString();
407 | Uri outuri = Uri.fromFile(outFile);
408 | intent.putExtra(MediaStore.EXTRA_OUTPUT, outuri);
409 | startActivityForResult(intent, CAMERA_REQUEST);
410 | }
411 |
412 | public void hideKeyboard() {
413 | InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
414 | View view = getCurrentFocus();
415 | if (view == null) {
416 | view = new View(this);
417 | }
418 | if (imm != null) {
419 | imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
420 | }
421 | }
422 |
423 | private void showResultCount(int count) {
424 | String str;
425 | if (count == 0) {
426 | str = getString(R.string.no_result_found);
427 | } else {
428 | str = getString(R.string.result_count, String.valueOf(count));
429 | }
430 |
431 | showToast(str);
432 | }
433 |
434 | private void showLoadingDialog(){
435 | if (spotsDialog != null) {
436 | hideLoadingDialog();
437 | }
438 |
439 | spotsDialog = new SpotsDialog.Builder().setContext(this)
440 | .setMessage(getString(R.string.please_wait))
441 | .setCancelable(false)
442 | .build();
443 | spotsDialog.show();
444 | }
445 |
446 | private void hideLoadingDialog() {
447 | if (spotsDialog != null) {
448 | spotsDialog.dismiss();
449 | }
450 |
451 | spotsDialog = null;
452 | }
453 |
454 | private void showToast(final String str) {
455 | runOnUiThread(new Runnable() {
456 | @Override
457 | public void run() {
458 | Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG)
459 | .show();
460 | }
461 | });
462 | }
463 |
464 | private Rect convertRect(Rect rect, float mlImageWidth, float mlImageHeight) {
465 | if (imageView.getWidth() == 0 || imageView.getHeight() == 0) {
466 | return new Rect(0, 0, 0, 0);
467 | }
468 |
469 | float scaleW = (float)mlImageWidth / (float)imageView.getWidth();
470 | float scaleH = (float)mlImageHeight / (float)imageView.getHeight();
471 | int left = scaleRect(rect.left, scaleW);
472 | int top = scaleRect(rect.top, scaleH);
473 | int right = scaleRect(rect.right, scaleW);
474 | int bottom = scaleRect(rect.bottom, scaleH);
475 |
476 | return new Rect(left, top, right, bottom);
477 | }
478 |
479 | private static int scaleRect(float px, float scale){
480 | return (int)(px/scale);
481 | }
482 | }
483 |
--------------------------------------------------------------------------------