2 * Copyright (C) 2008 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.client.android;
19 import com.google.zxing.ResultPoint;
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;
34 import java.io.IOException;
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.
41 * @author dswitkin@google.com (Daniel Switkin)
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;
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;
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.
70 private final Camera.PreviewCallback previewCallback = new Camera.PreviewCallback() {
71 public void onPreviewFrame(byte[] data, Camera camera) {
72 if (!useOneShotPreviewCallback) {
73 camera.setPreviewCallback(null);
75 if (previewHandler != null) {
76 Message message = previewHandler.obtainMessage(previewMessage, cameraResolution.x,
77 cameraResolution.y, data);
78 message.sendToTarget();
79 previewHandler = null;
85 * Autofocus callbacks arrive here, and are dispatched to the Handler which requested them.
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;
99 * Initializes this static object with the Context of the calling Activity.
101 * @param context The Activity which wants to use the camera.
103 public static void init(Context context) {
104 if (cameraManager == null) {
105 cameraManager = new CameraManager(context);
110 * Gets the CameraManager singleton instance.
112 * @return A reference to the CameraManager singleton.
114 public static CameraManager get() {
115 return cameraManager;
118 private CameraManager(Context context) {
119 this.context = context;
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;
131 useOneShotPreviewCallback = true;
136 * Opens the camera driver and initializes the hardware parameters.
138 * @param holder The surface object which the camera will draw preview frames into.
139 * @throws IOException Indicates the camera driver failed to open.
141 public void openDriver(SurfaceHolder holder) throws IOException {
142 if (camera == null) {
143 camera = Camera.open();
144 camera.setPreviewDisplay(holder);
148 getScreenResolution();
151 setCameraParameters();
156 * Closes the camera driver if still in use.
158 public void closeDriver() {
159 if (camera != null) {
166 * Asks the camera hardware to begin drawing preview frames to the screen.
168 public void startPreview() {
169 if (camera != null && !previewing) {
170 camera.startPreview();
176 * Tells the camera to stop drawing preview frames.
178 public void stopPreview() {
179 if (camera != null && previewing) {
180 if (!useOneShotPreviewCallback) {
181 camera.setPreviewCallback(null);
183 camera.stopPreview();
184 previewHandler = null;
185 autoFocusHandler = null;
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,
195 * @param handler The handler to send the message to.
196 * @param message The what field of the message to be sent.
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);
205 camera.setPreviewCallback(previewCallback);
211 * Asks the camera hardware to perform an autofocus.
213 * @param handler The Handler to notify when the autofocus completes.
214 * @param message The message to deliver.
216 public void requestAutoFocus(Handler handler, int message) {
217 if (camera != null && previewing) {
218 autoFocusHandler = handler;
219 autoFocusMessage = message;
220 camera.autoFocus(autoFocusCallback);
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.
229 * @return The rectangle to draw on screen in window coordinates.
231 public Rect getFramingRect() {
232 if (framingRect == null) {
233 if (camera == null) {
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;
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;
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);
257 * Converts the result points from still resolution coordinates to screen coordinates.
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.
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);
276 * A factory method to build the appropriate LuminanceSource object based on the format
277 * of the preview buffers, as described by Camera.Parameters.
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.
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());
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());
305 throw new IllegalArgumentException("Unsupported picture format: " +
306 previewFormat + '/' + previewFormatString);
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.
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);
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);
332 // FIXME: This is a hack to turn the flash off on the Samsung Galaxy.
333 parameters.set("flash-value", 2);
335 // This is the standard setting to turn the flash off that all devices should honor.
336 parameters.set("flash-mode", "off");
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");
345 camera.setParameters(parameters);
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());
354 return screenResolution;