Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.osk133.arnthai;
- import android.app.ProgressDialog;
- import android.content.Intent;
- import android.content.res.AssetManager;
- import android.graphics.Bitmap;
- import android.graphics.Color;
- import android.os.Build;
- import android.provider.MediaStore;
- import android.speech.tts.TextToSpeech;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.googlecode.tesseract.android.TessBaseAPI;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.Locale;
- public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
- private TextToSpeech textToSpeech;
- public static final String TAG = "MainActivity";
- Bitmap newBitmap;
- Bitmap captureImage;
- TessBaseAPI mTess; //Tess API reference
- String datapath = ""; //path to folder containing language data file
- Button btnOcr;
- Button btnCapture;
- Button btnSpeak;
- String txtResult = "";
- ProgressDialog progressDialog;
- ImageView ivOcrPhoto;
- TextView ocrTextView;
- CheckBox cbOptimize;
- private static final int TAKE_PICTURE = 100;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- textToSpeech = new TextToSpeech(this, this, "com.google.android.tts");
- Toast.makeText(MainActivity.this, "Start Activity", Toast.LENGTH_SHORT).show();
- //Activity is created
- initInstances(); //Bind up views
- btnOcr.setEnabled(false); //Disable OCR button
- btnOcr.setVisibility(View.INVISIBLE); //Hide OCR button
- btnSpeak.setEnabled(false); //Disable speak button
- btnSpeak.setVisibility(View.INVISIBLE); //Hide speak button
- //Capture photo button
- btnCapture.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(MainActivity.this, "Capture button is clicked", Toast.LENGTH_SHORT).show();
- //Launching camera app
- Intent intent = new Intent(
- MediaStore.ACTION_IMAGE_CAPTURE);
- startActivityForResult(intent, TAKE_PICTURE);
- }
- });
- } //End onCreate
- //Getting photo
- @Override
- protected void onActivityResult(int requestCode, int resultCode,
- Intent data) {
- if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK) {
- Toast.makeText(MainActivity.this, "Capture photo successfully", Toast.LENGTH_SHORT).show();
- captureImage = (Bitmap) data.getExtras().get("data");
- // captureImage =Bitmap.createScaledBitmap(captureImage,1000,1000, true);
- ivOcrPhoto.setImageBitmap(captureImage);
- btnOcr.setEnabled(true);
- btnOcr.setVisibility(View.VISIBLE);
- btnSpeak.setEnabled(true);
- btnSpeak.setVisibility(View.VISIBLE);
- } else {
- Toast.makeText(MainActivity.this, "Capture photo error, maybe you press cancel ?", Toast.LENGTH_SHORT).show();
- }
- //Initialize image to OCR
- //image = BitmapFactory.decodeResource(getResources(), R.drawable.ssru);
- datapath = getFilesDir() + "/tesseract/";
- //make sure training data has been copied
- checkFile(new File(datapath + "tessdata/"));
- //init Tesseract API
- String language = "tha";
- mTess = new TessBaseAPI();
- mTess.init(datapath, language);
- btnOcr.setOnClickListener(new View.OnClickListener() {
- //When OCR Button is clicked
- @Override
- public void onClick(View v) {
- if (cbOptimize.isChecked()) {
- Toast.makeText(MainActivity.this, "OCR button is clicked Starting optimizing image and OCR", Toast.LENGTH_SHORT).show();
- } else {
- Toast.makeText(MainActivity.this, "OCR button is clicked Starting OCR", Toast.LENGTH_SHORT).show();
- }
- btnOcr.setEnabled(false); //Disable button
- progressDialog.show(); //Show progress dialog
- new Thread(new Runnable() {
- public void run() {
- //Do OCR in background thread
- String OCRresult = null;
- if (cbOptimize.isChecked()) {
- newBitmap = setGrayscale(captureImage);
- newBitmap = removeNoise(newBitmap);
- } else {
- newBitmap = captureImage;
- }
- mTess.setImage(newBitmap);
- Log.d(TAG, "Start OCR");
- OCRresult = mTess.getUTF8Text();
- txtResult = OCRresult.trim();
- ocrTextView.post(new Runnable() {
- public void run() {
- //When OCR is ready
- ivOcrPhoto.setImageBitmap(newBitmap);
- ocrTextView.setText(txtResult); //Update text
- btnOcr.setEnabled(true); //Enable button
- progressDialog.dismiss(); //Close progress dialog
- }
- });
- }
- }).start();
- }
- });
- btnSpeak.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- speak(txtResult);
- }
- });
- } // End onActivityResult
- // All views
- private void initInstances() {
- btnCapture = (Button) findViewById(R.id.btnCapture);
- btnOcr = (Button) findViewById(R.id.btnOcr);
- btnSpeak = (Button) findViewById(R.id.btnSpeak);
- ivOcrPhoto = (ImageView) findViewById(R.id.ivOcrPhoto);
- ocrTextView = (TextView) findViewById(R.id.tvOutput);
- cbOptimize = (CheckBox) findViewById(R.id.cbOptimize);
- progressDialog = new ProgressDialog(this);
- progressDialog.setMessage("Loading...");
- progressDialog.setCanceledOnTouchOutside(false);
- progressDialog.setCancelable(false);
- }
- private void copyFiles() {
- try {
- //location we want the file to be at
- String filepath = datapath + "/tessdata/tha.traineddata";
- //get access to AssetManager
- AssetManager assetManager = getAssets();
- //open byte streams for reading/writing
- InputStream instream = assetManager.open("tessdata/tha.traineddata");
- OutputStream outstream = new FileOutputStream(filepath);
- //copy the file to the location specified by filepath
- byte[] buffer = new byte[1024];
- int read;
- while ((read = instream.read(buffer)) != -1) {
- outstream.write(buffer, 0, read);
- }
- outstream.flush();
- outstream.close();
- instream.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private void checkFile(File dir) {
- //directory does not exist, but we can successfully create it
- if (!dir.exists() && dir.mkdirs()) {
- copyFiles();
- }
- //The directory exists, but there is no data file in it
- if (dir.exists()) {
- String datafilepath = datapath + "/tessdata/tha.traineddata";
- File datafile = new File(datafilepath);
- if (!datafile.exists()) {
- copyFiles();
- }
- }
- }
- /*
- // Resize
- public Bitmap resize(Bitmap img, int newWidth, int newHeight) {
- Bitmap bmap = img.copy(img.getConfig(), true);
- double nWidthFactor = (double) img.getWidth() / (double) newWidth;
- double nHeightFactor = (double) img.getHeight() / (double) newHeight;
- double fx, fy, nx, ny;
- int cx, cy, fr_x, fr_y;
- int color1;
- int color2;
- int color3;
- int color4;
- byte nRed, nGreen, nBlue;
- byte bp1, bp2;
- for (int x = 0; x < bmap.getWidth(); ++x) {
- for (int y = 0; y < bmap.getHeight(); ++y) {
- fr_x = (int) Math.floor(x * nWidthFactor);
- fr_y = (int) Math.floor(y * nHeightFactor);
- cx = fr_x + 1;
- if (cx >= img.getWidth())
- cx = fr_x;
- cy = fr_y + 1;
- if (cy >= img.getHeight())
- cy = fr_y;
- fx = x * nWidthFactor - fr_x;
- fy = y * nHeightFactor - fr_y;
- nx = 1.0 - fx;
- ny = 1.0 - fy;
- color1 = img.getPixel(fr_x, fr_y);
- color2 = img.getPixel(cx, fr_y);
- color3 = img.getPixel(fr_x, cy);
- color4 = img.getPixel(cx, cy);
- // Blue
- bp1 = (byte) (nx * Color.blue(color1) + fx * Color.blue(color2));
- bp2 = (byte) (nx * Color.blue(color3) + fx * Color.blue(color4));
- nBlue = (byte) (ny * (double) (bp1) + fy * (double) (bp2));
- // Green
- bp1 = (byte) (nx * Color.green(color1) + fx * Color.green(color2));
- bp2 = (byte) (nx * Color.green(color3) + fx * Color.green(color4));
- nGreen = (byte) (ny * (double) (bp1) + fy * (double) (bp2));
- // Red
- bp1 = (byte) (nx * Color.red(color1) + fx * Color.red(color2));
- bp2 = (byte) (nx * Color.red(color3) + fx * Color.red(color4));
- nRed = (byte) (ny * (double) (bp1) + fy * (double) (bp2));
- bmap.setPixel(x, y, Color.argb(255, nRed, nGreen, nBlue));
- }
- }
- bmap = setGrayscale(bmap);
- bmap = removeNoise(bmap);
- return bmap;
- }
- */
- // SetGrayscale
- private Bitmap setGrayscale(Bitmap img) {
- Bitmap bmap = img.copy(img.getConfig(), true);
- int c;
- for (int i = 0; i < bmap.getWidth(); i++) {
- for (int j = 0; j < bmap.getHeight(); j++) {
- c = bmap.getPixel(i, j);
- byte gray = (byte) (.299 * Color.red(c) + .587 * Color.green(c)
- + .114 * Color.blue(c));
- bmap.setPixel(i, j, Color.argb(255, gray, gray, gray));
- }
- }
- return bmap;
- }
- // RemoveNoise
- private Bitmap removeNoise(Bitmap bmap) {
- int black = 162;
- for (int x = 0; x < bmap.getWidth(); x++) {
- for (int y = 0; y < bmap.getHeight(); y++) {
- int pixel = bmap.getPixel(x, y);
- if (Color.red(pixel) < black && Color.green(pixel) < black && Color.blue(pixel) < black) {
- bmap.setPixel(x, y, Color.BLACK);
- }
- }
- }
- for (int x = 0; x < bmap.getWidth(); x++) {
- for (int y = 0; y < bmap.getHeight(); y++) {
- int pixel = bmap.getPixel(x, y);
- if (Color.red(pixel) > black && Color.green(pixel) > black && Color.blue(pixel) > black) {
- bmap.setPixel(x, y, Color.WHITE);
- }
- }
- }
- return bmap;
- }
- @Override
- public void onInit(int status) {
- //Make sure that TTS is OK
- if (status == TextToSpeech.SUCCESS) {
- textToSpeech.setLanguage(new Locale("th"));
- }
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- textToSpeech.shutdown();
- }
- private void speak(CharSequence message) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
- textToSpeech.speak(message, TextToSpeech.QUEUE_FLUSH, null, "");
- } else {
- textToSpeech.speak(message.toString(), TextToSpeech.QUEUE_FLUSH, null);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement