Ir a la documentación de este archivo.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00026 package edu.uoc.ocr.camera;
00027
00028 import android.content.Context;
00029 import android.content.SharedPreferences;
00030 import android.graphics.Point;
00031 import android.graphics.Rect;
00032 import android.hardware.Camera;
00033 import android.os.Handler;
00034 import android.preference.PreferenceManager;
00035 import android.view.SurfaceHolder;
00036 import edu.uoc.ocr.PlanarYUVLuminanceSource;
00037 import edu.uoc.ocr.PreferencesActivity;
00038
00039 import java.io.IOException;
00040
00048 public final class CameraManager {
00049
00050 private static final int MIN_FRAME_WIDTH = 50;
00051 private static final int MIN_FRAME_HEIGHT = 20;
00052 private static final int MAX_FRAME_WIDTH = 800;
00053 private static final int MAX_FRAME_HEIGHT = 600;
00054
00055 private final Context context;
00056 private final CameraConfigurationManager configManager;
00057 private Camera camera;
00058 private AutoFocusManager autoFocusManager;
00059 private Rect framingRect;
00060 private Rect framingRectInPreview;
00061 private boolean initialized;
00062 private boolean previewing;
00063 private boolean reverseImage;
00064 private int requestedFramingRectWidth;
00065 private int requestedFramingRectHeight;
00066
00072 private final PreviewCallback previewCallback;
00073
00074 public CameraManager(Context context) {
00075 this.context = context;
00076 this.configManager = new CameraConfigurationManager(context);
00077 previewCallback = new PreviewCallback(configManager);
00078 }
00079
00090 public synchronized void openDriver(SurfaceHolder holder)
00091 throws IOException {
00092 Camera theCamera = camera;
00093 if (theCamera == null) {
00094 theCamera = Camera.open();
00095 if (theCamera == null) {
00096 throw new IOException();
00097 }
00098 camera = theCamera;
00099 }
00100 camera.setPreviewDisplay(holder);
00101 if (!initialized) {
00102 initialized = true;
00103 configManager.initFromCameraParameters(theCamera);
00104 if (requestedFramingRectWidth > 0 && requestedFramingRectHeight > 0) {
00105 adjustFramingRect(requestedFramingRectWidth,
00106 requestedFramingRectHeight);
00107 requestedFramingRectWidth = 0;
00108 requestedFramingRectHeight = 0;
00109 }
00110 }
00111 configManager.setDesiredCameraParameters(theCamera);
00112
00113 SharedPreferences prefs = PreferenceManager
00114 .getDefaultSharedPreferences(context);
00115 reverseImage = prefs.getBoolean(PreferencesActivity.KEY_REVERSE_IMAGE,
00116 false);
00117 }
00118
00122 public synchronized void closeDriver() {
00123 if (camera != null) {
00124 camera.release();
00125 camera = null;
00126
00127
00128
00129
00130 framingRect = null;
00131 framingRectInPreview = null;
00132 }
00133 }
00134
00139 public synchronized void startPreview() {
00140 Camera theCamera = camera;
00141 if (theCamera != null && !previewing) {
00142 theCamera.startPreview();
00143 previewing = true;
00144 autoFocusManager = new AutoFocusManager(context, camera);
00145 }
00146 }
00147
00152 public synchronized void stopPreview() {
00153 if (autoFocusManager != null) {
00154 autoFocusManager.stop();
00155 autoFocusManager = null;
00156 }
00157 if (camera != null && previewing) {
00158 camera.stopPreview();
00159 previewCallback.setHandler(null, 0);
00160 previewing = false;
00161 }
00162 }
00163
00175 public synchronized void requestOcrDecode(Handler handler, int message) {
00176 Camera theCamera = camera;
00177 if (theCamera != null && previewing) {
00178 previewCallback.setHandler(handler, message);
00179 theCamera.setOneShotPreviewCallback(previewCallback);
00180 }
00181 }
00182
00188 public synchronized void requestAutoFocus(long delay) {
00189 autoFocusManager.start(delay);
00190 }
00191
00202 public synchronized Rect getFramingRect() {
00203 if (framingRect == null) {
00204 if (camera == null) {
00205 return null;
00206 }
00207 Point screenResolution = configManager.getScreenResolution();
00208 if (screenResolution == null) {
00209
00210 return null;
00211 }
00212 int width = screenResolution.x * 3 / 5;
00213 if (width < MIN_FRAME_WIDTH) {
00214 width = MIN_FRAME_WIDTH;
00215 } else if (width > MAX_FRAME_WIDTH) {
00216 width = MAX_FRAME_WIDTH;
00217 }
00218 int height = screenResolution.y * 1 / 5;
00219 if (height < MIN_FRAME_HEIGHT) {
00220 height = MIN_FRAME_HEIGHT;
00221 } else if (height > MAX_FRAME_HEIGHT) {
00222 height = MAX_FRAME_HEIGHT;
00223 }
00224 int leftOffset = (screenResolution.x - width) / 2;
00225 int topOffset = (screenResolution.y - height) / 2;
00226 framingRect = new Rect(leftOffset, topOffset, leftOffset + width,
00227 topOffset + height);
00228 }
00229 return framingRect;
00230 }
00231
00236 public synchronized Rect getFramingRectInPreview() {
00237 if (framingRectInPreview == null) {
00238 Rect rect = new Rect(getFramingRect());
00239 Point cameraResolution = configManager.getCameraResolution();
00240 Point screenResolution = configManager.getScreenResolution();
00241 if (cameraResolution == null || screenResolution == null) {
00242
00243 return null;
00244 }
00245 rect.left = rect.left * cameraResolution.x / screenResolution.x;
00246 rect.right = rect.right * cameraResolution.x / screenResolution.x;
00247 rect.top = rect.top * cameraResolution.y / screenResolution.y;
00248 rect.bottom = rect.bottom * cameraResolution.y / screenResolution.y;
00249 framingRectInPreview = rect;
00250 }
00251 return framingRectInPreview;
00252 }
00253
00262 public synchronized void adjustFramingRect(int deltaWidth, int deltaHeight) {
00263 if (initialized) {
00264 Point screenResolution = configManager.getScreenResolution();
00265
00266
00267 if ((framingRect.width() + deltaWidth > screenResolution.x - 4)
00268 || (framingRect.width() + deltaWidth < 50)) {
00269 deltaWidth = 0;
00270 }
00271 if ((framingRect.height() + deltaHeight > screenResolution.y - 4)
00272 || (framingRect.height() + deltaHeight < 50)) {
00273 deltaHeight = 0;
00274 }
00275
00276 int newWidth = framingRect.width() + deltaWidth;
00277 int newHeight = framingRect.height() + deltaHeight;
00278 int leftOffset = (screenResolution.x - newWidth) / 2;
00279 int topOffset = (screenResolution.y - newHeight) / 2;
00280 framingRect = new Rect(leftOffset, topOffset,
00281 leftOffset + newWidth, topOffset + newHeight);
00282 framingRectInPreview = null;
00283 } else {
00284 requestedFramingRectWidth = deltaWidth;
00285 requestedFramingRectHeight = deltaHeight;
00286 }
00287 }
00288
00302 public PlanarYUVLuminanceSource buildLuminanceSource(byte[] data,
00303 int width, int height) {
00304 Rect rect = getFramingRectInPreview();
00305 if (rect == null) {
00306 return null;
00307 }
00308
00309 return new PlanarYUVLuminanceSource(data, width, height, rect.left,
00310 rect.top, rect.width(), rect.height(), reverseImage);
00311 }
00312
00313 }
00314