Checked in the ZXing Test app for Android under androidtest. This application exercis...
[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 /**
30  * This object wraps the Camera service object and expects to be the only one talking to it. The
31  * implementation encapsulates the steps needed to take preview-sized images, which are used for
32  * both preview and decoding.
33  */
34 final class CameraManager {
35
36   private static final String TAG = "CameraManager";
37
38   private static CameraManager mCameraManager;
39   private Camera mCamera;
40   private final Context mContext;
41   private Point mScreenResolution;
42   private Rect mFramingRect;
43   private Handler mPreviewHandler;
44   private int mPreviewMessage;
45   private Handler mAutoFocusHandler;
46   private int mAutoFocusMessage;
47   private boolean mPreviewing;
48
49   public static void init(Context context) {
50     if (mCameraManager == null) {
51       mCameraManager = new CameraManager(context);
52       mCameraManager.getScreenResolution();
53     }
54   }
55
56   public static CameraManager get() {
57     return mCameraManager;
58   }
59
60   private CameraManager(Context context) {
61     mContext = context;
62     mCamera = null;
63     mPreviewing = false;
64   }
65
66   public void openDriver(SurfaceHolder holder) {
67     if (mCamera == null) {
68       mCamera = Camera.open();
69       mCamera.setPreviewDisplay(holder);
70
71       setCameraParameters();
72     }
73   }
74
75   public void closeDriver() {
76     if (mCamera != null) {
77       mCamera.release();
78       mCamera = null;
79     }
80   }
81
82   public void startPreview() {
83     if (mCamera != null && !mPreviewing) {
84       mCamera.startPreview();
85       mPreviewing = true;
86     }
87   }
88
89   public void stopPreview() {
90     if (mCamera != null && mPreviewing) {
91       mCamera.setPreviewCallback(null);
92       mCamera.stopPreview();
93       mPreviewHandler = null;
94       mAutoFocusHandler = null;
95       mPreviewing = false;
96     }
97   }
98
99   /**
100    * A single preview frame will be returned to the handler supplied. The data will arrive as
101    * byte[] in the message.obj field, with width and height encoded as message.arg1 and
102    * message.arg2, respectively.
103    *
104    * @param handler The handler to send the message to.
105    * @param message The what field of the message to be sent.
106    */
107   public void requestPreviewFrame(Handler handler, int message) {
108     if (mCamera != null && mPreviewing) {
109       mPreviewHandler = handler;
110       mPreviewMessage = message;
111       mCamera.setPreviewCallback(previewCallback);
112     }
113   }
114
115   public void requestAutoFocus(Handler handler, int message) {
116     if (mCamera != null && mPreviewing) {
117       mAutoFocusHandler = handler;
118       mAutoFocusMessage = message;
119       mCamera.autoFocus(autoFocusCallback);
120     }
121   }
122
123   /**
124    * Calculates the framing rect which the UI should draw to show the user where to place the
125    * barcode. The actual captured image should be a bit larger than indicated because they might
126    * frame the shot too tightly. This target helps with alignment as well as forces the user to
127    * hold the device far enough away to ensure the image will be in focus.
128    *
129    * @return The rectangle to draw on screen in window coordinates.
130    */
131   public Rect getFramingRect() {
132     if (mFramingRect == null) {
133       int size = ((mScreenResolution.x < mScreenResolution.y) ? mScreenResolution.x :
134           mScreenResolution.y) * 3 / 4;
135       int leftOffset = (mScreenResolution.x - size) / 2;
136       int topOffset = (mScreenResolution.y - size) / 2;
137       mFramingRect = new Rect(leftOffset, topOffset, leftOffset + size, topOffset + size);
138     }
139     return mFramingRect;
140   }
141
142   /**
143    * Preview frames are delivered here, which we pass on to the registered handler. Make sure to
144    * clear the handler so it will only receive one message.
145    */
146   private final Camera.PreviewCallback previewCallback = new Camera.PreviewCallback() {
147     public void onPreviewFrame(byte[] data, Camera camera) {
148       camera.setPreviewCallback(null);
149       if (mPreviewHandler != null) {
150         Message message = mPreviewHandler.obtainMessage(mPreviewMessage,
151             mScreenResolution.x, mScreenResolution.y, data);
152         message.sendToTarget();
153         mPreviewHandler = null;
154       }
155     }
156   };
157
158   private final Camera.AutoFocusCallback autoFocusCallback = new Camera.AutoFocusCallback() {
159     public void onAutoFocus(boolean success, Camera camera) {
160       if (mAutoFocusHandler != null) {
161         Message message = mAutoFocusHandler.obtainMessage(mAutoFocusMessage, success);
162         // The Barcodes app needs to insert a delay here because it does continuous focus,
163         // but this test app does not, so send the message immediately.
164         message.sendToTarget();
165         mAutoFocusHandler = null;
166       }
167     }
168   };
169
170   /**
171    * Sets the camera up to take preview images which are used for both preview and decoding. We're
172    * counting on the default YUV420 semi-planar data. If that changes in the future, we'll need to
173    * specify it explicitly with setPreviewFormat().
174    */
175   private void setCameraParameters() {
176     Camera.Parameters parameters = mCamera.getParameters();
177     parameters.setPreviewSize(mScreenResolution.x, mScreenResolution.y);
178     mCamera.setParameters(parameters);
179   }
180
181   private Point getScreenResolution() {
182     if (mScreenResolution == null) {
183       WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
184       Display display = wm.getDefaultDisplay();
185       mScreenResolution = new Point(display.getWidth(), display.getHeight());
186     }
187     return mScreenResolution;
188   }
189
190 }