0a70eab736daf418379dcf1302db7740dbb8315d
[zxing.git] / android / src / com / google / zxing / client / android / 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.android;
18
19 import com.google.zxing.ResultPoint;
20
21 import android.content.Context;
22 import android.graphics.PixelFormat;
23 import android.graphics.Point;
24 import android.graphics.Rect;
25 import android.hardware.Camera;
26 import android.os.Build;
27 import android.os.Handler;
28 import android.os.Message;
29 import android.util.Log;
30 import android.view.Display;
31 import android.view.SurfaceHolder;
32 import android.view.WindowManager;
33
34 import java.io.IOException;
35
36 /**
37  * This object wraps the Camera service object and expects to be the only one talking to it. The
38  * implementation encapsulates the steps needed to take preview-sized images, which are used for
39  * both preview and decoding.
40  *
41  * @author dswitkin@google.com (Daniel Switkin)
42  */
43 final class CameraManager {
44   private static final String TAG = "CameraManager";
45   private static final int MIN_FRAME_WIDTH = 240;
46   private static final int MIN_FRAME_HEIGHT = 240;
47   private static final int MAX_FRAME_WIDTH = 480;
48   private static final int MAX_FRAME_HEIGHT = 360;
49
50   private static CameraManager cameraManager;
51   private Camera camera;
52   private final Context context;
53   private Point screenResolution;
54   private Point cameraResolution;
55   private Rect framingRect;
56   private Handler previewHandler;
57   private int previewMessage;
58   private Handler autoFocusHandler;
59   private int autoFocusMessage;
60   private boolean initialized;
61   private boolean previewing;
62   private int previewFormat;
63   private String previewFormatString;
64   private boolean useOneShotPreviewCallback;
65
66   /**
67    * Preview frames are delivered here, which we pass on to the registered handler. Make sure to
68    * clear the handler so it will only receive one message.
69    */
70   private final Camera.PreviewCallback previewCallback = new Camera.PreviewCallback() {
71     public void onPreviewFrame(byte[] data, Camera camera) {
72       if (!useOneShotPreviewCallback) {
73         camera.setPreviewCallback(null);
74       }
75       if (previewHandler != null) {
76         Message message = previewHandler.obtainMessage(previewMessage, cameraResolution.x,
77             cameraResolution.y, data);
78         message.sendToTarget();
79         previewHandler = null;
80       }
81     }
82   };
83
84   /**
85    * Autofocus callbacks arrive here, and are dispatched to the Handler which requested them.
86    */
87   private final Camera.AutoFocusCallback autoFocusCallback = new Camera.AutoFocusCallback() {
88     public void onAutoFocus(boolean success, Camera camera) {
89       if (autoFocusHandler != null) {
90         Message message = autoFocusHandler.obtainMessage(autoFocusMessage, success);
91         // Simulate continuous autofocus by sending a focus request every 1.5 seconds.
92         autoFocusHandler.sendMessageDelayed(message, 1500L);
93         autoFocusHandler = null;
94       }
95     }
96   };
97
98   /**
99    * Initializes this static object with the Context of the calling Activity.
100    *
101    * @param context The Activity which wants to use the camera.
102    */
103   public static void init(Context context) {
104     if (cameraManager == null) {
105       cameraManager = new CameraManager(context);
106     }
107   }
108
109   /**
110    * Gets the CameraManager singleton instance.
111    *
112    * @return A reference to the CameraManager singleton.
113    */
114   public static CameraManager get() {
115     return cameraManager;
116   }
117
118   private CameraManager(Context context) {
119     this.context = context;
120     camera = null;
121     initialized = false;
122     previewing = false;
123
124     // Camera.setOneShotPreviewCallback() has a race condition in Cupcake, so we use the older
125     // Camera.setPreviewCallback() on 1.5 and earlier. For Donut and later, we need to use
126     // the more efficient one shot callback, as the older one can swamp the system and cause it
127     // to run out of memory. We can't use SDK_INT because it was introduced in the Donut SDK.
128     if (Integer.parseInt(Build.VERSION.SDK) <= Build.VERSION_CODES.CUPCAKE) {
129       useOneShotPreviewCallback = false;
130     } else {
131       useOneShotPreviewCallback = true;
132     }
133   }
134
135   /**
136    * Opens the camera driver and initializes the hardware parameters.
137    *
138    * @param holder The surface object which the camera will draw preview frames into.
139    * @throws IOException Indicates the camera driver failed to open.
140    */
141   public void openDriver(SurfaceHolder holder) throws IOException {
142     if (camera == null) {
143       camera = Camera.open();
144       camera.setPreviewDisplay(holder);
145
146       if (!initialized) {
147         initialized = true;
148         getScreenResolution();
149       }
150
151       setCameraParameters();
152     }
153   }
154
155   /**
156    * Closes the camera driver if still in use.
157    */
158   public void closeDriver() {
159     if (camera != null) {
160       camera.release();
161       camera = null;
162     }
163   }
164
165   /**
166    * Asks the camera hardware to begin drawing preview frames to the screen.
167    */
168   public void startPreview() {
169     if (camera != null && !previewing) {
170       camera.startPreview();
171       previewing = true;
172     }
173   }
174
175   /**
176    * Tells the camera to stop drawing preview frames.
177    */
178   public void stopPreview() {
179     if (camera != null && previewing) {
180       if (!useOneShotPreviewCallback) {
181         camera.setPreviewCallback(null);
182       }
183       camera.stopPreview();
184       previewHandler = null;
185       autoFocusHandler = null;
186       previewing = false;
187     }
188   }
189
190   /**
191    * A single preview frame will be returned to the handler supplied. The data will arrive as byte[]
192    * in the message.obj field, with width and height encoded as message.arg1 and message.arg2,
193    * respectively.
194    *
195    * @param handler The handler to send the message to.
196    * @param message The what field of the message to be sent.
197    */
198   public void requestPreviewFrame(Handler handler, int message) {
199     if (camera != null && previewing) {
200       previewHandler = handler;
201       previewMessage = message;
202       if (useOneShotPreviewCallback) {
203         camera.setOneShotPreviewCallback(previewCallback);
204       } else {
205         camera.setPreviewCallback(previewCallback);
206       }
207     }
208   }
209
210   /**
211    * Asks the camera hardware to perform an autofocus.
212    *
213    * @param handler The Handler to notify when the autofocus completes.
214    * @param message The message to deliver.
215    */
216   public void requestAutoFocus(Handler handler, int message) {
217     if (camera != null && previewing) {
218       autoFocusHandler = handler;
219       autoFocusMessage = message;
220       camera.autoFocus(autoFocusCallback);
221     }
222   }
223
224   /**
225    * Calculates the framing rect which the UI should draw to show the user where to place the
226    * barcode. This target helps with alignment as well as forces the user to hold the device
227    * far enough away to ensure the image will be in focus.
228    *
229    * @return The rectangle to draw on screen in window coordinates.
230    */
231   public Rect getFramingRect() {
232     if (framingRect == null) {
233       if (camera == null) {
234         return null;
235       }
236       int width = cameraResolution.x * 3 / 4;
237       if (width < MIN_FRAME_WIDTH) {
238         width = MIN_FRAME_WIDTH;
239       } else if (width > MAX_FRAME_WIDTH) {
240         width = MAX_FRAME_WIDTH;
241       }
242       int height = cameraResolution.y * 3 / 4;
243       if (height < MIN_FRAME_HEIGHT) {
244         height = MIN_FRAME_HEIGHT;
245       } else if (height > MAX_FRAME_HEIGHT) {
246         height = MAX_FRAME_HEIGHT;
247       }
248       int leftOffset = (cameraResolution.x - width) / 2;
249       int topOffset = (cameraResolution.y - height) / 2;
250       framingRect = new Rect(leftOffset, topOffset, leftOffset + width, topOffset + height);
251       Log.v(TAG, "Calculated framing rect: " + framingRect);
252     }
253     return framingRect;
254   }
255
256   /**
257    * Converts the result points from still resolution coordinates to screen coordinates.
258    *
259    * @param points The points returned by the Reader subclass through Result.getResultPoints().
260    * @return An array of Points scaled to the size of the framing rect and offset appropriately
261    *         so they can be drawn in screen coordinates.
262    */
263   public Point[] convertResultPoints(ResultPoint[] points) {
264     Rect frame = getFramingRect();
265     int count = points.length;
266     Point[] output = new Point[count];
267     for (int x = 0; x < count; x++) {
268       output[x] = new Point();
269       output[x].x = frame.left + (int) (points[x].getX() + 0.5f);
270       output[x].y = frame.top + (int) (points[x].getY() + 0.5f);
271     }
272     return output;
273   }
274
275   /**
276    * A factory method to build the appropriate LuminanceSource object based on the format
277    * of the preview buffers, as described by Camera.Parameters.
278    *
279    * @param data A preview frame.
280    * @param width The width of the image.
281    * @param height The height of the image.
282    * @return A PlanarYUVLuminanceSource instance.
283    */
284   public PlanarYUVLuminanceSource buildLuminanceSource(byte[] data, int width, int height) {
285     Rect rect = getFramingRect();
286     switch (previewFormat) {
287       // This is the standard Android format which all devices are REQUIRED to support.
288       // In theory, it's the only one we should ever care about.
289       case PixelFormat.YCbCr_420_SP:
290         return new PlanarYUVLuminanceSource(data, width, height, rect.left, rect.top,
291             rect.width(), rect.height());
292       // This format has never been seen in the wild, but is compatible as we only care
293       // about the Y channel, so allow it.
294       case PixelFormat.YCbCr_422_SP:
295         return new PlanarYUVLuminanceSource(data, width, height, rect.left, rect.top,
296             rect.width(), rect.height());
297       default:
298         // The Samsung Moment incorrectly uses this variant instead of the 'sp' version.
299         // Fortunately, it too has all the Y data up front, so we can read it.
300         if (previewFormatString.equals("yuv420p")) {
301           return new PlanarYUVLuminanceSource(data, width, height, rect.left, rect.top,
302             rect.width(), rect.height());
303         }
304     }
305     throw new IllegalArgumentException("Unsupported picture format: " +
306         previewFormat + '/' + previewFormatString);
307   }
308
309   /**
310    * Sets the camera up to take preview images which are used for both preview and decoding.
311    * We detect the preview format here so that buildLuminanceSource() can build an appropriate
312    * LuminanceSource subclass. In the future we may want to force YUV420SP as it's the smallest,
313    * and the planar Y can be used for barcode scanning without a copy in some cases.
314    */
315   private void setCameraParameters() {
316     Camera.Parameters parameters = camera.getParameters();
317     Camera.Size size = parameters.getPreviewSize();
318     Log.v(TAG, "Default preview size: " + size.width + ", " + size.height);
319     previewFormat = parameters.getPreviewFormat();
320     previewFormatString = parameters.get("preview-format");
321     Log.v(TAG, "Default preview format: " + previewFormat + '/' + previewFormatString);
322
323     // Ensure that the camera resolution is a multiple of 8, as the screen may not be.
324     // TODO: A better solution would be to request the supported preview resolutions
325     // and pick the best match, but this parameter is not standardized in Cupcake.
326     cameraResolution = new Point();
327     cameraResolution.x = (screenResolution.x >> 3) << 3;
328     cameraResolution.y = (screenResolution.y >> 3) << 3;
329     Log.v(TAG, "Setting preview size: " + cameraResolution.x + ", " + cameraResolution.y);
330     parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);
331
332     // FIXME: This is a hack to turn the flash off on the Samsung Galaxy.
333     parameters.set("flash-value", 2);
334
335     // This is the standard setting to turn the flash off that all devices should honor.
336     parameters.set("flash-mode", "off");
337
338     // Set zoom to 2x if available. This helps encourage the user to pull back.
339     // Some devices like the Behold have a zoom parameter
340     parameters.set("zoom", "2.0");
341     // Most devices, like the Hero, appear to expose this zoom parameter.
342     // (I think) This means 2.0x
343     parameters.set("taking-picture-zoom", "20");
344
345     camera.setParameters(parameters);
346   }
347
348   private Point getScreenResolution() {
349     if (screenResolution == null) {
350       WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
351       Display display = manager.getDefaultDisplay();
352       screenResolution = new Point(display.getWidth(), display.getHeight());
353     }
354     return screenResolution;
355   }
356
357 }