Added a new feature to the test app, which captures all the device info and default...
[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   // Throws IOException added to accommodate Android 1.5.
69   public String openDriver(SurfaceHolder holder, boolean getParameters) throws IOException {
70     String result = null;
71     if (mCamera == null) {
72       mCamera = Camera.open();
73       mCamera.setPreviewDisplay(holder);
74       if (getParameters) {
75         result = collectCameraParameters();
76       }
77       setCameraParameters();
78     }
79     return result;
80   }
81
82   public void closeDriver() {
83     if (mCamera != null) {
84       mCamera.release();
85       mCamera = null;
86     }
87   }
88
89   public void startPreview() {
90     if (mCamera != null && !mPreviewing) {
91       mCamera.startPreview();
92       mPreviewing = true;
93     }
94   }
95
96   public void stopPreview() {
97     if (mCamera != null && mPreviewing) {
98       mCamera.setPreviewCallback(null);
99       mCamera.stopPreview();
100       mPreviewHandler = null;
101       mAutoFocusHandler = null;
102       mPreviewing = false;
103     }
104   }
105
106   /**
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.
110    *
111    * @param handler The handler to send the message to.
112    * @param message The what field of the message to be sent.
113    */
114   public void requestPreviewFrame(Handler handler, int message) {
115     if (mCamera != null && mPreviewing) {
116       mPreviewHandler = handler;
117       mPreviewMessage = message;
118       mCamera.setPreviewCallback(previewCallback);
119     }
120   }
121
122   public void requestAutoFocus(Handler handler, int message) {
123     if (mCamera != null && mPreviewing) {
124       mAutoFocusHandler = handler;
125       mAutoFocusMessage = message;
126       mCamera.autoFocus(autoFocusCallback);
127     }
128   }
129
130   /**
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.
135    *
136    * @return The rectangle to draw on screen in window coordinates.
137    */
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);
145     }
146     return mFramingRect;
147   }
148
149   /**
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.
152    */
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;
161       }
162     }
163   };
164
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;
173       }
174     }
175   };
176
177   /**
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().
181    */
182   private void setCameraParameters() {
183     Camera.Parameters parameters = mCamera.getParameters();
184     parameters.setPreviewSize(mScreenResolution.x, mScreenResolution.y);
185     mCamera.setParameters(parameters);
186   }
187
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);
196     }
197     result.append('\n');
198     return result.toString();
199   }
200
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());
206     }
207     return mScreenResolution;
208   }
209
210 }