Skip to content

Commit bd6d8b9

Browse files
committed
Cleaned-up version
1 parent 6ce7b1c commit bd6d8b9

4 files changed

Lines changed: 72 additions & 67 deletions

File tree

SimpleScan/AndroidManifest.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
android:allowBackup="true"
2222
android:icon="@drawable/ic_launcher"
2323
android:label="@string/app_name"
24-
android:theme="@style/AppTheme" >
24+
android:theme="@style/AppTheme" >
25+
<!--
2526
<activity
2627
android:name=".SplashWelcome"
2728
android:label="@string/app_name" >
@@ -31,7 +32,7 @@
3132
<category android:name="android.intent.category.LAUNCHER" />
3233
</intent-filter>
3334
</activity>
34-
35+
-->
3536
<activity
3637
android:name=".OCR"
3738
android:label="@string/ocr_name" >

SimpleScan/bin/AndroidManifest.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
android:allowBackup="true"
2222
android:icon="@drawable/ic_launcher"
2323
android:label="@string/app_name"
24-
android:theme="@style/AppTheme" >
24+
android:theme="@style/AppTheme" >
25+
<!--
2526
<activity
2627
android:name=".SplashWelcome"
2728
android:label="@string/app_name" >
@@ -31,7 +32,7 @@
3132
<category android:name="android.intent.category.LAUNCHER" />
3233
</intent-filter>
3334
</activity>
34-
35+
-->
3536
<activity
3637
android:name=".OCR"
3738
android:label="@string/ocr_name" >

SimpleScan/res/layout/main.xml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,20 @@
1010
android:layout_weight="6"
1111
android:gravity="center_horizontal|center_vertical">
1212

13-
<ImageView android:id="@+id/image"
14-
android:layout_width="fill_parent"
15-
android:layout_height="wrap_content"/>
13+
<ImageView
14+
android:id="@+id/image"
15+
android:layout_width="fill_parent"
16+
android:layout_height="wrap_content"
17+
android:gravity="center_horizontal"/>
1618

1719
<TextView
1820
android:id="@+id/field"
1921
android:layout_width="fill_parent"
2022
android:layout_height="wrap_content"
23+
android:textSize="25dp"
24+
android:layout_below="@id/image"
2125
android:layout_alignParentBottom="true"
2226
android:layout_alignParentLeft="true"
23-
android:layout_marginBottom="28dp"
2427
android:gravity="center_horizontal"
2528
android:text="No Image" />
2629

SimpleScan/src/com/SimpleScan/simplescan/OCR.java

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ public class OCR extends Activity {
3131
protected ImageView _image;
3232
protected TextView _field;
3333
protected String _appPath = Environment.getExternalStorageDirectory() + "/SimpleScan";
34-
protected String _path;
34+
protected String _ImgDirPath = Environment.getExternalStorageDirectory() + "/SimpleScan/images";
35+
protected String _ImgPath = Environment.getExternalStorageDirectory() + "/SimpleScan/images/recipt_image.jpg";
3536
protected String _tessPath = Environment.getExternalStorageDirectory() + "/SimpleScan/tesseract";
36-
protected String _traindataPath;
37+
protected String _traindataPath = Environment.getExternalStorageDirectory() + "/SimpleScan/tesseract/tessdata";
3738
protected boolean _taken;
3839

3940
protected static final String PHOTO_TAKEN = "photo_taken";
@@ -54,58 +55,52 @@ public void onCreate(Bundle savedInstanceState)
5455
if(!app_dir.exists()) {
5556
if(app_dir.mkdir()) {
5657
Log.i( "app_dir", "app_dir made" );
57-
CopyAssets();
58-
}
59-
else Log.i( "app_dir", "app_dir could not be made" );
60-
}
61-
else Log.i( "app_dir", "app_dir exists" );
62-
63-
64-
//File im_direct = new File( Environment.getExternalStorageDirectory() + "/photo_capture/images" );
65-
File im_direct = new File( Environment.getExternalStorageDirectory() + "/SimpleScan/images" );
66-
if(!im_direct.exists()) {
67-
if(im_direct.mkdir()) {
58+
59+
File im_direct = new File( Environment.getExternalStorageDirectory() + "/SimpleScan/images" );
60+
if(!im_direct.exists()) {
61+
if(im_direct.mkdir()) {
62+
63+
}
64+
}
65+
66+
File tess_dir = new File(_tessPath);
67+
if(!tess_dir.exists()) {
68+
if(tess_dir.mkdir()) {
69+
Log.i( "tess_dir", "tess_dir made" );
70+
71+
File traindata_direct = new File(_traindataPath);
72+
if(!traindata_direct.exists()) {
73+
if(traindata_direct.mkdir()) {
74+
Log.i( "traindata", "directory made" );
75+
CopyAssets();
76+
}
77+
else Log.i( "traindata", "traindata_direct could not be made" );
78+
}
79+
else Log.i( "traindata", "traindata_direct exists" );
80+
81+
}
82+
else Log.i( "tess_dir", "tess_dir could not be made" );
83+
}
84+
else Log.i( "tess_dir", "tess_dir exists" );
6885

6986
}
87+
else Log.i( "app_dir", "app_dir could not be made" );
7088
}
71-
//_path = Environment.getExternalStorageDirectory() + "/photo_capture/images/make_machine_example.jpg";
72-
_path = Environment.getExternalStorageDirectory() + "/SimpleScan/images/recipt_image.jpg";
73-
74-
75-
File tess_dir = new File(_tessPath);
76-
if(!tess_dir.exists()) {
77-
if(tess_dir.mkdir()) {
78-
Log.i( "tess_dir", "tess_dir made" );
79-
}
80-
else Log.i( "tess_dir", "tess_dir could not be made" );
81-
}
82-
else Log.i( "tess_dir", "tess_dir exists" );
83-
84-
//_traindataPath = Environment.getExternalStorageDirectory() + "/photo_capture/tesseract/tessdata";
85-
_traindataPath = Environment.getExternalStorageDirectory() + "/SimpleScan/tesseract/tessdata";
86-
File traindata_direct = new File(_traindataPath);
87-
if(!traindata_direct.exists()) {
88-
if(traindata_direct.mkdir()) {
89-
Log.i( "traindata", "directory made" );
90-
CopyAssets();
91-
}
92-
else Log.i( "traindata", "traindata_direct could not be made" );
93-
}
94-
else Log.i( "traindata", "traindata_direct exists" );
89+
else Log.i( "app_dir", "app_dir exists" );
9590
}
9691

9792
public class ButtonClickHandler implements View.OnClickListener
9893
{
9994
public void onClick( View view ){
100-
Log.i("MakeMachine", "ButtonClickHandler.onClick()" );
95+
Log.i("xml Button", "ButtonClickHandler.onClick()" );
10196
startCameraActivity();
10297
}
10398
}
10499

105100
protected void startCameraActivity()
106101
{
107-
Log.i("MakeMachine", "startCameraActivity()" );
108-
File file = new File( _path );
102+
Log.i("native_camApp", "startCameraActivity()" );
103+
File file = new File( _ImgPath );
109104
Uri outputFileUri = Uri.fromFile( file );
110105

111106
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
@@ -118,11 +113,11 @@ protected void startCameraActivity()
118113
@Override
119114
protected void onActivityResult(int requestCode, int resultCode, Intent data)
120115
{
121-
Log.i( "MakeMachine", "resultCode: " + resultCode );
116+
Log.i( "result_from_camera", "resultCode: " + resultCode );
122117
switch( resultCode )
123118
{
124119
case 0:
125-
Log.i( "MakeMachine", "User cancelled" );
120+
Log.i( "result_from_camera", "User cancelled" );
126121
break;
127122

128123
case -1:
@@ -138,18 +133,25 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data)
138133

139134
protected void onPhotoTaken() throws IOException
140135
{
141-
Log.i( "MakeMachine", "onPhotoTaken" );
136+
Log.i( "photo_taken", "onPhotoTaken" );
142137

143138
_taken = true;
144139

145140
BitmapFactory.Options options = new BitmapFactory.Options();
146141
options.inSampleSize = 4; //down-sampling the image
147142

148-
Bitmap bitmap = BitmapFactory.decodeFile( _path, options ); //create a bitmap from _path with options
143+
Bitmap bitmap = BitmapFactory.decodeFile( _ImgPath, options ); //create a bitmap from _ImgPath with options
144+
bitmap = correct_photo(bitmap);
145+
146+
_image.setImageBitmap(bitmap); //Assign the bitmap to ImageView
147+
String recognizedText = detect_text(bitmap);
149148

149+
_field.setText(recognizedText);
150150

151-
//correcting photo for OCR(tess-two)
152-
ExifInterface exif = new ExifInterface( _path );
151+
}
152+
153+
protected Bitmap correct_photo(Bitmap bitmap) throws IOException { //correcting photo for OCR(tess-two)
154+
ExifInterface exif = new ExifInterface( _ImgPath );
153155
int exifOrientation = exif.getAttributeInt(
154156
ExifInterface.TAG_ORIENTATION,
155157
ExifInterface.ORIENTATION_NORMAL);
@@ -181,33 +183,30 @@ protected void onPhotoTaken() throws IOException
181183
}
182184
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
183185

184-
185-
_image.setImageBitmap(bitmap); //Assign the bitmap to ImageView
186-
187-
//_field.setVisibility( View.GONE );
188-
189-
186+
return bitmap;
187+
}
188+
189+
protected String detect_text(Bitmap bitmap){
190190
TessBaseAPI baseApi = new TessBaseAPI();
191-
Log.i("tessrect", "new tess object created");
192-
//baseApi.init(Environment.getExternalStorageDirectory() + "/photo_capture/tesseract/", "eng");
191+
Log.i("tessrect", "new tess object created");
193192
baseApi.init(Environment.getExternalStorageDirectory() + "/SimpleScan/tesseract/", "eng");
194193
Log.i("tessrect", "initialized");
195194
// Eg. baseApi.init("/mnt/sdcard/tesseract/tessdata/eng.traineddata", "eng");
195+
196196
baseApi.setImage(bitmap);
197197
Log.i("tessrect", "bitmap/image set");
198+
198199
String recognizedText = baseApi.getUTF8Text();
199-
//Log.d("tessrect", recognizedText);
200-
_field.setText(recognizedText);
201-
200+
202201
baseApi.end();
203202

203+
return recognizedText;
204204
}
205-
206205

207206
@Override
208207
protected void onRestoreInstanceState( Bundle savedInstanceState){ //When the rotation is complete
209208
//restore from Bundle object
210-
Log.i( "MakeMachine", "onRestoreInstanceState()");
209+
Log.i( "restoreFrom_bundle", "onRestoreInstanceState()");
211210
if( savedInstanceState.getBoolean( OCR.PHOTO_TAKEN ) ) {
212211
try {
213212
onPhotoTaken();
@@ -251,6 +250,7 @@ private void CopyAssets() {
251250
}
252251
}
253252
}
253+
254254
private void copyFile(InputStream in, OutputStream out) throws IOException {
255255
byte[] buffer = new byte[1024];
256256
int read;

0 commit comments

Comments
 (0)