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.androidtest;
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;
29 import java.io.IOException;
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.
36 final class CameraManager {
38 private static final String TAG = "CameraManager";
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;
51 public static synchronized void init(Context context) {
52 if (mCameraManager == null) {
53 mCameraManager = new CameraManager(context);
54 mCameraManager.getScreenResolution();
58 public static CameraManager get() {
59 return mCameraManager;
62 private CameraManager(Context context) {
68 // Throws IOException added to accommodate Android 1.5.
69 public String openDriver(SurfaceHolder holder, boolean getParameters) throws IOException {
71 if (mCamera == null) {
72 mCamera = Camera.open();
73 mCamera.setPreviewDisplay(holder);
75 result = collectCameraParameters();
77 setCameraParameters();
82 public void closeDriver() {
83 if (mCamera != null) {
89 public void startPreview() {
90 if (mCamera != null && !mPreviewing) {
91 mCamera.startPreview();
96 public void stopPreview() {
97 if (mCamera != null && mPreviewing) {
98 mCamera.setPreviewCallback(null);
99 mCamera.stopPreview();
100 mPreviewHandler = null;
101 mAutoFocusHandler = null;
107 * A single preview frame will be returned to the handler supplied. The data will arrive as
108 * byte[] in the message.obj field, with width and height encoded as message.arg1 and
109 * message.arg2, respectively.
111 * @param handler The handler to send the message to.
112 * @param message The what field of the message to be sent.
114 public void requestPreviewFrame(Handler handler, int message) {
115 if (mCamera != null && mPreviewing) {
116 mPreviewHandler = handler;
117 mPreviewMessage = message;
118 mCamera.setPreviewCallback(previewCallback);
122 public void requestAutoFocus(Handler handler, int message) {
123 if (mCamera != null && mPreviewing) {
124 mAutoFocusHandler = handler;
125 mAutoFocusMessage = message;
126 mCamera.autoFocus(autoFocusCallback);
131 * Calculates the framing rect which the UI should draw to show the user where to place the
132 * barcode. The actual captured image should be a bit larger than indicated because they might
133 * frame the shot too tightly. This target helps with alignment as well as forces the user to
134 * hold the device far enough away to ensure the image will be in focus.
136 * @return The rectangle to draw on screen in window coordinates.
138 public Rect getFramingRect() {
139 if (mFramingRect == null) {
140 int width = mScreenResolution.x * 3 / 4;
141 int height = mScreenResolution.y * 3 / 4;
142 int leftOffset = (mScreenResolution.x - width) / 2;
143 int topOffset = (mScreenResolution.y - height) / 2;
144 mFramingRect = new Rect(leftOffset, topOffset, leftOffset + width, topOffset + height);
150 * Preview frames are delivered here, which we pass on to the registered handler. Make sure to
151 * clear the handler so it will only receive one message.
153 private final Camera.PreviewCallback previewCallback = new Camera.PreviewCallback() {
154 public void onPreviewFrame(byte[] data, Camera camera) {
155 if (mPreviewHandler != null) {
156 mCamera.setPreviewCallback(null);
157 Message message = mPreviewHandler.obtainMessage(mPreviewMessage,
158 mScreenResolution.x, mScreenResolution.y, data);
159 message.sendToTarget();
160 mPreviewHandler = null;
165 private final Camera.AutoFocusCallback autoFocusCallback = new Camera.AutoFocusCallback() {
166 public void onAutoFocus(boolean success, Camera camera) {
167 if (mAutoFocusHandler != null) {
168 Message message = mAutoFocusHandler.obtainMessage(mAutoFocusMessage, success);
169 // The Barcodes app needs to insert a delay here because it does continuous focus,
170 // but this test app does not, so send the message immediately.
171 message.sendToTarget();
172 mAutoFocusHandler = null;
178 * Sets the camera up to take preview images which are used for both preview and decoding. We're
179 * counting on the default YUV420 semi-planar data. If that changes in the future, we'll need to
180 * specify it explicitly with setPreviewFormat().
182 private void setCameraParameters() {
183 Camera.Parameters parameters = mCamera.getParameters();
184 parameters.setPreviewSize(mScreenResolution.x, mScreenResolution.y);
185 mCamera.setParameters(parameters);
188 private String collectCameraParameters() {
189 Camera.Parameters parameters = mCamera.getParameters();
190 String[] params = parameters.flatten().split(";");
191 StringBuffer result = new StringBuffer();
192 result.append("Default camera parameters:");
193 for (String param : params) {
194 result.append("\n ");
195 result.append(param);
198 return result.toString();
201 private Point getScreenResolution() {
202 if (mScreenResolution == null) {
203 WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
204 Display display = wm.getDefaultDisplay();
205 mScreenResolution = new Point(display.getWidth(), display.getHeight());
207 return mScreenResolution;