OCR Configurable  1.0
 Todo Clases Namespaces Archivos Funciones Variables
OcrRecognizeAsyncTask.java
Ir a la documentación de este archivo.
00001 /*
00002  * Copyright 2011 Robert Theis
00003  * Copyright 2012 Jaime Navarro Santapau
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 package edu.uoc.ocr;
00018 
00019 
00027 import com.googlecode.leptonica.android.ReadFile;
00028 import com.googlecode.tesseract.android.TessBaseAPI;
00029 
00030 import edu.uoc.ocr.R;
00031 
00032 import android.graphics.Bitmap;
00033 import android.os.AsyncTask;
00034 import android.os.Handler;
00035 import android.os.Message;
00036 import android.util.Log;
00037 
00038 
00046 final class OcrRecognizeAsyncTask extends AsyncTask<Void, Void, Boolean> {
00047 
00048         private CaptureActivityForOcr activity;
00049         private TessBaseAPI baseApi;
00050         private byte[] data;
00051         private int width;
00052         private int height;
00053         private OcrResult ocrResult;
00054         private long timeRequired;
00055 
00056         OcrRecognizeAsyncTask(CaptureActivityForOcr activity, TessBaseAPI baseApi,
00057                         byte[] data, int width, int height) {
00058                 this.activity = activity;
00059                 this.baseApi = baseApi;
00060                 this.data = data;
00061                 this.width = width;
00062                 this.height = height;
00063         }
00064 
00065         @Override
00066         protected Boolean doInBackground(Void... arg0) {
00067                 long start = System.currentTimeMillis();
00068                 Bitmap bitmap = activity.getCameraManager()
00069                                 .buildLuminanceSource(data, width, height)
00070                                 .renderCroppedGreyscaleBitmap();
00071                 String textResult;
00072 
00073                 try {
00074                         baseApi.setImage(ReadFile.readBitmap(bitmap));
00075                         textResult = baseApi.getUTF8Text();
00076                         timeRequired = System.currentTimeMillis() - start;
00077 
00078                         // Check for failure to recognize text
00079                         if (textResult == null || textResult.equals("")) {
00080                                 return false;
00081                         }
00082                         ocrResult = new OcrResult();
00083                         ocrResult.setWordConfidences(baseApi.wordConfidences());
00084                         ocrResult.setMeanConfidence(baseApi.meanConfidence());
00085                         ocrResult.setRegionBoundingBoxes(baseApi.getRegions().getBoxRects());
00086                         ocrResult.setWordBoundingBoxes(baseApi.getWords().getBoxRects());
00087 
00088                 } catch (RuntimeException e) {
00089                         Log.e("OcrRecognizeAsyncTask",
00090                                         "Caught RuntimeException in request to Tesseract. Setting state to CONTINUOUS_STOPPED.");
00091                         e.printStackTrace();
00092                         try {
00093                                 baseApi.clear();
00094                                 activity.stopHandler();
00095                         } catch (NullPointerException e1) {
00096                                 // Continue
00097                         }
00098                         return false;
00099                 }
00100                 timeRequired = System.currentTimeMillis() - start;
00101                 ocrResult.setBitmap(bitmap);
00102                 ocrResult.setText(textResult);
00103                 ocrResult.setRecognitionTimeRequired(timeRequired);
00104                 return true;
00105         }
00106 
00107         @Override
00108         protected void onPostExecute(Boolean result) {
00109                 super.onPostExecute(result);
00110 
00111                 Handler handler = activity.getHandler();
00112                 if (handler != null) {
00113                         // Send results for single-shot mode recognition.
00114                         if (result) {
00115                                 Message message = Message.obtain(handler,
00116                                                 R.id.ocr_decode_succeeded, ocrResult);
00117                                 message.sendToTarget();
00118                         } else {
00119                                 Message message = Message.obtain(handler,
00120                                                 R.id.ocr_decode_failed, ocrResult);
00121                                 message.sendToTarget();
00122                         }
00123                         activity.getProgressDialog().dismiss();
00124                 }
00125                 if (baseApi != null) {
00126                         baseApi.clear();
00127                 }
00128         }
00129 }
 Todo Clases Namespaces Archivos Funciones Variables