forked from zsol/android_frameworks_base
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCamera.java
More file actions
3494 lines (3222 loc) · 142 KB
/
Copy pathCamera.java
File metadata and controls
3494 lines (3222 loc) · 142 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.hardware;
import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType;
import android.graphics.ImageFormat;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.SurfaceTexture;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;
/**
* The Camera class is used to set image capture settings, start/stop preview,
* snap pictures, and retrieve frames for encoding for video. This class is a
* client for the Camera service, which manages the actual camera hardware.
*
* <p>To access the device camera, you must declare the
* {@link android.Manifest.permission#CAMERA} permission in your Android
* Manifest. Also be sure to include the
* <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html"><uses-feature></a>
* manifest element to declare camera features used by your application.
* For example, if you use the camera and auto-focus feature, your Manifest
* should include the following:</p>
* <pre> <uses-permission android:name="android.permission.CAMERA" />
* <uses-feature android:name="android.hardware.camera" />
* <uses-feature android:name="android.hardware.camera.autofocus" /></pre>
*
* <p>To take pictures with this class, use the following steps:</p>
*
* <ol>
* <li>Obtain an instance of Camera from {@link #open(int)}.
*
* <li>Get existing (default) settings with {@link #getParameters()}.
*
* <li>If necessary, modify the returned {@link Camera.Parameters} object and call
* {@link #setParameters(Camera.Parameters)}.
*
* <li>If desired, call {@link #setDisplayOrientation(int)}.
*
* <li><b>Important</b>: Pass a fully initialized {@link SurfaceHolder} to
* {@link #setPreviewDisplay(SurfaceHolder)}. Without a surface, the camera
* will be unable to start the preview.
*
* <li><b>Important</b>: Call {@link #startPreview()} to start updating the
* preview surface. Preview must be started before you can take a picture.
*
* <li>When you want, call {@link #takePicture(Camera.ShutterCallback,
* Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)} to
* capture a photo. Wait for the callbacks to provide the actual image data.
*
* <li>After taking a picture, preview display will have stopped. To take more
* photos, call {@link #startPreview()} again first.
*
* <li>Call {@link #stopPreview()} to stop updating the preview surface.
*
* <li><b>Important:</b> Call {@link #release()} to release the camera for
* use by other applications. Applications should release the camera
* immediately in {@link android.app.Activity#onPause()} (and re-{@link #open()}
* it in {@link android.app.Activity#onResume()}).
* </ol>
*
* <p>To quickly switch to video recording mode, use these steps:</p>
*
* <ol>
* <li>Obtain and initialize a Camera and start preview as described above.
*
* <li>Call {@link #unlock()} to allow the media process to access the camera.
*
* <li>Pass the camera to {@link android.media.MediaRecorder#setCamera(Camera)}.
* See {@link android.media.MediaRecorder} information about video recording.
*
* <li>When finished recording, call {@link #reconnect()} to re-acquire
* and re-lock the camera.
*
* <li>If desired, restart preview and take more photos or videos.
*
* <li>Call {@link #stopPreview()} and {@link #release()} as described above.
* </ol>
*
* <p>This class is not thread-safe, and is meant for use from one event thread.
* Most long-running operations (preview, focus, photo capture, etc) happen
* asynchronously and invoke callbacks as necessary. Callbacks will be invoked
* on the event thread {@link #open(int)} was called from. This class's methods
* must never be called from multiple threads at once.</p>
*
* <p class="caution"><strong>Caution:</strong> Different Android-powered devices
* may have different hardware specifications, such as megapixel ratings and
* auto-focus capabilities. In order for your application to be compatible with
* more devices, you should not make assumptions about the device camera
* specifications.</p>
*
* <div class="special reference">
* <h3>Developer Guides</h3>
* <p>For more information about using cameras, read the
* <a href="{@docRoot}guide/topics/media/camera.html">Camera</a> developer guide.</p>
* </div>
*/
public class Camera {
private static final String TAG = "Camera";
// These match the enums in frameworks/base/include/camera/Camera.h
private static final int CAMERA_MSG_ERROR = 0x001;
private static final int CAMERA_MSG_SHUTTER = 0x002;
private static final int CAMERA_MSG_FOCUS = 0x004;
private static final int CAMERA_MSG_ZOOM = 0x008;
private static final int CAMERA_MSG_PREVIEW_FRAME = 0x010;
private static final int CAMERA_MSG_VIDEO_FRAME = 0x020;
private static final int CAMERA_MSG_POSTVIEW_FRAME = 0x040;
private static final int CAMERA_MSG_RAW_IMAGE = 0x080;
private static final int CAMERA_MSG_COMPRESSED_IMAGE = 0x100;
private static final int CAMERA_MSG_RAW_IMAGE_NOTIFY = 0x200;
private static final int CAMERA_MSG_PREVIEW_METADATA = 0x400;
private static final int CAMERA_MSG_BURST_IMAGE = 0x800;
private static final int CAMERA_MSG_ALL_MSGS = 0x4FF;
private int mNativeContext; // accessed by native methods
private EventHandler mEventHandler;
private ShutterCallback mShutterCallback;
private PictureCallback mRawImageCallback;
private PictureCallback mJpegCallback;
private PreviewCallback mPreviewCallback;
private PictureCallback mPostviewCallback;
private AutoFocusCallback mAutoFocusCallback;
private OnZoomChangeListener mZoomListener;
private FaceDetectionListener mFaceListener;
private ErrorCallback mErrorCallback;
private boolean mOneShot;
private boolean mWithBuffer;
private boolean mFaceDetectionRunning = false;
/**
* Broadcast Action: A new picture is taken by the camera, and the entry of
* the picture has been added to the media store.
* {@link android.content.Intent#getData} is URI of the picture.
*/
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String ACTION_NEW_PICTURE = "android.hardware.action.NEW_PICTURE";
/**
* Broadcast Action: A new video is recorded by the camera, and the entry
* of the video has been added to the media store.
* {@link android.content.Intent#getData} is URI of the video.
*/
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String ACTION_NEW_VIDEO = "android.hardware.action.NEW_VIDEO";
/**
* Hardware face detection. It does not use much CPU.
*/
private static final int CAMERA_FACE_DETECTION_HW = 0;
/**
* Software face detection. It uses some CPU.
*/
private static final int CAMERA_FACE_DETECTION_SW = 1;
/**
* Returns the number of physical cameras available on this device.
*/
public native static int getNumberOfCameras();
/**
* Returns the information about a particular camera.
* If {@link #getNumberOfCameras()} returns N, the valid id is 0 to N-1.
*/
public native static void getCameraInfo(int cameraId, CameraInfo cameraInfo);
/**
* Information about a camera
*/
public static class CameraInfo {
/**
* The facing of the camera is opposite to that of the screen.
*/
public static final int CAMERA_FACING_BACK = 0;
/**
* The facing of the camera is the same as that of the screen.
*/
public static final int CAMERA_FACING_FRONT = 1;
/**
* The direction that the camera faces. It should be
* CAMERA_FACING_BACK or CAMERA_FACING_FRONT.
*/
public int facing;
/**
* <p>The orientation of the camera image. The value is the angle that the
* camera image needs to be rotated clockwise so it shows correctly on
* the display in its natural orientation. It should be 0, 90, 180, or 270.</p>
*
* <p>For example, suppose a device has a naturally tall screen. The
* back-facing camera sensor is mounted in landscape. You are looking at
* the screen. If the top side of the camera sensor is aligned with the
* right edge of the screen in natural orientation, the value should be
* 90. If the top side of a front-facing camera sensor is aligned with
* the right of the screen, the value should be 270.</p>
*
* @see #setDisplayOrientation(int)
* @see Parameters#setRotation(int)
* @see Parameters#setPreviewSize(int, int)
* @see Parameters#setPictureSize(int, int)
* @see Parameters#setJpegThumbnailSize(int, int)
*/
public int orientation;
};
/**
* Creates a new Camera object to access a particular hardware camera.
*
* <p>You must call {@link #release()} when you are done using the camera,
* otherwise it will remain locked and be unavailable to other applications.
*
* <p>Your application should only have one Camera object active at a time
* for a particular hardware camera.
*
* <p>Callbacks from other methods are delivered to the event loop of the
* thread which called open(). If this thread has no event loop, then
* callbacks are delivered to the main application event loop. If there
* is no main application event loop, callbacks are not delivered.
*
* <p class="caution"><b>Caution:</b> On some devices, this method may
* take a long time to complete. It is best to call this method from a
* worker thread (possibly using {@link android.os.AsyncTask}) to avoid
* blocking the main application UI thread.
*
* @param cameraId the hardware camera to access, between 0 and
* {@link #getNumberOfCameras()}-1.
* @return a new Camera object, connected, locked and ready for use.
* @throws RuntimeException if connection to the camera service fails (for
* example, if the camera is in use by another process or device policy
* manager has disabled the camera).
* @see android.app.admin.DevicePolicyManager#getCameraDisabled(android.content.ComponentName)
*/
public static Camera open(int cameraId) {
return new Camera(cameraId);
}
/**
* Creates a new Camera object to access the first back-facing camera on the
* device. If the device does not have a back-facing camera, this returns
* null.
* @see #open(int)
*/
public static Camera open() {
int numberOfCameras = getNumberOfCameras();
CameraInfo cameraInfo = new CameraInfo();
for (int i = 0; i < numberOfCameras; i++) {
getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
return new Camera(i);
}
}
return null;
}
Camera(int cameraId) {
mShutterCallback = null;
mRawImageCallback = null;
mJpegCallback = null;
mPreviewCallback = null;
mPostviewCallback = null;
mZoomListener = null;
Looper looper;
if ((looper = Looper.myLooper()) != null) {
mEventHandler = new EventHandler(this, looper);
} else if ((looper = Looper.getMainLooper()) != null) {
mEventHandler = new EventHandler(this, looper);
} else {
mEventHandler = null;
}
native_setup(new WeakReference<Camera>(this), cameraId);
}
protected void finalize() {
release();
}
private native final void native_setup(Object camera_this, int cameraId);
private native final void native_release();
/**
* Disconnects and releases the Camera object resources.
*
* <p>You must call this as soon as you're done with the Camera object.</p>
*/
public final void release() {
native_release();
mFaceDetectionRunning = false;
}
/**
* Unlocks the camera to allow another process to access it.
* Normally, the camera is locked to the process with an active Camera
* object until {@link #release()} is called. To allow rapid handoff
* between processes, you can call this method to release the camera
* temporarily for another process to use; once the other process is done
* you can call {@link #reconnect()} to reclaim the camera.
*
* <p>This must be done before calling
* {@link android.media.MediaRecorder#setCamera(Camera)}. This cannot be
* called after recording starts.
*
* <p>If you are not recording video, you probably do not need this method.
*
* @throws RuntimeException if the camera cannot be unlocked.
*/
public native final void unlock();
/**
* Re-locks the camera to prevent other processes from accessing it.
* Camera objects are locked by default unless {@link #unlock()} is
* called. Normally {@link #reconnect()} is used instead.
*
* <p>Since API level 14, camera is automatically locked for applications in
* {@link android.media.MediaRecorder#start()}. Applications can use the
* camera (ex: zoom) after recording starts. There is no need to call this
* after recording starts or stops.
*
* <p>If you are not recording video, you probably do not need this method.
*
* @throws RuntimeException if the camera cannot be re-locked (for
* example, if the camera is still in use by another process).
*/
public native final void lock();
/**
* Reconnects to the camera service after another process used it.
* After {@link #unlock()} is called, another process may use the
* camera; when the process is done, you must reconnect to the camera,
* which will re-acquire the lock and allow you to continue using the
* camera.
*
* <p>Since API level 14, camera is automatically locked for applications in
* {@link android.media.MediaRecorder#start()}. Applications can use the
* camera (ex: zoom) after recording starts. There is no need to call this
* after recording starts or stops.
*
* <p>If you are not recording video, you probably do not need this method.
*
* @throws IOException if a connection cannot be re-established (for
* example, if the camera is still in use by another process).
*/
public native final void reconnect() throws IOException;
/**
* Sets the {@link Surface} to be used for live preview.
* Either a surface or surface texture is necessary for preview, and
* preview is necessary to take pictures. The same surface can be re-set
* without harm. Setting a preview surface will un-set any preview surface
* texture that was set via {@link #setPreviewTexture}.
*
* <p>The {@link SurfaceHolder} must already contain a surface when this
* method is called. If you are using {@link android.view.SurfaceView},
* you will need to register a {@link SurfaceHolder.Callback} with
* {@link SurfaceHolder#addCallback(SurfaceHolder.Callback)} and wait for
* {@link SurfaceHolder.Callback#surfaceCreated(SurfaceHolder)} before
* calling setPreviewDisplay() or starting preview.
*
* <p>This method must be called before {@link #startPreview()}. The
* one exception is that if the preview surface is not set (or set to null)
* before startPreview() is called, then this method may be called once
* with a non-null parameter to set the preview surface. (This allows
* camera setup and surface creation to happen in parallel, saving time.)
* The preview surface may not otherwise change while preview is running.
*
* @param holder containing the Surface on which to place the preview,
* or null to remove the preview surface
* @throws IOException if the method fails (for example, if the surface
* is unavailable or unsuitable).
*/
public final void setPreviewDisplay(SurfaceHolder holder) throws IOException {
if (holder != null) {
setPreviewDisplay(holder.getSurface());
} else {
setPreviewDisplay((Surface)null);
}
}
private native final void setPreviewDisplay(Surface surface) throws IOException;
/**
* Sets the {@link SurfaceTexture} to be used for live preview.
* Either a surface or surface texture is necessary for preview, and
* preview is necessary to take pictures. The same surface texture can be
* re-set without harm. Setting a preview surface texture will un-set any
* preview surface that was set via {@link #setPreviewDisplay}.
*
* <p>This method must be called before {@link #startPreview()}. The
* one exception is that if the preview surface texture is not set (or set
* to null) before startPreview() is called, then this method may be called
* once with a non-null parameter to set the preview surface. (This allows
* camera setup and surface creation to happen in parallel, saving time.)
* The preview surface texture may not otherwise change while preview is
* running.
*
* <p>The timestamps provided by {@link SurfaceTexture#getTimestamp()} for a
* SurfaceTexture set as the preview texture have an unspecified zero point,
* and cannot be directly compared between different cameras or different
* instances of the same camera, or across multiple runs of the same
* program.
*
* @param surfaceTexture the {@link SurfaceTexture} to which the preview
* images are to be sent or null to remove the current preview surface
* texture
* @throws IOException if the method fails (for example, if the surface
* texture is unavailable or unsuitable).
*/
public native final void setPreviewTexture(SurfaceTexture surfaceTexture) throws IOException;
/**
* Callback interface used to deliver copies of preview frames as
* they are displayed.
*
* @see #setPreviewCallback(Camera.PreviewCallback)
* @see #setOneShotPreviewCallback(Camera.PreviewCallback)
* @see #setPreviewCallbackWithBuffer(Camera.PreviewCallback)
* @see #startPreview()
*/
public interface PreviewCallback
{
/**
* Called as preview frames are displayed. This callback is invoked
* on the event thread {@link #open(int)} was called from.
*
* @param data the contents of the preview frame in the format defined
* by {@link android.graphics.ImageFormat}, which can be queried
* with {@link android.hardware.Camera.Parameters#getPreviewFormat()}.
* If {@link android.hardware.Camera.Parameters#setPreviewFormat(int)}
* is never called, the default will be the YCbCr_420_SP
* (NV21) format.
* @param camera the Camera service object.
*/
void onPreviewFrame(byte[] data, Camera camera);
};
/**
* Starts capturing and drawing preview frames to the screen.
* Preview will not actually start until a surface is supplied
* with {@link #setPreviewDisplay(SurfaceHolder)} or
* {@link #setPreviewTexture(SurfaceTexture)}.
*
* <p>If {@link #setPreviewCallback(Camera.PreviewCallback)},
* {@link #setOneShotPreviewCallback(Camera.PreviewCallback)}, or
* {@link #setPreviewCallbackWithBuffer(Camera.PreviewCallback)} were
* called, {@link Camera.PreviewCallback#onPreviewFrame(byte[], Camera)}
* will be called when preview data becomes available.
*/
public native final void startPreview();
/**
* Stops capturing and drawing preview frames to the surface, and
* resets the camera for a future call to {@link #startPreview()}.
*/
public final void stopPreview() {
_stopPreview();
mFaceDetectionRunning = false;
mShutterCallback = null;
mRawImageCallback = null;
mPostviewCallback = null;
mJpegCallback = null;
mAutoFocusCallback = null;
}
private native final void _stopPreview();
/**
* Return current preview state.
*
* FIXME: Unhide before release
* @hide
*/
public native final boolean previewEnabled();
/**
* Installs a callback to be invoked for every preview frame in addition
* to displaying them on the screen. The callback will be repeatedly called
* for as long as preview is active. This method can be called at any time,
* even while preview is live. Any other preview callbacks are overridden.
*
* @param cb a callback object that receives a copy of each preview frame,
* or null to stop receiving callbacks.
*/
public final void setPreviewCallback(PreviewCallback cb) {
mPreviewCallback = cb;
mOneShot = false;
mWithBuffer = false;
// Always use one-shot mode. We fake camera preview mode by
// doing one-shot preview continuously.
setHasPreviewCallback(cb != null, false);
}
/**
* Installs a callback to be invoked for the next preview frame in addition
* to displaying it on the screen. After one invocation, the callback is
* cleared. This method can be called any time, even when preview is live.
* Any other preview callbacks are overridden.
*
* @param cb a callback object that receives a copy of the next preview frame,
* or null to stop receiving callbacks.
*/
public final void setOneShotPreviewCallback(PreviewCallback cb) {
mPreviewCallback = cb;
mOneShot = true;
mWithBuffer = false;
setHasPreviewCallback(cb != null, false);
}
private native final void setHasPreviewCallback(boolean installed, boolean manualBuffer);
/**
* Installs a callback to be invoked for every preview frame, using buffers
* supplied with {@link #addCallbackBuffer(byte[])}, in addition to
* displaying them on the screen. The callback will be repeatedly called
* for as long as preview is active and buffers are available.
* Any other preview callbacks are overridden.
*
* <p>The purpose of this method is to improve preview efficiency and frame
* rate by allowing preview frame memory reuse. You must call
* {@link #addCallbackBuffer(byte[])} at some point -- before or after
* calling this method -- or no callbacks will received.
*
* The buffer queue will be cleared if this method is called with a null
* callback, {@link #setPreviewCallback(Camera.PreviewCallback)} is called,
* or {@link #setOneShotPreviewCallback(Camera.PreviewCallback)} is called.
*
* @param cb a callback object that receives a copy of the preview frame,
* or null to stop receiving callbacks and clear the buffer queue.
* @see #addCallbackBuffer(byte[])
*/
public final void setPreviewCallbackWithBuffer(PreviewCallback cb) {
mPreviewCallback = cb;
mOneShot = false;
mWithBuffer = true;
setHasPreviewCallback(cb != null, true);
}
/**
* Adds a pre-allocated buffer to the preview callback buffer queue.
* Applications can add one or more buffers to the queue. When a preview
* frame arrives and there is still at least one available buffer, the
* buffer will be used and removed from the queue. Then preview callback is
* invoked with the buffer. If a frame arrives and there is no buffer left,
* the frame is discarded. Applications should add buffers back when they
* finish processing the data in them.
*
* <p>The size of the buffer is determined by multiplying the preview
* image width, height, and bytes per pixel. The width and height can be
* read from {@link Camera.Parameters#getPreviewSize()}. Bytes per pixel
* can be computed from
* {@link android.graphics.ImageFormat#getBitsPerPixel(int)} / 8,
* using the image format from {@link Camera.Parameters#getPreviewFormat()}.
*
* <p>This method is only necessary when
* {@link #setPreviewCallbackWithBuffer(PreviewCallback)} is used. When
* {@link #setPreviewCallback(PreviewCallback)} or
* {@link #setOneShotPreviewCallback(PreviewCallback)} are used, buffers
* are automatically allocated. When a supplied buffer is too small to
* hold the preview frame data, preview callback will return null and
* the buffer will be removed from the buffer queue.
*
* @param callbackBuffer the buffer to add to the queue.
* The size should be width * height * bits_per_pixel / 8.
* @see #setPreviewCallbackWithBuffer(PreviewCallback)
*/
public final void addCallbackBuffer(byte[] callbackBuffer)
{
_addCallbackBuffer(callbackBuffer, CAMERA_MSG_PREVIEW_FRAME);
}
/**
* Adds a pre-allocated buffer to the raw image callback buffer queue.
* Applications can add one or more buffers to the queue. When a raw image
* frame arrives and there is still at least one available buffer, the
* buffer will be used to hold the raw image data and removed from the
* queue. Then raw image callback is invoked with the buffer. If a raw
* image frame arrives but there is no buffer left, the frame is
* discarded. Applications should add buffers back when they finish
* processing the data in them by calling this method again in order
* to avoid running out of raw image callback buffers.
*
* <p>The size of the buffer is determined by multiplying the raw image
* width, height, and bytes per pixel. The width and height can be
* read from {@link Camera.Parameters#getPictureSize()}. Bytes per pixel
* can be computed from
* {@link android.graphics.ImageFormat#getBitsPerPixel(int)} / 8,
* using the image format from {@link Camera.Parameters#getPreviewFormat()}.
*
* <p>This method is only necessary when the PictureCallbck for raw image
* is used while calling {@link #takePicture(Camera.ShutterCallback,
* Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)}.
*
* <p>Please note that by calling this method, the mode for
* application-managed callback buffers is triggered. If this method has
* never been called, null will be returned by the raw image callback since
* there is no image callback buffer available. Furthermore, When a supplied
* buffer is too small to hold the raw image data, raw image callback will
* return null and the buffer will be removed from the buffer queue.
*
* @param callbackBuffer the buffer to add to the raw image callback buffer
* queue. The size should be width * height * (bits per pixel) / 8. An
* null callbackBuffer will be ignored and won't be added to the queue.
*
* @see #takePicture(Camera.ShutterCallback,
* Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)}.
*
* {@hide}
*/
public final void addRawImageCallbackBuffer(byte[] callbackBuffer)
{
addCallbackBuffer(callbackBuffer, CAMERA_MSG_RAW_IMAGE);
}
private final void addCallbackBuffer(byte[] callbackBuffer, int msgType)
{
// CAMERA_MSG_VIDEO_FRAME may be allowed in the future.
if (msgType != CAMERA_MSG_PREVIEW_FRAME &&
msgType != CAMERA_MSG_RAW_IMAGE) {
throw new IllegalArgumentException(
"Unsupported message type: " + msgType);
}
_addCallbackBuffer(callbackBuffer, msgType);
}
private native final void _addCallbackBuffer(
byte[] callbackBuffer, int msgType);
private class EventHandler extends Handler
{
private Camera mCamera;
public EventHandler(Camera c, Looper looper) {
super(looper);
mCamera = c;
}
@Override
public void handleMessage(Message msg) {
switch(msg.what) {
case CAMERA_MSG_SHUTTER:
if (mShutterCallback != null) {
mShutterCallback.onShutter();
}
return;
case CAMERA_MSG_RAW_IMAGE:
if (mRawImageCallback != null) {
mRawImageCallback.onPictureTaken((byte[])msg.obj, mCamera);
}
return;
case CAMERA_MSG_COMPRESSED_IMAGE:
if (mJpegCallback != null) {
mJpegCallback.onPictureTaken((byte[])msg.obj, mCamera);
}
return;
case CAMERA_MSG_PREVIEW_FRAME:
if (mPreviewCallback != null) {
PreviewCallback cb = mPreviewCallback;
if (mOneShot) {
// Clear the callback variable before the callback
// in case the app calls setPreviewCallback from
// the callback function
mPreviewCallback = null;
} else if (!mWithBuffer) {
// We're faking the camera preview mode to prevent
// the app from being flooded with preview frames.
// Set to oneshot mode again.
setHasPreviewCallback(true, false);
}
cb.onPreviewFrame((byte[])msg.obj, mCamera);
}
return;
case CAMERA_MSG_POSTVIEW_FRAME:
if (mPostviewCallback != null) {
mPostviewCallback.onPictureTaken((byte[])msg.obj, mCamera);
}
return;
case CAMERA_MSG_FOCUS:
if (mAutoFocusCallback != null) {
mAutoFocusCallback.onAutoFocus(msg.arg1 == 0 ? false : true, mCamera);
}
return;
case CAMERA_MSG_ZOOM:
if (mZoomListener != null) {
mZoomListener.onZoomChange(msg.arg1, msg.arg2 != 0, mCamera);
}
return;
case CAMERA_MSG_PREVIEW_METADATA:
if (mFaceListener != null) {
mFaceListener.onFaceDetection((Face[])msg.obj, mCamera);
}
return;
case CAMERA_MSG_ERROR :
Log.e(TAG, "Error " + msg.arg1);
if (mErrorCallback != null) {
mErrorCallback.onError(msg.arg1, mCamera);
}
return;
default:
Log.e(TAG, "Unknown message type " + msg.what);
return;
}
}
}
private static void postEventFromNative(Object camera_ref,
int what, int arg1, int arg2, Object obj)
{
Camera c = (Camera)((WeakReference)camera_ref).get();
if (c == null)
return;
if (c.mEventHandler != null) {
Message m = c.mEventHandler.obtainMessage(what, arg1, arg2, obj);
c.mEventHandler.sendMessage(m);
}
}
/**
* Callback interface used to notify on completion of camera auto focus.
*
* <p>Devices that do not support auto-focus will receive a "fake"
* callback to this interface. If your application needs auto-focus and
* should not be installed on devices <em>without</em> auto-focus, you must
* declare that your app uses the
* {@code android.hardware.camera.autofocus} feature, in the
* <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html"><uses-feature></a>
* manifest element.</p>
*
* @see #autoFocus(AutoFocusCallback)
*/
public interface AutoFocusCallback
{
/**
* Called when the camera auto focus completes. If the camera
* does not support auto-focus and autoFocus is called,
* onAutoFocus will be called immediately with a fake value of
* <code>success</code> set to <code>true</code>.
*
* The auto-focus routine does not lock auto-exposure and auto-white
* balance after it completes.
*
* @param success true if focus was successful, false if otherwise
* @param camera the Camera service object
* @see android.hardware.Camera.Parameters#setAutoExposureLock(boolean)
* @see android.hardware.Camera.Parameters#setAutoWhiteBalanceLock(boolean)
*/
void onAutoFocus(boolean success, Camera camera);
}
/**
* Starts camera auto-focus and registers a callback function to run when
* the camera is focused. This method is only valid when preview is active
* (between {@link #startPreview()} and before {@link #stopPreview()}).
*
* <p>Callers should check
* {@link android.hardware.Camera.Parameters#getFocusMode()} to determine if
* this method should be called. If the camera does not support auto-focus,
* it is a no-op and {@link AutoFocusCallback#onAutoFocus(boolean, Camera)}
* callback will be called immediately.
*
* <p>If your application should not be installed
* on devices without auto-focus, you must declare that your application
* uses auto-focus with the
* <a href="{@docRoot}guide/topics/manifest/uses-feature-element.html"><uses-feature></a>
* manifest element.</p>
*
* <p>If the current flash mode is not
* {@link android.hardware.Camera.Parameters#FLASH_MODE_OFF}, flash may be
* fired during auto-focus, depending on the driver and camera hardware.<p>
*
* <p>Auto-exposure lock {@link android.hardware.Camera.Parameters#getAutoExposureLock()}
* and auto-white balance locks {@link android.hardware.Camera.Parameters#getAutoWhiteBalanceLock()}
* do not change during and after autofocus. But auto-focus routine may stop
* auto-exposure and auto-white balance transiently during focusing.
*
* <p>Stopping preview with {@link #stopPreview()}, or triggering still
* image capture with {@link #takePicture(Camera.ShutterCallback,
* Camera.PictureCallback, Camera.PictureCallback)}, will not change the
* the focus position. Applications must call cancelAutoFocus to reset the
* focus.</p>
*
* @param cb the callback to run
* @see #cancelAutoFocus()
* @see android.hardware.Camera.Parameters#setAutoExposureLock(boolean)
* @see android.hardware.Camera.Parameters#setAutoWhiteBalanceLock(boolean)
*/
public final void autoFocus(AutoFocusCallback cb)
{
mAutoFocusCallback = cb;
native_autoFocus();
}
private native final void native_autoFocus();
/**
* Cancels any auto-focus function in progress.
* Whether or not auto-focus is currently in progress,
* this function will return the focus position to the default.
* If the camera does not support auto-focus, this is a no-op.
*
* @see #autoFocus(Camera.AutoFocusCallback)
*/
public final void cancelAutoFocus()
{
mAutoFocusCallback = null;
native_cancelAutoFocus();
}
private native final void native_cancelAutoFocus();
/**
* Callback interface used to signal the moment of actual image capture.
*
* @see #takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback)
*/
public interface ShutterCallback
{
/**
* Called as near as possible to the moment when a photo is captured
* from the sensor. This is a good opportunity to play a shutter sound
* or give other feedback of camera operation. This may be some time
* after the photo was triggered, but some time before the actual data
* is available.
*/
void onShutter();
}
/**
* Callback interface used to supply image data from a photo capture.
*
* @see #takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback)
*/
public interface PictureCallback {
/**
* Called when image data is available after a picture is taken.
* The format of the data depends on the context of the callback
* and {@link Camera.Parameters} settings.
*
* @param data a byte array of the picture data
* @param camera the Camera service object
*/
void onPictureTaken(byte[] data, Camera camera);
};
/**
* Equivalent to takePicture(shutter, raw, null, jpeg).
*
* @see #takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback)
*/
public final void takePicture(ShutterCallback shutter, PictureCallback raw,
PictureCallback jpeg) {
takePicture(shutter, raw, null, jpeg);
}
private native final void native_takePicture(int msgType);
/**
* Triggers an asynchronous image capture. The camera service will initiate
* a series of callbacks to the application as the image capture progresses.
* The shutter callback occurs after the image is captured. This can be used
* to trigger a sound to let the user know that image has been captured. The
* raw callback occurs when the raw image data is available (NOTE: the data
* will be null if there is no raw image callback buffer available or the
* raw image callback buffer is not large enough to hold the raw image).
* The postview callback occurs when a scaled, fully processed postview
* image is available (NOTE: not all hardware supports this). The jpeg
* callback occurs when the compressed image is available. If the
* application does not need a particular callback, a null can be passed
* instead of a callback method.
*
* <p>This method is only valid when preview is active (after
* {@link #startPreview()}). Preview will be stopped after the image is
* taken; callers must call {@link #startPreview()} again if they want to
* re-start preview or take more pictures. This should not be called between
* {@link android.media.MediaRecorder#start()} and
* {@link android.media.MediaRecorder#stop()}.
*
* <p>After calling this method, you must not call {@link #startPreview()}
* or take another picture until the JPEG callback has returned.
*
* @param shutter the callback for image capture moment, or null
* @param raw the callback for raw (uncompressed) image data, or null
* @param postview callback with postview image data, may be null
* @param jpeg the callback for JPEG image data, or null
*/
public final void takePicture(ShutterCallback shutter, PictureCallback raw,
PictureCallback postview, PictureCallback jpeg) {
mShutterCallback = shutter;
mRawImageCallback = raw;
mPostviewCallback = postview;
mJpegCallback = jpeg;
// If callback is not set, do not send me callbacks.
int msgType = 0;
if (mShutterCallback != null) {
msgType |= CAMERA_MSG_SHUTTER;
}
if (mRawImageCallback != null) {
msgType |= CAMERA_MSG_RAW_IMAGE;
}
if (mPostviewCallback != null) {
msgType |= CAMERA_MSG_POSTVIEW_FRAME;
}
if (mJpegCallback != null) {
msgType |= CAMERA_MSG_COMPRESSED_IMAGE;
}
native_takePicture(msgType);
}
/**
* Zooms to the requested value smoothly. The driver will notify {@link
* OnZoomChangeListener} of the zoom value and whether zoom is stopped at
* the time. For example, suppose the current zoom is 0 and startSmoothZoom
* is called with value 3. The
* {@link Camera.OnZoomChangeListener#onZoomChange(int, boolean, Camera)}
* method will be called three times with zoom values 1, 2, and 3.
* Applications can call {@link #stopSmoothZoom} to stop the zoom earlier.
* Applications should not call startSmoothZoom again or change the zoom
* value before zoom stops. If the supplied zoom value equals to the current
* zoom value, no zoom callback will be generated. This method is supported
* if {@link android.hardware.Camera.Parameters#isSmoothZoomSupported}
* returns true.
*
* @param value zoom value. The valid range is 0 to {@link
* android.hardware.Camera.Parameters#getMaxZoom}.
* @throws IllegalArgumentException if the zoom value is invalid.
* @throws RuntimeException if the method fails.
* @see #setZoomChangeListener(OnZoomChangeListener)
*/
public native final void startSmoothZoom(int value);
/**
* Stops the smooth zoom. Applications should wait for the {@link
* OnZoomChangeListener} to know when the zoom is actually stopped. This
* method is supported if {@link
* android.hardware.Camera.Parameters#isSmoothZoomSupported} is true.
*
* @throws RuntimeException if the method fails.
*/
public native final void stopSmoothZoom();
/**
* Set the clockwise rotation of preview display in degrees. This affects
* the preview frames and the picture displayed after snapshot. This method
* is useful for portrait mode applications. Note that preview display of
* front-facing cameras is flipped horizontally before the rotation, that
* is, the image is reflected along the central vertical axis of the camera
* sensor. So the users can see themselves as looking into a mirror.
*
* <p>This does not affect the order of byte array passed in {@link
* PreviewCallback#onPreviewFrame}, JPEG pictures, or recorded videos. This
* method is not allowed to be called during preview.
*
* <p>If you want to make the camera image show in the same orientation as
* the display, you can use the following code.
* <pre>
* public static void setCameraDisplayOrientation(Activity activity,
* int cameraId, android.hardware.Camera camera) {
* android.hardware.Camera.CameraInfo info =