/
CameraController1.java
1608 lines (1468 loc) · 56.9 KB
/
CameraController1.java
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
package net.sourceforge.opencamera.CameraController;
import net.sourceforge.opencamera.MyDebug;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusMoveCallback;
import android.location.Location;
import android.media.MediaRecorder;
import android.os.Build;
import android.os.Handler;
import android.util.Log;
import android.view.SurfaceHolder;
/** Provides support using Android's original camera API
* android.hardware.Camera.
*/
@SuppressWarnings("deprecation")
public class CameraController1 extends CameraController {
private static final String TAG = "CameraController1";
private Camera camera;
private int display_orientation;
private final Camera.CameraInfo camera_info = new Camera.CameraInfo();
private String iso_key;
private boolean frontscreen_flash;
private final ErrorCallback camera_error_cb;
private int n_burst; // number of expected burst images in this capture
private final List<byte []> pending_burst_images = new ArrayList<>(); // burst images that have been captured so far, but not yet sent to the application
private List<Integer> burst_exposures;
private boolean want_expo_bracketing;
private final static int max_expo_bracketing_n_images = 3; // could be more, but problem that Open Camera defaults to 2.0 bracketing stops, and max exposure compensation typically isn't more than 2.0
private int expo_bracketing_n_images = 3;
private double expo_bracketing_stops = 2.0;
/** Opens the camera device.
* @param cameraId Which camera to open (must be between 0 and CameraControllerManager1.getNumberOfCameras()-1).
* @param camera_error_cb onError() will be called if the camera closes due to serious error. No more calls to the CameraController1 object should be made (though a new one can be created, to try reopening the camera).
* @throws CameraControllerException if the camera device fails to open.
*/
public CameraController1(int cameraId, final ErrorCallback camera_error_cb) throws CameraControllerException {
super(cameraId);
if( MyDebug.LOG )
Log.d(TAG, "create new CameraController1: " + cameraId);
this.camera_error_cb = camera_error_cb;
try {
camera = Camera.open(cameraId);
}
catch(RuntimeException e) {
if( MyDebug.LOG )
Log.e(TAG, "failed to open camera");
e.printStackTrace();
throw new CameraControllerException();
}
if( camera == null ) {
// Although the documentation says Camera.open() should throw a RuntimeException, it seems that it some cases it can return null
// I've seen this in some crashes reported in Google Play; also see:
// http://stackoverflow.com/questions/12054022/camera-open-returns-null
if( MyDebug.LOG )
Log.e(TAG, "camera.open returned null");
throw new CameraControllerException();
}
try {
Camera.getCameraInfo(cameraId, camera_info);
}
catch(RuntimeException e) {
// Had reported RuntimeExceptions from Google Play
// also see http://stackoverflow.com/questions/22383708/java-lang-runtimeexception-fail-to-get-camera-info
if( MyDebug.LOG )
Log.e(TAG, "failed to get camera info");
e.printStackTrace();
this.release();
throw new CameraControllerException();
}
/*{
// TEST cam_mode workaround from http://stackoverflow.com/questions/7225571/camcorderprofile-quality-high-resolution-produces-green-flickering-video
if( MyDebug.LOG )
Log.d(TAG, "setting cam_mode workaround");
Camera.Parameters parameters = this.getParameters();
parameters.set("cam_mode", 1);
setCameraParameters(parameters);
}*/
final CameraErrorCallback camera_error_callback = new CameraErrorCallback();
camera.setErrorCallback(camera_error_callback);
/*{
// test error handling
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if( MyDebug.LOG )
Log.d(TAG, "test camera error");
camera_error_callback.onError(Camera.CAMERA_ERROR_SERVER_DIED, camera);
}
}, 5000);
}*/
}
@Override
public void onError() {
Log.e(TAG, "onError");
if( this.camera != null ) { // I got Google Play crash reports due to camera being null in v1.36
this.camera.release();
this.camera = null;
}
if( this.camera_error_cb != null ) {
// need to communicate the problem to the application
this.camera_error_cb.onError();
}
}
private class CameraErrorCallback implements Camera.ErrorCallback {
@Override
public void onError(int error, Camera cam) {
// n.b., as this is potentially serious error, we always log even if MyDebug.LOG is false
Log.e(TAG, "camera onError: " + error);
if( error == Camera.CAMERA_ERROR_SERVER_DIED ) {
Log.e(TAG, " CAMERA_ERROR_SERVER_DIED");
CameraController1.this.onError();
}
else if( error == Camera.CAMERA_ERROR_UNKNOWN ) {
Log.e(TAG, " CAMERA_ERROR_UNKNOWN ");
}
}
}
public void release() {
camera.release();
camera = null;
}
private Camera.Parameters getParameters() {
return camera.getParameters();
}
private void setCameraParameters(Camera.Parameters parameters) {
if( MyDebug.LOG )
Log.d(TAG, "setCameraParameters");
try {
camera.setParameters(parameters);
if( MyDebug.LOG )
Log.d(TAG, "done");
}
catch(RuntimeException e) {
// just in case something has gone wrong
if( MyDebug.LOG )
Log.d(TAG, "failed to set parameters");
e.printStackTrace();
count_camera_parameters_exception++;
}
}
private List<String> convertFlashModesToValues(List<String> supported_flash_modes) {
if( MyDebug.LOG ) {
Log.d(TAG, "convertFlashModesToValues()");
Log.d(TAG, "supported_flash_modes: " + supported_flash_modes);
}
List<String> output_modes = new ArrayList<>();
if( supported_flash_modes != null ) {
// also resort as well as converting
if( supported_flash_modes.contains(Camera.Parameters.FLASH_MODE_OFF) ) {
output_modes.add("flash_off");
if( MyDebug.LOG )
Log.d(TAG, " supports flash_off");
}
if( supported_flash_modes.contains(Camera.Parameters.FLASH_MODE_AUTO) ) {
output_modes.add("flash_auto");
if( MyDebug.LOG )
Log.d(TAG, " supports flash_auto");
}
if( supported_flash_modes.contains(Camera.Parameters.FLASH_MODE_ON) ) {
output_modes.add("flash_on");
if( MyDebug.LOG )
Log.d(TAG, " supports flash_on");
}
if( supported_flash_modes.contains(Camera.Parameters.FLASH_MODE_TORCH) ) {
output_modes.add("flash_torch");
if( MyDebug.LOG )
Log.d(TAG, " supports flash_torch");
}
if( supported_flash_modes.contains(Camera.Parameters.FLASH_MODE_RED_EYE) ) {
output_modes.add("flash_red_eye");
if( MyDebug.LOG )
Log.d(TAG, " supports flash_red_eye");
}
}
// Samsung Galaxy S7 at least for front camera has supported_flash_modes: auto, beach, portrait?!
// so rather than checking supported_flash_modes, we should check output_modes here
// this is always why we check whether the size is greater than 1, rather than 0 (this also matches
// the check we do in Preview.setupCameraParameters()).
if( output_modes.size() > 1 ) {
if( MyDebug.LOG )
Log.d(TAG, "flash supported");
}
else {
if( isFrontFacing() ) {
if( MyDebug.LOG )
Log.d(TAG, "front-screen with no flash");
output_modes.clear(); // clear any pre-existing mode (see note above about Samsung Galaxy S7)
output_modes.add("flash_off");
output_modes.add("flash_frontscreen_on");
}
else {
if( MyDebug.LOG )
Log.d(TAG, "no flash");
// probably best to not return any modes, rather than one mode (see note about about Samsung Galaxy S7)
output_modes.clear();
}
}
return output_modes;
}
private List<String> convertFocusModesToValues(List<String> supported_focus_modes) {
if( MyDebug.LOG )
Log.d(TAG, "convertFocusModesToValues()");
List<String> output_modes = new ArrayList<>();
if( supported_focus_modes != null ) {
// also resort as well as converting
if( supported_focus_modes.contains(Camera.Parameters.FOCUS_MODE_AUTO) ) {
output_modes.add("focus_mode_auto");
if( MyDebug.LOG ) {
Log.d(TAG, " supports focus_mode_auto");
}
}
if( supported_focus_modes.contains(Camera.Parameters.FOCUS_MODE_INFINITY) ) {
output_modes.add("focus_mode_infinity");
if( MyDebug.LOG )
Log.d(TAG, " supports focus_mode_infinity");
}
if( supported_focus_modes.contains(Camera.Parameters.FOCUS_MODE_MACRO) ) {
output_modes.add("focus_mode_macro");
if( MyDebug.LOG )
Log.d(TAG, " supports focus_mode_macro");
}
if( supported_focus_modes.contains(Camera.Parameters.FOCUS_MODE_AUTO) ) {
output_modes.add("focus_mode_locked");
if( MyDebug.LOG ) {
Log.d(TAG, " supports focus_mode_locked");
}
}
if( supported_focus_modes.contains(Camera.Parameters.FOCUS_MODE_FIXED) ) {
output_modes.add("focus_mode_fixed");
if( MyDebug.LOG )
Log.d(TAG, " supports focus_mode_fixed");
}
if( supported_focus_modes.contains(Camera.Parameters.FOCUS_MODE_EDOF) ) {
output_modes.add("focus_mode_edof");
if( MyDebug.LOG )
Log.d(TAG, " supports focus_mode_edof");
}
if( supported_focus_modes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) ) {
output_modes.add("focus_mode_continuous_picture");
if( MyDebug.LOG )
Log.d(TAG, " supports focus_mode_continuous_picture");
}
if( supported_focus_modes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO) ) {
output_modes.add("focus_mode_continuous_video");
if( MyDebug.LOG )
Log.d(TAG, " supports focus_mode_continuous_video");
}
}
return output_modes;
}
public String getAPI() {
return "Camera";
}
public CameraFeatures getCameraFeatures() {
if( MyDebug.LOG )
Log.d(TAG, "getCameraFeatures()");
Camera.Parameters parameters = this.getParameters();
CameraFeatures camera_features = new CameraFeatures();
camera_features.is_zoom_supported = parameters.isZoomSupported();
if( camera_features.is_zoom_supported ) {
camera_features.max_zoom = parameters.getMaxZoom();
try {
camera_features.zoom_ratios = parameters.getZoomRatios();
}
catch(NumberFormatException e) {
// crash java.lang.NumberFormatException: Invalid int: " 500" reported in v1.4 on device "es209ra", Android 4.1, 3 Jan 2014
// this is from java.lang.Integer.invalidInt(Integer.java:138) - unclear if this is a bug in Open Camera, all we can do for now is catch it
if( MyDebug.LOG )
Log.e(TAG, "NumberFormatException in getZoomRatios()");
e.printStackTrace();
camera_features.is_zoom_supported = false;
camera_features.max_zoom = 0;
camera_features.zoom_ratios = null;
}
}
camera_features.supports_face_detection = parameters.getMaxNumDetectedFaces() > 0;
// get available sizes
List<Camera.Size> camera_picture_sizes = parameters.getSupportedPictureSizes();
camera_features.picture_sizes = new ArrayList<>();
//camera_features.picture_sizes.add(new CameraController.Size(1920, 1080)); // test
for(Camera.Size camera_size : camera_picture_sizes) {
camera_features.picture_sizes.add(new CameraController.Size(camera_size.width, camera_size.height));
}
//camera_features.supported_flash_modes = parameters.getSupportedFlashModes(); // Android format
List<String> supported_flash_modes = parameters.getSupportedFlashModes(); // Android format
camera_features.supported_flash_values = convertFlashModesToValues(supported_flash_modes); // convert to our format (also resorts)
List<String> supported_focus_modes = parameters.getSupportedFocusModes(); // Android format
camera_features.supported_focus_values = convertFocusModesToValues(supported_focus_modes); // convert to our format (also resorts)
camera_features.max_num_focus_areas = parameters.getMaxNumFocusAreas();
camera_features.is_exposure_lock_supported = parameters.isAutoExposureLockSupported();
camera_features.is_video_stabilization_supported = parameters.isVideoStabilizationSupported();
camera_features.supports_expo_bracketing = true;
camera_features.max_expo_bracketing_n_images = max_expo_bracketing_n_images;
camera_features.min_exposure = parameters.getMinExposureCompensation();
camera_features.max_exposure = parameters.getMaxExposureCompensation();
camera_features.exposure_step = getExposureCompensationStep();
List<Camera.Size> camera_video_sizes = parameters.getSupportedVideoSizes();
if( camera_video_sizes == null ) {
// if null, we should use the preview sizes - see http://stackoverflow.com/questions/14263521/android-getsupportedvideosizes-allways-returns-null
if( MyDebug.LOG )
Log.d(TAG, "take video_sizes from preview sizes");
camera_video_sizes = parameters.getSupportedPreviewSizes();
}
camera_features.video_sizes = new ArrayList<>();
//camera_features.video_sizes.add(new CameraController.Size(1920, 1080)); // test
for(Camera.Size camera_size : camera_video_sizes) {
camera_features.video_sizes.add(new CameraController.Size(camera_size.width, camera_size.height));
}
List<Camera.Size> camera_preview_sizes = parameters.getSupportedPreviewSizes();
camera_features.preview_sizes = new ArrayList<>();
for(Camera.Size camera_size : camera_preview_sizes) {
camera_features.preview_sizes.add(new CameraController.Size(camera_size.width, camera_size.height));
}
if( MyDebug.LOG )
Log.d(TAG, "camera parameters: " + parameters.flatten());
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 ) {
// Camera.canDisableShutterSound requires JELLY_BEAN_MR1 or greater
camera_features.can_disable_shutter_sound = camera_info.canDisableShutterSound;
}
else {
camera_features.can_disable_shutter_sound = false;
}
// Determine view angles. Note that these can vary based on the resolution - and since we read these before the caller has
// set the desired resolution, this isn't strictly correct. However these are presumably view angles for the photo anyway,
// when some callers (e.g., DrawPreview) want view angles for the preview anyway - so these will only be an approximation for
// what we want anyway.
final float default_view_angle_x = 55.0f;
final float default_view_angle_y = 43.0f;
try {
camera_features.view_angle_x = parameters.getHorizontalViewAngle();
camera_features.view_angle_y = parameters.getVerticalViewAngle();
}
catch(Exception e) {
// apparently some devices throw exceptions...
e.printStackTrace();
Log.e(TAG, "exception reading horizontal or vertical view angles");
camera_features.view_angle_x = default_view_angle_x;
camera_features.view_angle_y = default_view_angle_y;
}
if( MyDebug.LOG ) {
Log.d(TAG, "view_angle_x: " + camera_features.view_angle_x);
Log.d(TAG, "view_angle_y: " + camera_features.view_angle_y);
}
// need to handle some devices reporting rubbish
if( camera_features.view_angle_x > 150.0f || camera_features.view_angle_y > 150.0f ) {
Log.e(TAG, "camera API reporting stupid view angles, set to sensible defaults");
camera_features.view_angle_x = default_view_angle_x;
camera_features.view_angle_y = default_view_angle_y;
}
return camera_features;
}
public long getDefaultExposureTime() {
// not supported for CameraController1
return 0L;
}
// important, from docs:
// "Changing scene mode may override other parameters (such as flash mode, focus mode, white balance).
// For example, suppose originally flash mode is on and supported flash modes are on/off. In night
// scene mode, both flash mode and supported flash mode may be changed to off. After setting scene
// mode, applications should call getParameters to know if some parameters are changed."
public SupportedValues setSceneMode(String value) {
String default_value = getDefaultSceneMode();
Camera.Parameters parameters = this.getParameters();
List<String> values = parameters.getSupportedSceneModes();
/*{
// test
values = new ArrayList<>();
values.add("auto");
}*/
SupportedValues supported_values = checkModeIsSupported(values, value, default_value);
if( supported_values != null ) {
String scene_mode = parameters.getSceneMode();
// if scene mode is null, it should mean scene modes aren't supported anyway
if( scene_mode != null && !scene_mode.equals(supported_values.selected_value) ) {
parameters.setSceneMode(supported_values.selected_value);
setCameraParameters(parameters);
}
}
return supported_values;
}
public String getSceneMode() {
Camera.Parameters parameters = this.getParameters();
return parameters.getSceneMode();
}
public SupportedValues setColorEffect(String value) {
String default_value = getDefaultColorEffect();
Camera.Parameters parameters = this.getParameters();
List<String> values = parameters.getSupportedColorEffects();
SupportedValues supported_values = checkModeIsSupported(values, value, default_value);
if( supported_values != null ) {
if( !parameters.getColorEffect().equals(supported_values.selected_value) ) {
parameters.setColorEffect(supported_values.selected_value);
setCameraParameters(parameters);
}
}
return supported_values;
}
public String getColorEffect() {
Camera.Parameters parameters = this.getParameters();
return parameters.getColorEffect();
}
public SupportedValues setWhiteBalance(String value) {
String default_value = getDefaultWhiteBalance();
Camera.Parameters parameters = this.getParameters();
List<String> values = parameters.getSupportedWhiteBalance();
if( values != null ) {
// Some devices (e.g., OnePlus 3T) claim to support a "manual" mode, even though this
// isn't one of the possible white balances defined in Camera.Parameters.
// Since the old API doesn't support white balance temperatures, and this mode seems to
// have no useful effect, we remove it to avoid confusion.
while( values.contains("manual") ) {
values.remove("manual");
}
}
SupportedValues supported_values = checkModeIsSupported(values, value, default_value);
if( supported_values != null ) {
if( !parameters.getWhiteBalance().equals(supported_values.selected_value) ) {
parameters.setWhiteBalance(supported_values.selected_value);
setCameraParameters(parameters);
}
}
return supported_values;
}
public String getWhiteBalance() {
Camera.Parameters parameters = this.getParameters();
return parameters.getWhiteBalance();
}
@Override
public boolean setWhiteBalanceTemperature(int temperature) {
// not supported for CameraController1
return false;
}
@Override
public int getWhiteBalanceTemperature() {
// not supported for CameraController1
return 0;
}
@Override
public SupportedValues setISO(String value) {
Camera.Parameters parameters = this.getParameters();
// get available isos - no standard value for this, see http://stackoverflow.com/questions/2978095/android-camera-api-iso-setting
String iso_values = parameters.get("iso-values");
if( iso_values == null ) {
iso_values = parameters.get("iso-mode-values"); // Galaxy Nexus
if( iso_values == null ) {
iso_values = parameters.get("iso-speed-values"); // Micromax A101
if( iso_values == null )
iso_values = parameters.get("nv-picture-iso-values"); // LG dual P990
}
}
List<String> values = null;
if( iso_values != null && iso_values.length() > 0 ) {
if( MyDebug.LOG )
Log.d(TAG, "iso_values: " + iso_values);
String [] isos_array = iso_values.split(",");
// split shouldn't return null
if( isos_array.length > 0 ) {
// remove duplicates (OnePlus 3T has several duplicate "auto" entries)
HashSet<String> hashSet = new HashSet<>();
values = new ArrayList<>();
// use hashset for efficiency
// make sure we alo preserve the order
for(String iso : isos_array) {
if( !hashSet.contains(iso) ) {
values.add(iso);
hashSet.add(iso);
}
}
}
}
iso_key = "iso";
if( parameters.get(iso_key) == null ) {
iso_key = "iso-speed"; // Micromax A101
if( parameters.get(iso_key) == null ) {
iso_key = "nv-picture-iso"; // LG dual P990
if( parameters.get(iso_key) == null ) {
if ( Build.MODEL.contains("Z00") )
iso_key = "iso"; // Asus Zenfone 2 Z00A and Z008: see https://sourceforge.net/p/opencamera/tickets/183/
else
iso_key = null; // not supported
}
}
}
/*values = new ArrayList<>();
//values.add("auto");
//values.add("ISO_HJR");
values.add("ISO50");
values.add("ISO64");
values.add("ISO80");
values.add("ISO100");
values.add("ISO125");
values.add("ISO160");
values.add("ISO200");
values.add("ISO250");
values.add("ISO320");
values.add("ISO400");
values.add("ISO500");
values.add("ISO640");
values.add("ISO800");
values.add("ISO1000");
values.add("ISO1250");
values.add("ISO1600");
values.add("ISO2000");
values.add("ISO2500");
values.add("ISO3200");
values.add("auto");
//values.add("400");
//values.add("800");
//values.add("1600");
iso_key = "iso";*/
if( iso_key != null ){
if( values == null ) {
// set a default for some devices which have an iso_key, but don't give a list of supported ISOs
values = new ArrayList<>();
values.add("auto");
values.add("50");
values.add("100");
values.add("200");
values.add("400");
values.add("800");
values.add("1600");
}
SupportedValues supported_values = checkModeIsSupported(values, value, getDefaultISO());
if( supported_values != null ) {
if( MyDebug.LOG )
Log.d(TAG, "set: " + iso_key + " to: " + supported_values.selected_value);
parameters.set(iso_key, supported_values.selected_value);
setCameraParameters(parameters);
}
return supported_values;
}
return null;
}
@Override
public String getISOKey() {
if( MyDebug.LOG )
Log.d(TAG, "getISOKey");
return this.iso_key;
}
@Override
public void setManualISO(boolean manual_iso, int iso) {
// not supported for CameraController1
}
@Override
public boolean isManualISO() {
// not supported for CameraController1
return false;
}
@Override
public boolean setISO(int iso) {
// not supported for CameraController1
return false;
}
@Override
public int getISO() {
// not supported for CameraController1
return 0;
}
@Override
public long getExposureTime() {
// not supported for CameraController1
return 0L;
}
@Override
public boolean setExposureTime(long exposure_time) {
// not supported for CameraController1
return false;
}
@Override
public CameraController.Size getPictureSize() {
Camera.Parameters parameters = this.getParameters();
Camera.Size camera_size = parameters.getPictureSize();
return new CameraController.Size(camera_size.width, camera_size.height);
}
@Override
public void setPictureSize(int width, int height) {
Camera.Parameters parameters = this.getParameters();
parameters.setPictureSize(width, height);
if( MyDebug.LOG )
Log.d(TAG, "set picture size: " + parameters.getPictureSize().width + ", " + parameters.getPictureSize().height);
setCameraParameters(parameters);
}
@Override
public CameraController.Size getPreviewSize() {
Camera.Parameters parameters = this.getParameters();
Camera.Size camera_size = parameters.getPreviewSize();
return new CameraController.Size(camera_size.width, camera_size.height);
}
@Override
public void setPreviewSize(int width, int height) {
Camera.Parameters parameters = this.getParameters();
if( MyDebug.LOG )
Log.d(TAG, "current preview size: " + parameters.getPreviewSize().width + ", " + parameters.getPreviewSize().height);
parameters.setPreviewSize(width, height);
if( MyDebug.LOG )
Log.d(TAG, "new preview size: " + parameters.getPreviewSize().width + ", " + parameters.getPreviewSize().height);
setCameraParameters(parameters);
}
@Override
public void setExpoBracketing(boolean want_expo_bracketing) {
if( MyDebug.LOG )
Log.d(TAG, "setExpoBracketing: " + want_expo_bracketing);
if( camera == null ) {
if( MyDebug.LOG )
Log.e(TAG, "no camera");
return;
}
if( this.want_expo_bracketing == want_expo_bracketing ) {
return;
}
this.want_expo_bracketing = want_expo_bracketing;
}
@Override
public void setExpoBracketingNImages(int n_images) {
if( MyDebug.LOG )
Log.d(TAG, "setExpoBracketingNImages: " + n_images);
if( n_images <= 1 || (n_images % 2) == 0 ) {
if( MyDebug.LOG )
Log.e(TAG, "n_images should be an odd number greater than 1");
throw new RuntimeException(); // throw as RuntimeException, as this is a programming error
}
if( n_images > max_expo_bracketing_n_images ) {
n_images = max_expo_bracketing_n_images;
if( MyDebug.LOG )
Log.e(TAG, "limiting n_images to max of " + n_images);
}
this.expo_bracketing_n_images = n_images;
}
@Override
public void setExpoBracketingStops(double stops) {
if( MyDebug.LOG )
Log.d(TAG, "setExpoBracketingStops: " + stops);
if( stops <= 0.0 ) {
if( MyDebug.LOG )
Log.e(TAG, "stops should be positive");
throw new RuntimeException(); // throw as RuntimeException, as this is a programming error
}
this.expo_bracketing_stops = stops;
}
@Override
public void setUseExpoFastBurst(boolean use_expo_fast_burst) {
// not supported for CameraController1
}
@Override
public void setOptimiseAEForDRO(boolean optimise_ae_for_dro) {
// not supported for CameraController1
}
@Override
public void setRaw(boolean want_raw) {
// not supported for CameraController1
}
@Override
public void setVideoStabilization(boolean enabled) {
Camera.Parameters parameters = this.getParameters();
parameters.setVideoStabilization(enabled);
setCameraParameters(parameters);
}
public boolean getVideoStabilization() {
Camera.Parameters parameters = this.getParameters();
return parameters.getVideoStabilization();
}
public int getJpegQuality() {
Camera.Parameters parameters = this.getParameters();
return parameters.getJpegQuality();
}
public void setJpegQuality(int quality) {
Camera.Parameters parameters = this.getParameters();
parameters.setJpegQuality(quality);
setCameraParameters(parameters);
}
public int getZoom() {
Camera.Parameters parameters = this.getParameters();
return parameters.getZoom();
}
public void setZoom(int value) {
Camera.Parameters parameters = this.getParameters();
if( MyDebug.LOG )
Log.d(TAG, "zoom was: " + parameters.getZoom());
parameters.setZoom(value);
setCameraParameters(parameters);
}
public int getExposureCompensation() {
Camera.Parameters parameters = this.getParameters();
return parameters.getExposureCompensation();
}
private float getExposureCompensationStep() {
float exposure_step;
Camera.Parameters parameters = this.getParameters();
try {
exposure_step = parameters.getExposureCompensationStep();
}
catch(Exception e) {
// received a NullPointerException from StringToReal.parseFloat() beneath getExposureCompensationStep() on Google Play!
if( MyDebug.LOG )
Log.e(TAG, "exception from getExposureCompensationStep()");
e.printStackTrace();
exposure_step = 1.0f/3.0f; // make up a typical example
}
return exposure_step;
}
// Returns whether exposure was modified
public boolean setExposureCompensation(int new_exposure) {
Camera.Parameters parameters = this.getParameters();
int current_exposure = parameters.getExposureCompensation();
if( new_exposure != current_exposure ) {
if( MyDebug.LOG )
Log.d(TAG, "change exposure from " + current_exposure + " to " + new_exposure);
parameters.setExposureCompensation(new_exposure);
setCameraParameters(parameters);
return true;
}
return false;
}
public void setPreviewFpsRange(int min, int max) {
if( MyDebug.LOG )
Log.d(TAG, "setPreviewFpsRange: " + min + " to " + max);
Camera.Parameters parameters = this.getParameters();
parameters.setPreviewFpsRange(min, max);
setCameraParameters(parameters);
}
public List<int []> getSupportedPreviewFpsRange() {
Camera.Parameters parameters = this.getParameters();
try {
return parameters.getSupportedPreviewFpsRange();
}
catch(StringIndexOutOfBoundsException e) {
/* Have had reports of StringIndexOutOfBoundsException on Google Play on Sony Xperia M devices
at android.hardware.Camera$Parameters.splitRange(Camera.java:4098)
at android.hardware.Camera$Parameters.getSupportedPreviewFpsRange(Camera.java:2799)
*/
e.printStackTrace();
if( MyDebug.LOG ) {
Log.e(TAG, "getSupportedPreviewFpsRange() gave StringIndexOutOfBoundsException");
}
}
return null;
}
@Override
public void setFocusValue(String focus_value) {
Camera.Parameters parameters = this.getParameters();
switch(focus_value) {
case "focus_mode_auto":
case "focus_mode_locked":
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
break;
case "focus_mode_infinity":
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY);
break;
case "focus_mode_macro":
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);
break;
case "focus_mode_fixed":
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_FIXED);
break;
case "focus_mode_edof":
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_EDOF);
break;
case "focus_mode_continuous_picture":
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
break;
case "focus_mode_continuous_video":
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
break;
default:
if (MyDebug.LOG)
Log.d(TAG, "setFocusValue() received unknown focus value " + focus_value);
break;
}
setCameraParameters(parameters);
}
private String convertFocusModeToValue(String focus_mode) {
// focus_mode may be null on some devices; we return ""
if( MyDebug.LOG )
Log.d(TAG, "convertFocusModeToValue: " + focus_mode);
String focus_value = "";
if( focus_mode == null ) {
// ignore, leave focus_value at ""
}
else if( focus_mode.equals(Camera.Parameters.FOCUS_MODE_AUTO) ) {
focus_value = "focus_mode_auto";
}
else if( focus_mode.equals(Camera.Parameters.FOCUS_MODE_INFINITY) ) {
focus_value = "focus_mode_infinity";
}
else if( focus_mode.equals(Camera.Parameters.FOCUS_MODE_MACRO) ) {
focus_value = "focus_mode_macro";
}
else if( focus_mode.equals(Camera.Parameters.FOCUS_MODE_FIXED) ) {
focus_value = "focus_mode_fixed";
}
else if( focus_mode.equals(Camera.Parameters.FOCUS_MODE_EDOF) ) {
focus_value = "focus_mode_edof";
}
else if( focus_mode.equals(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) ) {
focus_value = "focus_mode_continuous_picture";
}
else if( focus_mode.equals(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO) ) {
focus_value = "focus_mode_continuous_video";
}
return focus_value;
}
@Override
public String getFocusValue() {
// returns "" if Parameters.getFocusMode() returns null
Camera.Parameters parameters = this.getParameters();
String focus_mode = parameters.getFocusMode();
// getFocusMode() is documented as never returning null, however I've had null pointer exceptions reported in Google Play
return convertFocusModeToValue(focus_mode);
}
@Override
public float getFocusDistance() {
// not supported for CameraController1!
return 0.0f;
}
@Override
public boolean setFocusDistance(float focus_distance) {
// not supported for CameraController1!
return false;
}
private String convertFlashValueToMode(String flash_value) {
String flash_mode = "";
switch(flash_value) {
case "flash_off":
flash_mode = Camera.Parameters.FLASH_MODE_OFF;
break;
case "flash_auto":
flash_mode = Camera.Parameters.FLASH_MODE_AUTO;
break;
case "flash_on":
flash_mode = Camera.Parameters.FLASH_MODE_ON;
break;
case "flash_torch":
flash_mode = Camera.Parameters.FLASH_MODE_TORCH;
break;
case "flash_red_eye":
flash_mode = Camera.Parameters.FLASH_MODE_RED_EYE;
break;
case "flash_frontscreen_on":
flash_mode = Camera.Parameters.FLASH_MODE_OFF;
break;
}
return flash_mode;
}
public void setFlashValue(String flash_value) {
Camera.Parameters parameters = this.getParameters();
if( MyDebug.LOG )
Log.d(TAG, "setFlashValue: " + flash_value);
this.frontscreen_flash = false;
if( flash_value.equals("flash_frontscreen_on") ) {
// we do this check first due to weird behaviour on Samsung Galaxy S7 front camera where parameters.getFlashMode() returns values (auto, beach, portrait)
this.frontscreen_flash = true;
return;
}
if( parameters.getFlashMode() == null ) {
if( MyDebug.LOG )
Log.d(TAG, "flash mode not supported");
return;
}
final String flash_mode = convertFlashValueToMode(flash_value);
if( flash_mode.length() > 0 && !flash_mode.equals(parameters.getFlashMode()) ) {
if( parameters.getFlashMode().equals(Camera.Parameters.FLASH_MODE_TORCH) && !flash_mode.equals(Camera.Parameters.FLASH_MODE_OFF) ) {
// workaround for bug on Nexus 5 and Nexus 6 where torch doesn't switch off until we set FLASH_MODE_OFF
if( MyDebug.LOG )
Log.d(TAG, "first turn torch off");
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
setCameraParameters(parameters);
// need to set the correct flash mode after a delay
Handler handler = new Handler();
handler.postDelayed(new Runnable(){
@Override
public void run(){
if( MyDebug.LOG )
Log.d(TAG, "now set actual flash mode after turning torch off");
if( camera != null ) { // make sure camera wasn't released in the meantime (has a Google Play crash as a result of this)
Camera.Parameters parameters = getParameters();
parameters.setFlashMode(flash_mode);
setCameraParameters(parameters);
}
}
}, 100);
}
else {
parameters.setFlashMode(flash_mode);
setCameraParameters(parameters);
}
}
}
private String convertFlashModeToValue(String flash_mode) {
// flash_mode may be null, meaning flash isn't supported; we return ""
if( MyDebug.LOG )
Log.d(TAG, "convertFlashModeToValue: " + flash_mode);
String flash_value = "";
if( flash_mode == null ) {
// ignore, leave focus_value at ""
}
else if( flash_mode.equals(Camera.Parameters.FLASH_MODE_OFF) ) {
flash_value = "flash_off";
}
else if( flash_mode.equals(Camera.Parameters.FLASH_MODE_AUTO) ) {
flash_value = "flash_auto";
}
else if( flash_mode.equals(Camera.Parameters.FLASH_MODE_ON) ) {
flash_value = "flash_on";
}
else if( flash_mode.equals(Camera.Parameters.FLASH_MODE_TORCH) ) {
flash_value = "flash_torch";
}
else if( flash_mode.equals(Camera.Parameters.FLASH_MODE_RED_EYE) ) {
flash_value = "flash_red_eye";
}
return flash_value;
}