1/*
2 * Copyright (C) 2014 The Android Open Source Project
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
17package com.android.camera.one;
18
19import android.content.Context;
20import android.graphics.Bitmap;
21import android.location.Location;
22import android.net.Uri;
23import android.view.Surface;
24
25import com.android.camera.session.CaptureSession;
26import com.android.camera.util.Size;
27
28import java.io.File;
29
30/**
31 * OneCamera is a camera API tailored around our Google Camera application
32 * needs. It's not a general purpose API but instead offers an API with exactly
33 * what's needed from the app's side.
34 */
35public interface OneCamera {
36
37    /** Which way the camera is facing. */
38    public static enum Facing {
39        FRONT, BACK;
40    }
41
42    /**
43     * Auto focus system status; 1:1 mapping from camera2 AF_STATE.
44     * <ul>
45     * <li>{@link #INACTIVE}</li>
46     * <li>{@link #ACTIVE_SCAN}</li>
47     * <li>{@link #ACTIVE_FOCUSED}</li>
48     * <li>{@link #ACTIVE_UNFOCUSED}</li>
49     * <li>{@link #PASSIVE_SCAN}</li>
50     * <li>{@link #PASSIVE_FOCUSED}</li>
51     * <li>{@link #PASSIVE_UNFOCUSED}</li>
52     * </ul>
53     */
54    public static enum AutoFocusState {
55        /** Indicates AF system is inactive for some reason (could be an error). */
56        INACTIVE,
57        /** Indicates active scan in progress. */
58        ACTIVE_SCAN,
59        /** Indicates active scan success (in focus). */
60        ACTIVE_FOCUSED,
61        /** Indicates active scan failure (not in focus). */
62        ACTIVE_UNFOCUSED,
63        /** Indicates passive scan in progress. */
64        PASSIVE_SCAN,
65        /** Indicates passive scan success (in focus). */
66        PASSIVE_FOCUSED,
67        /** Indicates passive scan failure (not in focus). */
68        PASSIVE_UNFOCUSED
69    }
70
71    /**
72     * Auto focus system mode.
73     * <ul>
74     * <li>{@link #CONTINUOUS_PICTURE}</li>
75     * <li>{@link #AUTO}</li>
76     * </ul>
77     */
78    public static enum AutoFocusMode {
79        /** System is continuously focusing. */
80        CONTINUOUS_PICTURE,
81        /** System is running a triggered scan. */
82        AUTO
83    }
84
85    /**
86     * Classes implementing this interface will be called when the camera was
87     * opened or failed to open.
88     */
89    public static interface OpenCallback {
90        /**
91         * Called when the camera was opened successfully.
92         *
93         * @param camera the camera instance that was successfully opened
94         */
95        public void onCameraOpened(OneCamera camera);
96
97        /**
98         * Called if opening the camera failed.
99         */
100        public void onFailure();
101
102        /**
103         * Called if the camera is closed or disconnected while attempting to
104         * open.
105         */
106        public void onCameraClosed();
107    }
108
109    /**
110     * Classes implementing this interface will be called when the camera was
111     * closed.
112     */
113    public static interface CloseCallback {
114        /** Called when the camera was fully closed. */
115        public void onCameraClosed();
116    }
117
118    /**
119     * Classes implementing this interface can be informed when we're ready to
120     * take a picture of if setting up the capture pipeline failed.
121     */
122    public static interface CaptureReadyCallback {
123        /** After this is called, the system is ready for capture requests. */
124        public void onReadyForCapture();
125
126        /**
127         * Indicates that something went wrong during setup and the system is
128         * not ready for capture requests.
129         */
130        public void onSetupFailed();
131    }
132
133    /**
134     * Classes implementing this interface can be informed when the state of
135     * capture changes.
136     */
137    public static interface ReadyStateChangedListener {
138        /**
139         * Called when the camera is either ready or not ready to take a picture
140         * right now.
141         */
142        public void onReadyStateChanged(boolean readyForCapture);
143    }
144
145    /**
146     * A class implementing this interface can be passed into the call to take a
147     * picture in order to receive the resulting image or updated about the
148     * progress.
149     */
150    public static interface PictureCallback {
151        /**
152         * Called near the the when an image is being exposed for cameras which
153         * are exposing a single frame, so that a UI can be presented for the
154         * capture.
155         */
156        public void onQuickExpose();
157
158        /**
159         * Called when a thumbnail image is provided before the final image is
160         * finished.
161         */
162        public void onThumbnailResult(Bitmap bitmap);
163
164        /**
165         * Called when the final picture is done taking
166         *
167         * @param session the capture session
168         */
169        public void onPictureTaken(CaptureSession session);
170
171        /**
172         * Called when the picture has been saved to disk.
173         *
174         * @param uri the URI of the stored data.
175         */
176        public void onPictureSaved(Uri uri);
177
178        /**
179         * Called when picture taking failed.
180         */
181        public void onPictureTakenFailed();
182
183        /**
184         * Called when capture session is reporting a processing update. This
185         * should only be called by capture sessions that require the user to
186         * hold still for a while.
187         *
188         * @param progress a value from 0...1, indicating the current processing
189         *            progress.
190         */
191        public void onTakePictureProgress(float progress);
192    }
193
194    /**
195     * Classes implementing this interface will be called whenever the camera
196     * encountered an error.
197     */
198    public static interface CameraErrorListener {
199        /** Called when the camera encountered an error. */
200        public void onCameraError();
201    }
202
203    /**
204     * Classes implementing this interface will be called when the state of the
205     * focus changes. Guaranteed not to stay stuck in scanning state past some
206     * reasonable timeout even if Camera API is stuck.
207     */
208    public static interface FocusStateListener {
209        /**
210         * Called when state of auto focus system changes.
211         *
212         * @param state Current auto focus state.
213         * @param frameNumber Frame number if available.
214         */
215        public void onFocusStatusUpdate(AutoFocusState state, long frameNumber);
216    }
217
218    /**
219     * Parameters to be given to photo capture requests.
220     */
221    public static final class PhotoCaptureParameters {
222        /**
223         * Flash modes.
224         * <p>
225         * Has to be in sync with R.arrays.pref_camera_flashmode_entryvalues.
226         */
227        public static enum Flash {
228            AUTO, OFF, ON
229        }
230
231        /** The title/filename (without suffix) for this capture. */
232        public String title = null;
233        /** Called when the capture is completed or failed. */
234        public PictureCallback callback = null;
235        /** The device orientation so we can compute the right JPEG rotation. */
236        public int orientation = Integer.MIN_VALUE;
237        /** The heading of the device at time of capture. In degrees. */
238        public int heading = Integer.MIN_VALUE;
239        /** Flash mode for this capture. */
240        public Flash flashMode = Flash.AUTO;
241        /** The location of this capture. */
242        public Location location = null;
243
244        /** Set this to provide a debug folder for this capture. */
245        public File debugDataFolder;
246
247        /**
248         * Checks whether all required values are set. If one is missing, it
249         * throws a {@link RuntimeException}.
250         */
251        public void checkSanity() {
252            checkRequired(title);
253            checkRequired(callback);
254            checkRequired(orientation);
255            checkRequired(heading);
256        }
257
258        private void checkRequired(int num) {
259            if (num == Integer.MIN_VALUE) {
260                throw new RuntimeException("Photo capture parameter missing.");
261            }
262        }
263
264        private void checkRequired(Object obj) {
265            if (obj == null) {
266                throw new RuntimeException("Photo capture parameter missing.");
267            }
268        }
269    }
270
271    /**
272     * Meters and triggers auto focus scan with ROI around tap point.
273     * <p/>
274     * Normalized coordinates are referenced to portrait preview window with
275     * (0, 0) top left and (1, 1) bottom right. Rotation has no effect.
276     *
277     * @param nx normalized x coordinate.
278     * @param ny normalized y coordinate.
279     */
280    public void triggerFocusAndMeterAtPoint(float nx, float ny);
281
282    /**
283     * Call this to take a picture.
284     *
285     * @param params parameters for taking pictures.
286     * @param session the capture session for this picture.
287     */
288    public void takePicture(PhotoCaptureParameters params, CaptureSession session);
289
290    /**
291     * Sets or replaces a listener that is called whenever the camera encounters
292     * an error.
293     */
294    public void setCameraErrorListener(CameraErrorListener listener);
295
296    /**
297     * Sets or replaces a listener that is called whenever the focus state of
298     * the camera changes.
299     */
300    public void setFocusStateListener(FocusStateListener listener);
301
302    /**
303     * Sets or replaces a listener that is called whenever the state of the
304     * camera changes to be either ready or not ready to take another picture.
305     */
306    public void setReadyStateChangedListener(ReadyStateChangedListener listener);
307
308    /**
309     * Starts a preview stream and renders it to the given surface.
310     */
311    public void startPreview(Surface surface, CaptureReadyCallback listener);
312
313    /**
314     * Sets the size of the viewfinder.
315     * <p>
316     * The preview size requested from the camera device will depend on this as
317     * well as the requested photo/video aspect ratio.
318     */
319    public void setViewfinderSize(int width, int height);
320
321    /**
322     * @return Whether this camera supports flash.
323     * @param if true, returns whether flash is supported in enhanced mode. If
324     *        false, whether flash is supported in normal capture mode.
325     */
326    public boolean isFlashSupported(boolean enhanced);
327
328    /**
329     * @return Whether this camera supports enhanced mode, such as HDR.
330     */
331    public boolean isSupportingEnhancedMode();
332
333    /**
334     * Closes the camera.
335     *
336     * @param closeCallback Optional. Called as soon as the camera is fully
337     *            closed.
338     */
339    public void close(CloseCallback closeCallback);
340
341    /**
342     * @return A list of all supported resolutions.
343     */
344    public Size[] getSupportedSizes();
345
346    /**
347     * @return The aspect ratio of the full size capture (usually the native
348     *         resolution of the camera).
349     */
350    public float getFullSizeAspectRatio();
351
352    /**
353     * @return Whether this camera is facing to the back.
354     */
355    public boolean isBackFacing();
356
357    /**
358     * @return Whether this camera is facing to the front.
359     */
360    public boolean isFrontFacing();
361
362    /**
363     * Get the maximum zoom value.
364     *
365     * @return A float number to represent the maximum zoom value(>= 1.0).
366     */
367    public float getMaxZoom();
368
369    /**
370     * This function sets the current zoom ratio value.
371     * <p>
372     * The zoom range must be [1.0, maxZoom]. The maxZoom can be queried by
373     * {@link #getMaxZoom}.
374     *
375     * @param zoom Zoom ratio value passed to scaler.
376     */
377    public void setZoom(float zoom);
378
379    /**
380     * Based on the selected picture size, this returns the best preview size.
381     *
382     * @param pictureSize the picture size as selected by the user. A camera
383     *            might choose not to obey these and therefore the returned
384     *            preview size might not match the aspect ratio of the given
385     *            size.
386     * @param context the android application context
387     * @return The preview size that best matches the picture aspect ratio that
388     *         will be taken.
389     */
390    public Size pickPreviewSize(Size pictureSize, Context context);
391}
392