41
41
42
42
/**
43
43
* The aim of this service is to secretly take pictures (without preview or opening device's camera app)
44
- * from all available cameras.
44
+ * from all available cameras using Android Camera 2 API
45
45
*
46
46
* @author hzitoun ([email protected] )
47
47
*/
48
48
49
- @ TargetApi (Build .VERSION_CODES .LOLLIPOP ) //camera 2 api was added in API level 21
49
+ @ TargetApi (Build .VERSION_CODES .LOLLIPOP ) //NOTE: camera 2 api was added in API level 21
50
50
public class PictureCapturingServiceImpl extends APictureCapturingService {
51
51
52
52
private static final String TAG = PictureCapturingServiceImpl .class .getSimpleName ();
53
53
54
- private boolean cameraClosed ;
55
54
private CameraDevice cameraDevice ;
56
55
private ImageReader imageReader ;
56
+ /***
57
+ * camera ids queue.
58
+ */
59
+ private Queue <String > cameraIds ;
60
+ private String currentCameraId ;
61
+ private boolean cameraClosed ;
62
+ /**
63
+ * stores a sorted map of (pictureUrlOnDisk, PictureData).
64
+ */
57
65
private TreeMap <String , byte []> picturesTaken ;
58
66
private PictureCapturingListener capturingListener ;
59
- private String currentCameraId ;
60
- //camera ids queue;
61
- private Queue <String > cameraIds ;
62
-
63
67
64
68
/***
65
- * private constructor, meant to force the use of {@see getInstance} method
69
+ * private constructor, meant to force the use of {@link # getInstance} method
66
70
*/
67
71
private PictureCapturingServiceImpl (final Activity activity ) {
68
72
super (activity );
69
73
}
70
-
71
- /**
74
+
75
+ /**
72
76
* @param activity the activity used to get the app's context and the display manager
73
77
* @return a new instance
74
78
*/
@@ -92,9 +96,10 @@ public void startCapturing(final PictureCapturingListener listener) {
92
96
this .currentCameraId = this .cameraIds .poll ();
93
97
openCamera ();
94
98
} else {
99
+ //No camera detected!
95
100
capturingListener .onDoneCapturingAllPhotos (picturesTaken );
96
101
}
97
- } catch (CameraAccessException e ) {
102
+ } catch (final CameraAccessException e ) {
98
103
Log .e (TAG , "Exception occurred while accessing the list of cameras" , e );
99
104
}
100
105
}
@@ -109,7 +114,7 @@ private void openCamera() {
109
114
== PackageManager .PERMISSION_GRANTED ) {
110
115
manager .openCamera (currentCameraId , stateCallback , null );
111
116
}
112
- } catch (CameraAccessException e ) {
117
+ } catch (final CameraAccessException e ) {
113
118
Log .e (TAG , " exception occurred while opening camera " + currentCameraId , e );
114
119
}
115
120
}
@@ -146,13 +151,12 @@ public void onOpened(@NonNull CameraDevice camera) {
146
151
Log .i (TAG , "Taking picture from camera " + camera .getId ());
147
152
//Take the picture after some delay. It may resolve getting a black dark photos.
148
153
new Handler ().postDelayed (() -> {
149
- try {
150
- takePicture ();
151
- } catch (CameraAccessException e ) {
152
- Log .e (TAG , " exception occurred while taking picture from " + currentCameraId , e );
153
- }
154
- }
155
- , 500 );
154
+ try {
155
+ takePicture ();
156
+ } catch (final CameraAccessException e ) {
157
+ Log .e (TAG , " exception occurred while taking picture from " + currentCameraId , e );
158
+ }
159
+ }, 500 );
156
160
}
157
161
158
162
@ Override
@@ -255,5 +259,4 @@ private void closeCamera() {
255
259
}
256
260
257
261
258
-
259
262
}
0 commit comments