-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lucem-Anb
committed
Jul 25, 2018
0 parents
commit 0861c9e
Showing
22 changed files
with
660 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/libraries | ||
/.idea/modules.xml | ||
/.idea/workspace.xml | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild |
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Top-level build file where you can add configuration options common to all sub-projects/modules. | ||
|
||
buildscript { | ||
|
||
repositories { | ||
google() | ||
jcenter() | ||
} | ||
dependencies { | ||
classpath 'com.android.tools.build:gradle:3.1.3' | ||
|
||
|
||
// NOTE: Do not place your application dependencies here; they belong | ||
// in the individual module build.gradle files | ||
} | ||
} | ||
|
||
allprojects { | ||
repositories { | ||
google() | ||
jcenter() | ||
} | ||
} | ||
|
||
task clean(type: Delete) { | ||
delete rootProject.buildDir | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
apply plugin: 'com.android.library' | ||
|
||
android { | ||
compileSdkVersion 27 | ||
|
||
|
||
|
||
defaultConfig { | ||
minSdkVersion 7 | ||
targetSdkVersion 27 | ||
versionCode 1 | ||
versionName "1.0" | ||
|
||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | ||
|
||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
|
||
} | ||
|
||
dependencies { | ||
|
||
implementation fileTree(dir: 'libs', include: ['*.jar']) | ||
implementation 'com.android.support:appcompat-v7:27.1.1' | ||
testImplementation 'junit:junit:4.12' | ||
androidTestImplementation 'com.android.support.test:runner:1.0.2' | ||
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' | ||
implementation 'com.google.android.gms:play-services-vision:15.0.2' | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
26 changes: 26 additions & 0 deletions
26
...rscanner/src/androidTest/java/com/lucem/anb/characterscanner/ExampleInstrumentedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.lucem.anb.characterscanner; | ||
|
||
import android.content.Context; | ||
import android.support.test.InstrumentationRegistry; | ||
import android.support.test.runner.AndroidJUnit4; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Instrumented test, which will execute on an Android device. | ||
* | ||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a> | ||
*/ | ||
@RunWith(AndroidJUnit4.class) | ||
public class ExampleInstrumentedTest { | ||
@Test | ||
public void useAppContext() { | ||
// Context of the app under test. | ||
Context appContext = InstrumentationRegistry.getTargetContext(); | ||
|
||
assertEquals("com.lucem.anb.characterscanner.test", appContext.getPackageName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.lucem.anb.characterscanner" > | ||
<uses-permission android:name="android.permission.CAMERA"/> | ||
<uses-permission android:name="android.permission.INTERNET"/> | ||
</manifest> |
6 changes: 6 additions & 0 deletions
6
characterscanner/src/main/java/com/lucem/anb/characterscanner/DetectionsListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.lucem.anb.characterscanner; | ||
|
||
public interface DetectionsListener { | ||
void state(String state); | ||
void detections(String detections, long numbersLong, int numbersInt); | ||
} |
138 changes: 138 additions & 0 deletions
138
characterscanner/src/main/java/com/lucem/anb/characterscanner/Scanner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package com.lucem.anb.characterscanner; | ||
|
||
import android.Manifest; | ||
import android.app.Activity; | ||
import android.content.DialogInterface; | ||
import android.content.Intent; | ||
import android.content.IntentFilter; | ||
import android.content.pm.PackageManager; | ||
import android.net.Uri; | ||
import android.support.v4.app.ActivityCompat; | ||
import android.support.v7.app.AlertDialog; | ||
import android.util.Log; | ||
import android.util.SparseArray; | ||
import android.view.SurfaceHolder; | ||
import android.view.SurfaceView; | ||
import android.widget.Toast; | ||
|
||
import com.google.android.gms.vision.CameraSource; | ||
import com.google.android.gms.vision.Detector; | ||
import com.google.android.gms.vision.text.TextBlock; | ||
import com.google.android.gms.vision.text.TextRecognizer; | ||
|
||
import java.io.IOException; | ||
|
||
public class Scanner { | ||
|
||
private Activity activity; | ||
private static int REQUEST_CAMERA = 12; | ||
private static String LOG_TEXT = "SCANNER", state = "loading"; | ||
private SurfaceView camera; | ||
private DetectionsListener listener; | ||
private boolean showToasts = true; | ||
|
||
public Scanner(Activity activity, SurfaceView surfaceView, DetectionsListener listener){ | ||
this.activity = activity; | ||
this.camera = surfaceView; | ||
this.listener = listener; | ||
prepareScanning(); | ||
} | ||
|
||
private void prepareScanning(){ | ||
|
||
if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { | ||
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA); | ||
return; | ||
} | ||
TextRecognizer textRecognizer = new TextRecognizer.Builder(activity).build(); | ||
if (!textRecognizer.isOperational()) { | ||
|
||
IntentFilter lowStorageFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW); | ||
boolean lowStorage = activity.registerReceiver(null, lowStorageFilter) != null; | ||
if (lowStorage){ | ||
state = "You have low storage"; | ||
if (showToasts)Toast.makeText(activity, state, Toast.LENGTH_LONG).show(); | ||
Log.e(LOG_TEXT,state); | ||
listener.state(state); | ||
return; | ||
}else{ | ||
state = "OCR not ready"; | ||
if (showToasts)Toast.makeText(activity, state, Toast.LENGTH_LONG).show(); | ||
Log.e(LOG_TEXT,state); | ||
listener.state(state); | ||
return; | ||
} | ||
|
||
} | ||
final CameraSource cameraSource = new CameraSource.Builder(activity, textRecognizer) | ||
.setFacing(CameraSource.CAMERA_FACING_BACK) | ||
.setRequestedPreviewSize(1280, 1024) | ||
.setRequestedFps(2.0f) | ||
.setAutoFocusEnabled(true) | ||
.build(); | ||
camera.getHolder().addCallback(new SurfaceHolder.Callback() { | ||
@Override | ||
public void surfaceCreated(SurfaceHolder surfaceHolder) { | ||
try { | ||
if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { | ||
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA); | ||
return; | ||
} | ||
cameraSource.start(camera.getHolder()); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) { | ||
|
||
} | ||
|
||
@Override | ||
public void surfaceDestroyed(SurfaceHolder surfaceHolder) { | ||
cameraSource.stop(); | ||
} | ||
}); | ||
textRecognizer.setProcessor(new Detector.Processor<TextBlock>() { | ||
|
||
@Override | ||
public void release(){} | ||
|
||
@Override | ||
public void receiveDetections(Detector.Detections<TextBlock> detections) { | ||
|
||
state = "running"; | ||
listener.state(state); | ||
|
||
final SparseArray<TextBlock> items = detections.getDetectedItems(); | ||
if (items.size() != 0) { | ||
|
||
StringBuilder stringBuilder = new StringBuilder(); | ||
for (int i = 0; i < items.size(); ++i) { | ||
TextBlock item = items.valueAt(i); | ||
stringBuilder.append(item.getValue()); | ||
stringBuilder.append("\n"); | ||
} | ||
|
||
String detectedNumbers = stringBuilder.toString().replaceAll("[^0-9]", ""); | ||
listener.detections(stringBuilder.toString(), | ||
Long.parseLong(detectedNumbers), | ||
Integer.parseInt(detectedNumbers)); | ||
|
||
Log.d(LOG_TEXT, stringBuilder.toString()); | ||
} | ||
} | ||
}); | ||
|
||
} | ||
|
||
public void showToasts(boolean show){ | ||
showToasts = show; | ||
} | ||
|
||
public String getState(){ | ||
return state; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<resources> | ||
<string name="app_name">CharacterScanner</string> | ||
</resources> |
Oops, something went wrong.