OCR Configurable  1.0
 Todo Clases Namespaces Archivos Funciones Variables
CaptureActivityHandler.java
Ir a la documentación de este archivo.
00001 /*
00002  * Copyright (C) 2008 ZXing authors
00003  * Copyright 2011 Robert Theis
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 
00026 package edu.uoc.ocr;
00027 
00028 import edu.uoc.ocr.R;
00029 import edu.uoc.ocr.CaptureActivityForOcr;
00030 import edu.uoc.ocr.OcrResult;
00031 import edu.uoc.ocr.camera.CameraManager;
00032 
00033 import android.os.Handler;
00034 import android.os.Message;
00035 import android.util.Log;
00036 import android.view.Gravity;
00037 import android.widget.Toast;
00038 
00044 final class CaptureActivityHandler extends Handler {
00045 
00046         private static final String TAG = CaptureActivityHandler.class
00047                         .getSimpleName();
00048 
00049         private final CaptureActivityForOcr activity;
00050         private final DecodeThread decodeThread;
00051         private static State state;
00052         private final CameraManager cameraManager;
00053 
00054         private enum State {
00055                 PREVIEW, PREVIEW_PAUSED, SUCCESS, DONE
00056         }
00057 
00058         CaptureActivityHandler(CaptureActivityForOcr activity, CameraManager cameraManager) {
00059                 this.activity = activity;
00060                 this.cameraManager = cameraManager;
00061 
00062                 // Start ourselves capturing previews (and decoding if using continuous
00063                 // recognition mode).
00064                 cameraManager.startPreview();
00065 
00066                 decodeThread = new DecodeThread(activity);
00067                 decodeThread.start();
00068 
00069                 state = State.SUCCESS;
00070 
00071                 // Show the shutter and torch buttons
00072                 activity.setButtonVisibility(true);
00073 
00074                 restartOcrPreview();
00075 
00076         }
00077 
00078         @Override
00079         public void handleMessage(Message message) {
00080 
00081                 switch (message.what) {
00082                 case R.id.restart_preview:
00083                         restartOcrPreview();
00084                         break;
00085 
00086                 case R.id.ocr_decode_succeeded:
00087                         state = State.SUCCESS;
00088                         activity.setShutterButtonClickable(true);
00089                         activity.handleOcrDecode((OcrResult) message.obj);
00090                         break;
00091                 case R.id.ocr_decode_failed:
00092                         state = State.PREVIEW;
00093                         activity.setShutterButtonClickable(true);
00094                         Toast toast = Toast.makeText(activity.getBaseContext(),
00095                                         activity.getBaseContext().getString(R.string.dialogo_2),
00096                                         Toast.LENGTH_SHORT);
00097                         toast.setGravity(Gravity.TOP, 0, 0);
00098                         toast.show();
00099                         break;
00100                 }
00101         }
00102 
00103         void stop() {
00104                 // TODO See if this should be done by sending a quit message to
00105                 // decodeHandler as is done
00106                 // below in quitSynchronously().
00107                 Log.d(TAG, "Setting state to CONTINUOUS_PAUSED.");
00108                 removeMessages(R.id.ocr_decode);
00109         }
00110 
00111 
00112         void quitSynchronously() {
00113                 state = State.DONE;
00114                 if (cameraManager != null) {
00115                         cameraManager.stopPreview();
00116                 }
00117                 // Message quit = Message.obtain(decodeThread.getHandler(), R.id.quit);
00118                 try {
00119                         // quit.sendToTarget(); // This always gives
00120                         // "sending message to a Handler on a dead thread"
00121 
00122                         // Wait at most half a second; should be enough time, and onPause()
00123                         // will timeout quickly
00124                         decodeThread.join(500L);
00125                 } catch (InterruptedException e) {
00126                         Log.w(TAG, "Caught InterruptedException in quitSyncronously()", e);
00127                         // continue
00128                 } catch (RuntimeException e) {
00129                         Log.w(TAG, "Caught RuntimeException in quitSyncronously()", e);
00130                         // continue
00131                 } catch (Exception e) {
00132                         Log.w(TAG, "Caught unknown Exception in quitSynchronously()", e);
00133                 }
00134 
00135                 // Be absolutely sure we don't send any queued up messages
00136                 removeMessages(R.id.ocr_decode);
00137 
00138         }
00139 
00144         private void restartOcrPreview() {
00145                 // Display the shutter and torch buttons
00146                 activity.setButtonVisibility(true);
00147 
00148                 if (state == State.SUCCESS) {
00149                         state = State.PREVIEW;
00150 
00151                         // Draw the viewfinder.
00152                         activity.drawViewfinder();
00153                 }
00154         }
00155 
00156 
00160         private void ocrDecode() {
00161                 state = State.PREVIEW_PAUSED;
00162                 cameraManager.requestOcrDecode(decodeThread.getHandler(),R.id.ocr_decode);
00163         }
00164 
00168         void hardwareShutterButtonClick() {
00169                 // Ensure that we're not in continuous recognition mode
00170                 if (state == State.PREVIEW) {
00171                         ocrDecode();
00172                 }
00173         }
00174 
00179         void shutterButtonClick() {
00180                 // Disable further clicks on this button until OCR request is finished
00181                 activity.setShutterButtonClickable(false);
00182                 ocrDecode();
00183         }
00184 
00185 }
 Todo Clases Namespaces Archivos Funciones Variables