Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.19 KB | None | 0 0
  1. package com.osk133.arnthai;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.content.Intent;
  5. import android.content.res.AssetManager;
  6. import android.graphics.Bitmap;
  7. import android.graphics.Color;
  8. import android.os.Build;
  9. import android.provider.MediaStore;
  10. import android.speech.tts.TextToSpeech;
  11. import android.support.v7.app.AppCompatActivity;
  12. import android.os.Bundle;
  13. import android.util.Log;
  14. import android.view.View;
  15. import android.widget.Button;
  16. import android.widget.CheckBox;
  17. import android.widget.ImageView;
  18. import android.widget.TextView;
  19. import android.widget.Toast;
  20.  
  21. import com.googlecode.tesseract.android.TessBaseAPI;
  22.  
  23. import java.io.File;
  24. import java.io.FileNotFoundException;
  25. import java.io.FileOutputStream;
  26. import java.io.IOException;
  27. import java.io.InputStream;
  28. import java.io.OutputStream;
  29. import java.util.Locale;
  30.  
  31. public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
  32.  
  33. private TextToSpeech textToSpeech;
  34.  
  35. public static final String TAG = "MainActivity";
  36.  
  37. Bitmap newBitmap;
  38. Bitmap captureImage;
  39.  
  40. TessBaseAPI mTess; //Tess API reference
  41.  
  42. String datapath = ""; //path to folder containing language data file
  43.  
  44. Button btnOcr;
  45. Button btnCapture;
  46. Button btnSpeak;
  47. String txtResult = "";
  48. ProgressDialog progressDialog;
  49. ImageView ivOcrPhoto;
  50. TextView ocrTextView;
  51. CheckBox cbOptimize;
  52.  
  53. private static final int TAKE_PICTURE = 100;
  54.  
  55.  
  56. @Override
  57. protected void onCreate(Bundle savedInstanceState) {
  58. super.onCreate(savedInstanceState);
  59. setContentView(R.layout.activity_main);
  60. textToSpeech = new TextToSpeech(this, this, "com.google.android.tts");
  61.  
  62.  
  63. Toast.makeText(MainActivity.this, "Start Activity", Toast.LENGTH_SHORT).show();
  64.  
  65. //Activity is created
  66.  
  67.  
  68. initInstances(); //Bind up views
  69.  
  70. btnOcr.setEnabled(false); //Disable OCR button
  71. btnOcr.setVisibility(View.INVISIBLE); //Hide OCR button
  72.  
  73. btnSpeak.setEnabled(false); //Disable speak button
  74. btnSpeak.setVisibility(View.INVISIBLE); //Hide speak button
  75.  
  76. //Capture photo button
  77. btnCapture.setOnClickListener(new View.OnClickListener() {
  78. @Override
  79. public void onClick(View v) {
  80. Toast.makeText(MainActivity.this, "Capture button is clicked", Toast.LENGTH_SHORT).show();
  81. //Launching camera app
  82. Intent intent = new Intent(
  83. MediaStore.ACTION_IMAGE_CAPTURE);
  84. startActivityForResult(intent, TAKE_PICTURE);
  85. }
  86. });
  87. } //End onCreate
  88.  
  89. //Getting photo
  90. @Override
  91. protected void onActivityResult(int requestCode, int resultCode,
  92. Intent data) {
  93. if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK) {
  94. Toast.makeText(MainActivity.this, "Capture photo successfully", Toast.LENGTH_SHORT).show();
  95.  
  96. captureImage = (Bitmap) data.getExtras().get("data");
  97.  
  98. // captureImage =Bitmap.createScaledBitmap(captureImage,1000,1000, true);
  99.  
  100. ivOcrPhoto.setImageBitmap(captureImage);
  101. btnOcr.setEnabled(true);
  102. btnOcr.setVisibility(View.VISIBLE);
  103. btnSpeak.setEnabled(true);
  104. btnSpeak.setVisibility(View.VISIBLE);
  105. } else {
  106. Toast.makeText(MainActivity.this, "Capture photo error, maybe you press cancel ?", Toast.LENGTH_SHORT).show();
  107.  
  108. }
  109.  
  110.  
  111. //Initialize image to OCR
  112. //image = BitmapFactory.decodeResource(getResources(), R.drawable.ssru);
  113.  
  114. datapath = getFilesDir() + "/tesseract/";
  115.  
  116. //make sure training data has been copied
  117. checkFile(new File(datapath + "tessdata/"));
  118.  
  119. //init Tesseract API
  120. String language = "tha";
  121.  
  122. mTess = new TessBaseAPI();
  123. mTess.init(datapath, language);
  124. btnOcr.setOnClickListener(new View.OnClickListener() {
  125.  
  126. //When OCR Button is clicked
  127. @Override
  128. public void onClick(View v) {
  129. if (cbOptimize.isChecked()) {
  130. Toast.makeText(MainActivity.this, "OCR button is clicked Starting optimizing image and OCR", Toast.LENGTH_SHORT).show();
  131.  
  132. } else {
  133. Toast.makeText(MainActivity.this, "OCR button is clicked Starting OCR", Toast.LENGTH_SHORT).show();
  134.  
  135. }
  136. btnOcr.setEnabled(false); //Disable button
  137. progressDialog.show(); //Show progress dialog
  138. new Thread(new Runnable() {
  139. public void run() {
  140. //Do OCR in background thread
  141. String OCRresult = null;
  142. if (cbOptimize.isChecked()) {
  143. newBitmap = setGrayscale(captureImage);
  144. newBitmap = removeNoise(newBitmap);
  145. } else {
  146. newBitmap = captureImage;
  147. }
  148.  
  149. mTess.setImage(newBitmap);
  150. Log.d(TAG, "Start OCR");
  151. OCRresult = mTess.getUTF8Text();
  152. txtResult = OCRresult.trim();
  153. ocrTextView.post(new Runnable() {
  154. public void run() {
  155. //When OCR is ready
  156. ivOcrPhoto.setImageBitmap(newBitmap);
  157.  
  158. ocrTextView.setText(txtResult); //Update text
  159. btnOcr.setEnabled(true); //Enable button
  160. progressDialog.dismiss(); //Close progress dialog
  161. }
  162. });
  163. }
  164. }).start();
  165. }
  166. });
  167.  
  168.  
  169. btnSpeak.setOnClickListener(new View.OnClickListener() {
  170. @Override
  171. public void onClick(View v) {
  172. speak(txtResult);
  173.  
  174. }
  175. });
  176.  
  177.  
  178. } // End onActivityResult
  179.  
  180.  
  181. // All views
  182. private void initInstances() {
  183. btnCapture = (Button) findViewById(R.id.btnCapture);
  184. btnOcr = (Button) findViewById(R.id.btnOcr);
  185. btnSpeak = (Button) findViewById(R.id.btnSpeak);
  186. ivOcrPhoto = (ImageView) findViewById(R.id.ivOcrPhoto);
  187. ocrTextView = (TextView) findViewById(R.id.tvOutput);
  188. cbOptimize = (CheckBox) findViewById(R.id.cbOptimize);
  189.  
  190. progressDialog = new ProgressDialog(this);
  191. progressDialog.setMessage("Loading...");
  192. progressDialog.setCanceledOnTouchOutside(false);
  193. progressDialog.setCancelable(false);
  194. }
  195.  
  196.  
  197. private void copyFiles() {
  198. try {
  199. //location we want the file to be at
  200. String filepath = datapath + "/tessdata/tha.traineddata";
  201.  
  202. //get access to AssetManager
  203. AssetManager assetManager = getAssets();
  204.  
  205. //open byte streams for reading/writing
  206. InputStream instream = assetManager.open("tessdata/tha.traineddata");
  207. OutputStream outstream = new FileOutputStream(filepath);
  208.  
  209. //copy the file to the location specified by filepath
  210. byte[] buffer = new byte[1024];
  211. int read;
  212. while ((read = instream.read(buffer)) != -1) {
  213. outstream.write(buffer, 0, read);
  214. }
  215. outstream.flush();
  216. outstream.close();
  217. instream.close();
  218.  
  219. } catch (FileNotFoundException e) {
  220. e.printStackTrace();
  221. } catch (IOException e) {
  222. e.printStackTrace();
  223. }
  224.  
  225.  
  226. }
  227.  
  228. private void checkFile(File dir) {
  229. //directory does not exist, but we can successfully create it
  230. if (!dir.exists() && dir.mkdirs()) {
  231. copyFiles();
  232. }
  233. //The directory exists, but there is no data file in it
  234. if (dir.exists()) {
  235. String datafilepath = datapath + "/tessdata/tha.traineddata";
  236. File datafile = new File(datafilepath);
  237. if (!datafile.exists()) {
  238. copyFiles();
  239. }
  240. }
  241. }
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250. /*
  251. // Resize
  252. public Bitmap resize(Bitmap img, int newWidth, int newHeight) {
  253. Bitmap bmap = img.copy(img.getConfig(), true);
  254.  
  255. double nWidthFactor = (double) img.getWidth() / (double) newWidth;
  256. double nHeightFactor = (double) img.getHeight() / (double) newHeight;
  257.  
  258. double fx, fy, nx, ny;
  259. int cx, cy, fr_x, fr_y;
  260. int color1;
  261. int color2;
  262. int color3;
  263. int color4;
  264. byte nRed, nGreen, nBlue;
  265.  
  266. byte bp1, bp2;
  267.  
  268. for (int x = 0; x < bmap.getWidth(); ++x) {
  269. for (int y = 0; y < bmap.getHeight(); ++y) {
  270.  
  271. fr_x = (int) Math.floor(x * nWidthFactor);
  272. fr_y = (int) Math.floor(y * nHeightFactor);
  273. cx = fr_x + 1;
  274. if (cx >= img.getWidth())
  275. cx = fr_x;
  276. cy = fr_y + 1;
  277. if (cy >= img.getHeight())
  278. cy = fr_y;
  279. fx = x * nWidthFactor - fr_x;
  280. fy = y * nHeightFactor - fr_y;
  281. nx = 1.0 - fx;
  282. ny = 1.0 - fy;
  283.  
  284. color1 = img.getPixel(fr_x, fr_y);
  285. color2 = img.getPixel(cx, fr_y);
  286. color3 = img.getPixel(fr_x, cy);
  287. color4 = img.getPixel(cx, cy);
  288.  
  289. // Blue
  290. bp1 = (byte) (nx * Color.blue(color1) + fx * Color.blue(color2));
  291. bp2 = (byte) (nx * Color.blue(color3) + fx * Color.blue(color4));
  292. nBlue = (byte) (ny * (double) (bp1) + fy * (double) (bp2));
  293.  
  294. // Green
  295. bp1 = (byte) (nx * Color.green(color1) + fx * Color.green(color2));
  296. bp2 = (byte) (nx * Color.green(color3) + fx * Color.green(color4));
  297. nGreen = (byte) (ny * (double) (bp1) + fy * (double) (bp2));
  298.  
  299. // Red
  300. bp1 = (byte) (nx * Color.red(color1) + fx * Color.red(color2));
  301. bp2 = (byte) (nx * Color.red(color3) + fx * Color.red(color4));
  302. nRed = (byte) (ny * (double) (bp1) + fy * (double) (bp2));
  303.  
  304. bmap.setPixel(x, y, Color.argb(255, nRed, nGreen, nBlue));
  305. }
  306. }
  307.  
  308. bmap = setGrayscale(bmap);
  309. bmap = removeNoise(bmap);
  310.  
  311. return bmap;
  312. }
  313. */
  314.  
  315. // SetGrayscale
  316. private Bitmap setGrayscale(Bitmap img) {
  317. Bitmap bmap = img.copy(img.getConfig(), true);
  318. int c;
  319. for (int i = 0; i < bmap.getWidth(); i++) {
  320. for (int j = 0; j < bmap.getHeight(); j++) {
  321. c = bmap.getPixel(i, j);
  322. byte gray = (byte) (.299 * Color.red(c) + .587 * Color.green(c)
  323. + .114 * Color.blue(c));
  324.  
  325. bmap.setPixel(i, j, Color.argb(255, gray, gray, gray));
  326. }
  327. }
  328. return bmap;
  329. }
  330.  
  331. // RemoveNoise
  332. private Bitmap removeNoise(Bitmap bmap) {
  333. int black = 162;
  334.  
  335. for (int x = 0; x < bmap.getWidth(); x++) {
  336. for (int y = 0; y < bmap.getHeight(); y++) {
  337. int pixel = bmap.getPixel(x, y);
  338. if (Color.red(pixel) < black && Color.green(pixel) < black && Color.blue(pixel) < black) {
  339. bmap.setPixel(x, y, Color.BLACK);
  340. }
  341. }
  342. }
  343. for (int x = 0; x < bmap.getWidth(); x++) {
  344. for (int y = 0; y < bmap.getHeight(); y++) {
  345. int pixel = bmap.getPixel(x, y);
  346. if (Color.red(pixel) > black && Color.green(pixel) > black && Color.blue(pixel) > black) {
  347. bmap.setPixel(x, y, Color.WHITE);
  348. }
  349. }
  350. }
  351. return bmap;
  352. }
  353.  
  354. @Override
  355. public void onInit(int status) {
  356. //Make sure that TTS is OK
  357. if (status == TextToSpeech.SUCCESS) {
  358. textToSpeech.setLanguage(new Locale("th"));
  359.  
  360. }
  361. }
  362.  
  363. @Override
  364. protected void onDestroy() {
  365. super.onDestroy();
  366. textToSpeech.shutdown();
  367. }
  368.  
  369. private void speak(CharSequence message) {
  370. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  371. textToSpeech.speak(message, TextToSpeech.QUEUE_FLUSH, null, "");
  372. } else {
  373. textToSpeech.speak(message.toString(), TextToSpeech.QUEUE_FLUSH, null);
  374. }
  375. }
  376. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement