LISTING PROGRAM
1.
splash_screen.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@drawable/splash_background">
<ImageView
android:layout_width="179dp" android:layout_height="264dp" android:id="@+id/imageView"
android:src="@drawable/splash_logo" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_gravity="center" android:layout_marginTop="140dp" android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content" android:layout_height="0dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="RIZKY RIVANNI | 121401072 | PROGRAM STUDI S-1 ILMU KOMPUTER”
android:id="@+id/textView"
android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:height="20dp"
android:layout_marginTop="150dp" android:textStyle="bold"
android:textSize="10dp" android:textColor="#ffffff" android:singleLine="false" android:paddingLeft="0dp"
android:layout_gravity="center_horizontal" android:layout_weight="1"/>
</LinearLayout>
2.
SplashScreen.java
import android.app.Activity; import android.content.Intent; import android.os.Bundle;
public class SplashScreen extends Activity {
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
Thread timerThread = new Thread() { public void run() {
try { sleep(5000); }
catch (InterruptedException e) { e.printStackTrace(); } finally {
Intent intent = new Intent(SplashScreen.this,MainActivity.class); startActivity(intent);
} } };
protected void onPause() { super.onPause();
finish(); }
}
3.
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical" android:weightSum="6">
<Button
android:id="@+id/button"
android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="4"
android:text="OPEN IMAGE" android:background="#005f94" android:textColor="#FFF" android:textSize="17dp" android:typeface="sans"
android:layout_above="@+id/button2" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"/>
<Button
android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="5"
android:text="GRAYSCALE" android:id="@+id/button2" android:background="#005f94" android:backgroundTint="#fdfdfd" android:textColor="#005f94" android:typeface="sans" android:textSize="17dp"
android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"/>
<ImageView
android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/imageView2"
android:src="@drawable/button_photo" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_above="@+id/button" android:layout_below="@+id/textView2" android:layout_alignParentRight="true" android:layout_alignParentEnd="true"/>
<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" android:text="OCR ME"
android:id="@+id/textView2"
android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:textSize="18dp"
android:textIsSelectable="false" android:password="false"
android:paddingLeft="15dp" android:height="50dp"
android:background="#005f94" android:textColor="#ffffff" android:typeface="sans"
style="@android:style/TextAppearance.DeviceDefault.DialogWindowTitle" android:linksClickable="false"
android:shadowColor="#060606" android:singleLine="false" android:textStyle="normal|bold" android:autoText="true"/>
<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView3"
android:layout_above="@+id/button" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"/>
</RelativeLayout>
4.
MainActivity.java
import java.io.ByteArrayOutputStream; import java.io.File;
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.app.Activity; import android.app.AlertDialog;
import android.content.ActivityNotFoundException; import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.AssetManager; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.ColorFilter; import android.graphics.Matrix;
import android.graphics.drawable.Drawable; import android.media.ExifInterface; import android.media.Image;
import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.util.Log;
import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast;
import com.googlecode.tesseract.android.TessBaseAPI;
public class MainActivity extends Activity {
static final int CAMERA_CAPTURE = 1;
final int PIC_CROP = 3;
final int PICK_IMAGE_REQUEST = 2;
private Uri picUri;
String mCurrentPhotoPath;
public static final String PACKAGE_NAME = "com.android.me.ocrme"; public static final String DATA_PATH =
Environment.getExternalStorageDirectory().toString() + "/SimpleAndroidOCR/";
public static final String lang = "ind";
protected Button _button ,_button2;
protected String _path;
protected TextView _textView3; protected boolean _taken; protected Bitmap bitmap;
protected static final String PHOTO_TAKEN = "photo_taken"; protected ImageView _imgview;
protected int phase;
public void onCreate(Bundle savedInstanceState) {
String[] paths = new String[] { DATA_PATH, DATA_PATH + "tessdata/" };
for ( String path : paths ) { File dir = new File(path); if ( !dir.exists() ) { if ( !dir.mkdirs() ) {
Log.v(TAG, "ERROR: Creation of directory " + path + " on sdcard failed");
return;
} else { Log.v(TAG, "Created directory " + path + " on sdcard" ); } }
}
if ( !(new File(DATA_PATH + "tessdata/" + lang + ".traineddata")).exists() ) { try {
AssetManager assetManager = getAssets();
InputStream in = assetManager.open("tessdata/"+ lang + ".traineddata");
OutputStream out = new FileOutputStream(DATA_PATH + "tessdata/" + lang + ".traineddata");
// Transfer bytes from in to out byte[] buf = new byte[1024]; int len;
while ( (len = in.read(buf)) > 0 ) { out.write(buf, 0, len);
}
in.close();
out.close();
Log.v(TAG, "Copied " + lang + " traineddata"); } catch (IOException e) {
Log.e(TAG, "Was unable to copy " + lang + " traineddata " + e.toString());
} }
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
_button = (Button) findViewById(R.id.button); _button2 = (Button) findViewById(R.id.button2); _textView3 = (TextView) findViewById(R.id.textView3); _button.setOnClickListener(new ButtonClickHandler()); _button2.setOnClickListener(new ButtonNextHandler()); _imgview = (ImageView)findViewById(R.id.imageView2); _path = DATA_PATH + "/ocr.jpg";
}
public class ButtonClickHandler implements View.OnClickListener { public void onClick(View view) {
final CharSequence[] options = { "Take from Camera", "Select from Gallery" }; AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("Select Image");
builder.setItems(options, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) { if ( options[item].equals("Take from Camera") ) { try {
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); String imageFilePath = Environment.getExternalStorageDirectory(). getAbsolutePath() + "/picture.jpg";
takePicture.putExtra(MediaStore.EXTRA_OUTPUT, picUri); startActivityForResult(takePicture, CAMERA_CAPTURE); } catch (ActivityNotFoundException anfe) {
String errorMessage = "Whoops - your device doesn't support capturing images!";
Toast.makeText(MainActivity.this, errorMessage, Toast.LENGTH_SHORT).show(); }
} else if ( options[item].equals("Select from Gallery") ) { Intent gallery = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(gallery, PICK_IMAGE_REQUEST);
} } });
builder.show(); }
}
public class ButtonNextHandler implements View.OnClickListener { public void onClick(View view) {
if ( phase == 0 ) {
bitmap = AdaptiveTresholding.getGrayscaleIMG(bitmap); _imgview.setImageBitmap(bitmap);
_button2.setText("ADAPTIVE THRESHOLDING"); phase++;
} else if ( phase == 1 ) {
bitmap = AdaptiveTresholding.getTresholdIMG(bitmap); _imgview.setImageBitmap(bitmap);
_button2.setText("TESSERACT OCR"); phase++;
} else {
TessBaseAPI baseApi = new TessBaseAPI(); baseApi.setDebug(true);
baseApi.init(DATA_PATH, lang); baseApi.setImage(bitmap);
String recognizedText = baseApi.getUTF8Text(); baseApi.end();
if ( lang.equalsIgnoreCase("ind") ) {
recognizedText = recognizedText.replaceAll("[^a-zA-Z0-9]+", " "); }
recognizedText = recognizedText.trim();
if ( recognizedText.length() == 0 ) { recognizedText = "Teks tidak dikenali"; }
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).create(); dialog.setTitle("HASIL TEKS KEMASAN");
dialog.setMessage(recognizedText); dialog.show();
} } }
protected void onActivityResult(int requestCode, int resultCode, Intent data) { if ( resultCode == RESULT_OK ) {
if ( requestCode == CAMERA_CAPTURE ) { Uri uri = picUri;
performCrop();
Log.d("picUri", uri.toString());
} else if ( requestCode == PICK_IMAGE_REQUEST ) { picUri = data.getData();
Log.d("uriGallery", picUri.toString()); performCrop();
} else if ( requestCode == PIC_CROP ) { Bundle extras = data.getExtras();
Bitmap thePic = (Bitmap) extras.get("data"); bitmap = thePic;
} } }
private void performCrop() { try {
Intent cropIntent = new Intent("com.android.camera.action.CROP"); cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("crop", "true"); cropIntent.putExtra("return-data", true); startActivityForResult(cropIntent, PIC_CROP); } catch (ActivityNotFoundException anfe) {
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT); toast.show();
} } }
5.
AdaptiveTresholding.java
import android.graphics.Bitmap; import android.graphics.Color; import android.util.Log;
public class AdaptiveTresholding {
private static Bitmap grayscaleIMG; private static Bitmap tresholdIMG;
public static Bitmap getGrayscaleIMG(Bitmap originalIMG) { grayscaleIMG = originalIMG;
for ( int x = 0; x < grayscaleIMG.getWidth(); x++ ) { for ( int y = 0; y < grayscaleIMG.getHeight(); y++ ) { int pixel = grayscaleIMG.getPixel(x,y);
int R = (pixel & 0xff0000) >> 16; int G = (pixel & 0x00ff00) >> 8; int B = (pixel & 0x0000ff) >> 0;
int gray = (int)(0.2126 * R + 0.7152 * G + 0.0722 * B); grayscaleIMG.setPixel(x, y, Color.rgb(gray, gray, gray)); }
}
return grayscaleIMG; }
public static Bitmap getTresholdIMG(Bitmap originalIMG) { tresholdIMG = originalIMG;
int n = 5; //Jumlah Pembagian int jumlah = 0; //Total Mean int total = 0; //Total Sementara
int Timage = 0; //Variabel Nilai Threshold
int max = 0; //Max Intensitas Citra int min = 255; //Min Intensitas Citra int varianssub = 0; //Varians Sub Citra int ratasub = 0; //Rata2 Sub Citra int maxsub = 0; //Max Sub Citra int minsub = 255; //Min Sub Citra
int xn = (int) Math.floor(tresholdIMG.getWidth()/n); int yn = (int) Math.floor(tresholdIMG.getHeight()/n);
for ( int x = 0; x < (n-1)*xn+xn; x++ ) { for ( int y = 0; y < (n-1)*yn+yn; y++ ) { int pixel = tresholdIMG.getPixel(x, y); int R = (int) (pixel & 0xff0000)>>16; if ( max < R ) max = R;
if ( min > R ) min = R; }
for ( int j = 0;j<n; j++ ) { for ( int i = 0;i<n; i++ ) { varianssub = 0;
jumlah = 0; total = 0;
for ( int y = j * yn; y< j * yn + yn; y++ ) { for ( int x = i * xn; x< i * xn + xn; x++ ) { int pixel = tresholdIMG.getPixel(x, y); int R = (int) (pixel & 0xff0000)>>16; if ( maxsub < R ) maxsub = R;
if ( minsub > R ) minsub = R; total += R;
jumlah++; }
}
ratasub = ( total/jumlah ); jumlah = 0;
total = 0;
//Perhitungan Nilai Varians
for ( int y = j * yn; y< j * yn + yn; y++ ) { for ( int x = i * xn; x< i * xn + xn; x++ ) { int pixel = tresholdIMG.getPixel(x, y); int R = (int) (pixel & 0xff0000)>>16; total += (R-ratasub)*(R-ratasub); jumlah++;
} }
varianssub = total/(jumlah-1);
for ( int y = j* yn; y< j * yn + yn; y++ ) { for ( int x = i*xn; x< i*xn + xn; x++ ) { int pixel = tresholdIMG.getPixel(x, y); int R = (int) (pixel & 0xff0000)>>16; if ( varianssub > 100 ) {
Timage = ((maxsub - minsub)/2); if ( R < Timage ) {
tresholdIMG.setPixel(x, y, Color.BLACK); } else {
tresholdIMG.setPixel(x, y, Color.WHITE); }
} else {
Timage = ((max - min)/2); if ( R < Timage ) {
tresholdIMG.setPixel(x, y, Color.BLACK); } else {
tresholdIMG.setPixel(x, y, Color.WHITE); }
} } } } }
return tresholdIMG; }
}
6.
AndroidManifest.xml
<manifest
xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.me.ocrme">
<application
android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:debuggable="true"
<activity
android:name=".SplashScreen" android:label="@string/app_name"> <intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
</activity>
<activity
android:configChanges="orientation|keyboardHidden" android:label="@string/app_name"
android:name=".MainActivity" > <intent-filter>
<action android:name="com.android.me.MAINACTIVITY" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter>
</activity> </application>
<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" android:required="true" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />