05c5990da821158a4555f6e6fc6bb7b2d6fe0366
[zxing.git] / androidtest / src / com / google / zxing / client / androidtest / CameraManager.java
1 /*
2  * Copyright (C) 2008 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.client.androidtest;
18
19 import android.content.Context;
20 import android.graphics.Point;
21 import android.graphics.Rect;
22 import android.hardware.Camera;
23 import android.os.Handler;
24 import android.os.Message;
25 import android.view.Display;
26 import android.view.SurfaceHolder;
27 import android.view.WindowManager;
28
29 import java.io.IOException;
30
31 /**
32  * This object wraps the Camera service object and expects to be the only one talking to it. The
33  * implementation encapsulates the steps needed to take preview-sized images, which are used for
34  * both preview and decoding.
35  */
36 final class CameraManager {
37
38   private static final String TAG = "CameraManager";
39
40   private static CameraManager mCameraManager;
41   private Camera mCamera;
42   private final Context mContext;
43   private Point mScreenResolution;
44   private Rect mFramingRect;
45   private Handler mPreviewHandler;
46   private int mPreviewMessage;
47   private Handler mAutoFocusHandler;
48   private int mAutoFocusMessage;
49   private boolean mPreviewing;
50
51   public static synchronized void init(Context context) {
52     if (mCameraManager == null) {
53       mCameraManager = new CameraManager(context);
54       mCameraManager.getScreenResolution();
55     }
56   }
57
58   public static CameraManager get() {
59     return mCameraManager;
60   }
61
62   private CameraManager(Context context) {
63     mContext = context;
64     mCamera = null;
65     mPreviewing = false;
66   }
67
68   public void openDriver(SurfaceHolder holder) throws IOException {
69     // "throws IOException added to accommodate Android 1.5
70     if (mCamera == null) {
71       mCamera = Camera.open();
72       mCamera.setPreviewDisplay(holder);
73       setCameraParameters();
74     }
75   }
76
77   public void closeDriver() {
78     if (mCamera != null) {
79       mCamera.release();
80       mCamera = null;
81     }
82   }
83
84   public void startPreview() {
85     if (mCamera != null && !mPreviewing) {
86       mCamera.startPreview();
87       mPreviewing = true;
88     }
89   }
90
91   public void stopPreview() {
92     if (mCamera != null && mPreviewing) {
93       mCamera.setPreviewCallback(null);
94       mCamera.stopPreview();
95       mPreviewHandler = null;
96       mAutoFocusHandler = null;
97       mPreviewing = false;
98     }
99   }
100
101   /**
102    * A single preview frame will be returned to the handler supplied. The data will arrive as
103    * byte[] in the message.obj field, with width and height encoded as message.arg1 and
104    * message.arg2, respectively.
105    *
106    * @param handler The handler to send the message to.
107    * @param message The what field of the message to be sent.
108    */
109   public void requestPreviewFrame(Handler handler, int message) {
110     if (mCamera != null && mPreviewing) {
111       mPreviewHandler = handler;
112       mPreviewMessage = message;
113       mCamera.setOneShotPreviewCallback(previewCallback);
114     }
115   }
116
117   public void requestAutoFocus(Handler handler, int message) {
118     if (mCamera != null && mPreviewing) {
119       mAutoFocusHandler = handler;
120       mAutoFocusMessage = message;
121       mCamera.autoFocus(autoFocusCallback);
122     }
123   }
124
125   /**
126    * Calculates the framing rect which the UI should draw to show the user where to place the
127    * barcode. The actual captured image should be a bit larger than indicated because they might
128    * frame the shot too tightly. This target helps with alignment as well as forces the user to
129    * hold the device far enough away to ensure the image will be in focus.
130    *
131    * @return The rectangle to draw on screen in window coordinates.
132    */
133   public Rect getFramingRect() {
134     if (mFramingRect == null) {
135       int width = mScreenResolution.x;
136       int height = mScreenResolution.y * 3 / 4;
137       int leftOffset = (mScreenResolution.x - width) / 2;
138       int topOffset = (mScreenResolution.y - height) / 2;
139       mFramingRect = new Rect(leftOffset, topOffset, leftOffset + width, topOffset + height);
140     }
141     return mFramingRect;
142   }
143
144   /**
145    * Preview frames are delivered here, which we pass on to the registered handler. Make sure to
146    * clear the handler so it will only receive one message.
147    */
148   private final Camera.PreviewCallback previewCallback = new Camera.PreviewCallback() {
149     public void onPreviewFrame(byte[] data, Camera camera) {
150       if (mPreviewHandler != null) {
151         Message message = mPreviewHandler.obtainMessage(mPreviewMessage,
152             mScreenResolution.x, mScreenResolution.y, data);
153         message.sendToTarget();
154         mPreviewHandler = null;
155       }
156     }
157   };
158
159   private final Camera.AutoFocusCallback autoFocusCallback = new Camera.AutoFocusCallback() {
160     public void onAutoFocus(boolean success, Camera camera) {
161       if (mAutoFocusHandler != null) {
162         Message message = mAutoFocusHandler.obtainMessage(mAutoFocusMessage, success);
163         // The Barcodes app needs to insert a delay here because it does continuous focus,
164         // but this test app does not, so send the message immediately.
165         message.sendToTarget();
166         mAutoFocusHandler = null;
167       }
168     }
169   };
170
171   /**
172    * Sets the camera up to take preview images which are used for both preview and decoding. We're
173    * counting on the default YUV420 semi-planar data. If that changes in the future, we'll need to
174    * specify it explicitly with setPreviewFormat().
175    */
176   private void setCameraParameters() {
177     Camera.Parameters parameters = mCamera.getParameters();
178     parameters.setPreviewSize(mScreenResolution.x, mScreenResolution.y);
179     mCamera.setParameters(parameters);
180   }
181
182   private Point getScreenResolution() {
183     if (mScreenResolution == null) {
184       WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
185       Display display = wm.getDefaultDisplay();
186       mScreenResolution = new Point(display.getWidth(), display.getHeight());
187     }
188     return mScreenResolution;
189   }
190
191 }