-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathCameraLauncher.java
1396 lines (1233 loc) · 57.4 KB
/
CameraLauncher.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
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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 org.apache.cordova.camera;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.MediaScannerConnectionClient;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import androidx.core.content.FileProvider;
import android.util.Base64;
import android.system.Os;
import android.system.OsConstants;
import org.apache.cordova.BuildHelper;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.LOG;
import org.apache.cordova.PermissionHelper;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
/**
* This class launches the camera view, allows the user to take a picture, closes the camera view,
* and returns the captured image. When the camera view is closed, the screen displayed before
* the camera view was shown is redisplayed.
*/
public class CameraLauncher extends CordovaPlugin implements MediaScannerConnectionClient {
private static final int DATA_URL = 0; // Return base64 encoded string
private static final int FILE_URI = 1; // Return file uri (content://media/external/images/media/2 for Android)
private static final int PHOTOLIBRARY = 0; // Choose image from picture library (same as SAVEDPHOTOALBUM for Android)
private static final int CAMERA = 1; // Take picture from camera
private static final int SAVEDPHOTOALBUM = 2; // Choose image from picture library (same as PHOTOLIBRARY for Android)
private static final int PICTURE = 0; // allow selection of still pictures only. DEFAULT. Will return format specified via DestinationType
private static final int VIDEO = 1; // allow selection of video only, ONLY RETURNS URL
private static final int ALLMEDIA = 2; // allow selection from all media types
private static final int JPEG = 0; // Take a picture of type JPEG
private static final int PNG = 1; // Take a picture of type PNG
private static final String JPEG_TYPE = "jpg";
private static final String PNG_TYPE = "png";
private static final String JPEG_EXTENSION = "." + JPEG_TYPE;
private static final String PNG_EXTENSION = "." + PNG_TYPE;
private static final String PNG_MIME_TYPE = "image/png";
private static final String JPEG_MIME_TYPE = "image/jpeg";
private static final String HEIC_MIME_TYPE = "image/heic";
private static final String GET_PICTURE = "Get Picture";
private static final String GET_VIDEO = "Get Video";
private static final String GET_All = "Get All";
private static final String CROPPED_URI_KEY = "croppedUri";
private static final String IMAGE_URI_KEY = "imageUri";
private static final String TAKE_PICTURE_ACTION = "takePicture";
public static final int PERMISSION_DENIED_ERROR = 20;
public static final int TAKE_PIC_SEC = 0;
public static final int SAVE_TO_ALBUM_SEC = 1;
private static final String LOG_TAG = "CameraLauncher";
//Where did this come from?
private static final int CROP_CAMERA = 100;
private static final String TIME_FORMAT = "yyyyMMdd_HHmmss";
private int mQuality; // Compression quality hint (0-100: 0=low quality & high compression, 100=compress of max quality)
private int targetWidth; // desired width of the image
private int targetHeight; // desired height of the image
private Uri imageUri; // Uri of captured image
private int encodingType; // Type of encoding to use
private int mediaType; // What type of media to retrieve
private int destType; // Source type (needs to be saved for the permission handling)
private int srcType; // Destination type (needs to be saved for permission handling)
private boolean saveToPhotoAlbum; // Should the picture be saved to the device's photo album
private boolean correctOrientation; // Should the pictures orientation be corrected
private boolean orientationCorrected; // Has the picture's orientation been corrected
private boolean allowEdit; // Should we allow the user to crop the image.
public CallbackContext callbackContext;
private MediaScannerConnection conn; // Used to update gallery app with newly-written files
private Uri scanMe; // Uri of image to be added to content store
private Uri croppedUri;
private String croppedFilePath;
private ExifHelper exifData; // Exif data from source
private String applicationId;
/**
* Executes the request and returns PluginResult.
*
* @param action The action to execute.
* @param args JSONArry of arguments for the plugin.
* @param callbackContext The callback id used when calling back into JavaScript.
* @return A PluginResult object with a status and message.
*/
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
this.callbackContext = callbackContext;
this.applicationId = cordova.getContext().getPackageName();
this.applicationId = preferences.getString("applicationId", this.applicationId);
if (action.equals(TAKE_PICTURE_ACTION)) {
this.srcType = CAMERA;
this.destType = FILE_URI;
this.saveToPhotoAlbum = false;
this.targetHeight = 0;
this.targetWidth = 0;
this.encodingType = JPEG;
this.mediaType = PICTURE;
this.mQuality = 50;
//Take the values from the arguments if they're not already defined (this is tricky)
this.destType = args.getInt(1);
this.srcType = args.getInt(2);
this.mQuality = args.getInt(0);
this.targetWidth = args.getInt(3);
this.targetHeight = args.getInt(4);
this.encodingType = args.getInt(5);
this.mediaType = args.getInt(6);
this.allowEdit = args.getBoolean(7);
this.correctOrientation = args.getBoolean(8);
this.saveToPhotoAlbum = args.getBoolean(9);
// If the user specifies a 0 or smaller width/height
// make it -1 so later comparisons succeed
if (this.targetWidth < 1) {
this.targetWidth = -1;
}
if (this.targetHeight < 1) {
this.targetHeight = -1;
}
// We don't return full-quality PNG files. The camera outputs a JPEG
// so requesting it as a PNG provides no actual benefit
if (this.targetHeight == -1 && this.targetWidth == -1 && this.mQuality == 100 &&
!this.correctOrientation && this.encodingType == PNG && this.srcType == CAMERA) {
this.encodingType = JPEG;
}
try {
if (this.srcType == CAMERA) {
this.callTakePicture(destType, encodingType);
}
else if ((this.srcType == PHOTOLIBRARY) || (this.srcType == SAVEDPHOTOALBUM)) {
this.getImage(this.srcType, destType);
}
}
catch (IllegalStateException e)
{
callbackContext.error(e.getLocalizedMessage());
PluginResult r = new PluginResult(PluginResult.Status.ERROR);
callbackContext.sendPluginResult(r);
return true;
}
catch (IllegalArgumentException e)
{
callbackContext.error("Illegal Argument Exception");
PluginResult r = new PluginResult(PluginResult.Status.ERROR);
callbackContext.sendPluginResult(r);
return true;
}
PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
r.setKeepCallback(true);
callbackContext.sendPluginResult(r);
return true;
}
return false;
}
//--------------------------------------------------------------------------
// LOCAL METHODS
//--------------------------------------------------------------------------
private String getTempDirectoryPath() {
File cache = cordova.getActivity().getCacheDir();
// Create the cache directory if it doesn't exist
cache.mkdirs();
return cache.getAbsolutePath();
}
/**
* Take a picture with the camera.
* When an image is captured or the camera view is cancelled, the result is returned
* in CordovaActivity.onActivityResult, which forwards the result to this.onActivityResult.
*
* The image can either be returned as a base64 string or a URI that points to the file.
* To display base64 string in an img tag, set the source to:
* img.src="data:image/jpeg;base64,"+result;
* or to display URI in an img tag
* img.src=result;
*
* @param returnType Set the type of image to return.
* @param encodingType Compression quality hint (0-100: 0=low quality & high compression, 100=compress of max quality)
*/
public void callTakePicture(int returnType, int encodingType) throws IllegalStateException {
// CB-10120: The CAMERA permission does not need to be requested unless it is declared
// in AndroidManifest.xml. This plugin does not declare it, but others may and so we must
// check the package info to determine if the permission is present.
boolean manifestContainsCameraPermission = false;
// write permission is not necessary, unless if we are saving to photo album
// On API 29+ devices, write permission is completely obsolete and not required.
boolean manifestContainsWriteExternalPermission = false;
boolean cameraPermissionGranted = PermissionHelper.hasPermission(this, Manifest.permission.CAMERA);
boolean writeExternalPermissionGranted = false;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
writeExternalPermissionGranted = PermissionHelper.hasPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
else {
writeExternalPermissionGranted = true;
}
try {
PackageManager packageManager = this.cordova.getActivity().getPackageManager();
String[] permissionsInPackage = packageManager.getPackageInfo(this.cordova.getActivity().getPackageName(), PackageManager.GET_PERMISSIONS).requestedPermissions;
if (permissionsInPackage != null) {
for (String permission : permissionsInPackage) {
if (permission.equals(Manifest.permission.CAMERA)) {
manifestContainsCameraPermission = true;
}
else if (permission.equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
manifestContainsWriteExternalPermission = true;
}
}
}
} catch (NameNotFoundException e) {
// We are requesting the info for our package, so this should
// never be caught
}
ArrayList<String> requiredPermissions = new ArrayList<>();
if (manifestContainsCameraPermission && !cameraPermissionGranted) {
requiredPermissions.add(Manifest.permission.CAMERA);
}
if (saveToPhotoAlbum && !writeExternalPermissionGranted) {
// This block only applies for API 24-28
// because writeExternalPermissionGranted is always true on API 29+
if (!manifestContainsWriteExternalPermission) {
throw new IllegalStateException("WRITE_EXTERNAL_STORAGE permission not declared in AndroidManifest");
}
requiredPermissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (!requiredPermissions.isEmpty()) {
PermissionHelper.requestPermissions(this, TAKE_PIC_SEC, requiredPermissions.toArray(new String[0]));
}
else {
takePicture(returnType, encodingType);
}
}
public void takePicture(int returnType, int encodingType)
{
// Let's use the intent and see what happens
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Specify file so that large image is captured and returned
File photo = createCaptureFile(encodingType);
this.imageUri = FileProvider.getUriForFile(
cordova.getActivity(),
applicationId + ".cordova.plugin.camera.provider",
photo
);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
//We can write to this URI, this will hopefully allow us to write files to get to the next step
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
if (this.cordova != null) {
// Let's check to make sure the camera is actually installed. (Legacy Nexus 7 code)
PackageManager mPm = this.cordova.getActivity().getPackageManager();
if(intent.resolveActivity(mPm) != null)
{
this.cordova.startActivityForResult((CordovaPlugin) this, intent, (CAMERA + 1) * 16 + returnType + 1);
}
else
{
LOG.d(LOG_TAG, "Error: You don't have a default camera. Your device may not be CTS complaint.");
}
}
}
/**
* Create a file in the applications temporary directory based upon the supplied encoding.
*
* @param encodingType of the image to be taken
* @return a File object pointing to the temporary picture
*/
private File createCaptureFile(int encodingType) {
return createCaptureFile(encodingType, "");
}
/**
* Create a file in the applications temporary directory based upon the supplied encoding.
*
* @param encodingType of the image to be taken
* @param fileName or resultant File object.
* @return a File object pointing to the temporary picture
*/
private File createCaptureFile(int encodingType, String fileName) {
if (fileName.isEmpty()) {
fileName = ".Pic";
}
if (encodingType == JPEG) {
fileName = fileName + JPEG_EXTENSION;
} else if (encodingType == PNG) {
fileName = fileName + PNG_EXTENSION;
} else {
throw new IllegalArgumentException("Invalid Encoding Type: " + encodingType);
}
File cacheDir = new File(getTempDirectoryPath(), "org.apache.cordova.camera");
cacheDir.mkdir();
return new File(cacheDir, fileName);
}
/**
* Get image from photo library.
*
* @param srcType The album to get image from.
* @param returnType Set the type of image to return.
*/
// TODO: Images selected from SDCARD don't display correctly, but from CAMERA ALBUM do!
// TODO: Images from kitkat filechooser not going into crop function
public void getImage(int srcType, int returnType) {
Intent intent = new Intent();
String title = GET_PICTURE;
croppedUri = null;
croppedFilePath = null;
if (this.mediaType == PICTURE) {
intent.setType("image/*");
if (this.allowEdit) {
intent.setAction(Intent.ACTION_PICK);
intent.putExtra("crop", "true");
if (targetWidth > 0) {
intent.putExtra("outputX", targetWidth);
}
if (targetHeight > 0) {
intent.putExtra("outputY", targetHeight);
}
if (targetHeight > 0 && targetWidth > 0 && targetWidth == targetHeight) {
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
}
File croppedFile = createCaptureFile(JPEG);
croppedFilePath = croppedFile.getAbsolutePath();
croppedUri = Uri.fromFile(croppedFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, croppedUri);
} else {
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
}
} else if (this.mediaType == VIDEO) {
intent.setType("video/*");
title = GET_VIDEO;
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
} else if (this.mediaType == ALLMEDIA) {
// I wanted to make the type 'image/*, video/*' but this does not work on all versions
// of android so I had to go with the wildcard search.
intent.setType("*/*");
title = GET_All;
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
}
if (this.cordova != null) {
this.cordova.startActivityForResult((CordovaPlugin) this, Intent.createChooser(intent,
new String(title)), (srcType + 1) * 16 + returnType + 1);
}
}
/**
* Brings up the UI to perform crop on passed image URI
*
* @param picUri
*/
private void performCrop(Uri picUri, int destType, Intent cameraIntent) {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
// set crop properties
cropIntent.putExtra("crop", "true");
// indicate output X and Y
if (targetWidth > 0) {
cropIntent.putExtra("outputX", targetWidth);
}
if (targetHeight > 0) {
cropIntent.putExtra("outputY", targetHeight);
}
if (targetHeight > 0 && targetWidth > 0 && targetWidth == targetHeight) {
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
}
// create new file handle to get full resolution crop
croppedFilePath = createCaptureFile(this.encodingType, System.currentTimeMillis() + "").getAbsolutePath();
croppedUri = Uri.parse(croppedFilePath);
cropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
cropIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
cropIntent.putExtra("output", croppedUri);
// start the activity - we handle returning in onActivityResult
if (this.cordova != null) {
this.cordova.startActivityForResult((CordovaPlugin) this,
cropIntent, CROP_CAMERA + destType);
}
} catch (ActivityNotFoundException anfe) {
LOG.e(LOG_TAG, "Crop operation not supported on this device");
try {
processResultFromCamera(destType, cameraIntent);
} catch (IOException e) {
e.printStackTrace();
LOG.e(LOG_TAG, "Unable to write to file");
}
}
}
/**
* Applies all needed transformation to the image received from the camera.
*
* @param destType In which form should we return the image
* @param intent An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
*/
private void processResultFromCamera(int destType, Intent intent) throws IOException {
int rotate = 0;
// Create an ExifHelper to save the exif data that is lost during compression
ExifHelper exif = new ExifHelper();
InputStream input = null;
String mimeType;
if (this.allowEdit && this.croppedUri != null) {
input = new FileInputStream(this.croppedFilePath);
mimeType = FileHelper.getMimeTypeForExtension(this.croppedFilePath);
}
else {
input = cordova.getActivity().getContentResolver().openInputStream(imageUri);
mimeType = FileHelper.getMimeType(imageUri.toString(), cordova);
}
if (input == null) {
throw new IOException("Unable to open result source.");
}
byte[] sourceData = readData(input);
try {
if (this.encodingType == JPEG) {
try {
//We don't support PNG, so let's not pretend we do
exif.createInFile(new ByteArrayInputStream(sourceData));
exif.readExifData();
rotate = exif.getOrientation();
} catch (IOException e) {
e.printStackTrace();
}
}
Bitmap bitmap = null;
Uri galleryUri = null;
// CB-5479 When this option is given the unchanged image should be saved
// in the gallery and the modified image is saved in the temporary
// directory
if (this.saveToPhotoAlbum) {
GalleryPathVO galleryPathVO = getPicturesPath();
galleryUri = Uri.fromFile(new File(galleryPathVO.getGalleryPath()));
if (this.allowEdit && this.croppedUri != null) {
writeUncompressedImage(croppedUri, galleryUri);
} else {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
writeTakenPictureToGalleryLowerThanAndroidQ(galleryUri);
} else { // Android Q or higher
writeTakenPictureToGalleryStartingFromAndroidQ(galleryPathVO);
}
}
}
// If sending base64 image back
if (destType == DATA_URL) {
bitmap = getScaledAndRotatedBitmap(sourceData, mimeType);
if (bitmap == null) {
// Try to get the bitmap from intent.
bitmap = (Bitmap) intent.getExtras().get("data");
}
// Double-check the bitmap.
if (bitmap == null) {
LOG.d(LOG_TAG, "I either have an unreadable imageUri or null bitmap");
this.failPicture("Unable to create bitmap!");
return;
}
this.processPicture(bitmap, this.encodingType);
}
// If sending filename back
else if (destType == FILE_URI) {
// If all this is true we shouldn't compress the image.
if (this.targetHeight == -1 && this.targetWidth == -1 && this.mQuality == 100 &&
!this.correctOrientation) {
// If we saved the uncompressed photo to the album, we can just
// return the URI we already created
if (this.saveToPhotoAlbum) {
this.callbackContext.success(galleryUri.toString());
} else {
Uri uri = Uri.fromFile(createCaptureFile(this.encodingType, System.currentTimeMillis() + ""));
if (this.allowEdit && this.croppedUri != null) {
Uri croppedUri = Uri.parse(croppedFilePath);
writeUncompressedImage(croppedUri, uri);
} else {
Uri imageUri = this.imageUri;
writeUncompressedImage(imageUri, uri);
}
this.callbackContext.success(uri.toString());
}
} else {
Uri uri = Uri.fromFile(createCaptureFile(this.encodingType, System.currentTimeMillis() + ""));
bitmap = getScaledAndRotatedBitmap(sourceData, mimeType);
// Double-check the bitmap.
if (bitmap == null) {
LOG.d(LOG_TAG, "I either have an unreadable imageUri or null bitmap");
this.failPicture("Unable to create bitmap!");
return;
}
// Add compressed version of captured image to returned media store Uri
OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(uri);
CompressFormat compressFormat = getCompressFormatForEncodingType(encodingType);
bitmap.compress(compressFormat, this.mQuality, os);
os.close();
// Restore exif data to file
if (this.encodingType == JPEG) {
String exifPath;
exifPath = uri.getPath();
//We just finished rotating it by an arbitrary orientation, just make sure it's normal
if (rotate != ExifInterface.ORIENTATION_NORMAL)
exif.resetOrientation();
exif.createOutFile(exifPath);
exif.writeExifData();
}
// Send Uri back to JavaScript for viewing image
this.callbackContext.success(uri.toString());
}
} else {
throw new IllegalStateException();
}
this.cleanup(this.imageUri, galleryUri, bitmap);
bitmap = null;
input.close();
}
catch (Exception e) {
input.close();
throw e;
}
}
private void writeTakenPictureToGalleryLowerThanAndroidQ(Uri galleryUri) throws IOException {
writeUncompressedImage(imageUri, galleryUri);
refreshGallery(galleryUri);
}
private void writeTakenPictureToGalleryStartingFromAndroidQ(GalleryPathVO galleryPathVO) throws IOException {
// Starting from Android Q, working with the ACTION_MEDIA_SCANNER_SCAN_FILE intent is deprecated
// https://developer.android.com/reference/android/content/Intent#ACTION_MEDIA_SCANNER_SCAN_FILE
// we must start working with the MediaStore from Android Q on.
ContentResolver resolver = this.cordova.getActivity().getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, galleryPathVO.getGalleryFileName());
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, getMimetypeForEncodingType());
Uri galleryOutputUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
InputStream fileStream = org.apache.cordova.camera.FileHelper.getInputStreamFromUriString(imageUri.toString(), cordova);
writeUncompressedImage(fileStream, galleryOutputUri);
}
private CompressFormat getCompressFormatForEncodingType(int encodingType) {
return encodingType == JPEG ? CompressFormat.JPEG : CompressFormat.PNG;
}
private GalleryPathVO getPicturesPath() {
String timeStamp = new SimpleDateFormat(TIME_FORMAT).format(new Date());
String imageFileName = "IMG_" + timeStamp + getExtensionForEncodingType();
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
storageDir.mkdirs();
return new GalleryPathVO(storageDir.getAbsolutePath(), imageFileName);
}
private void refreshGallery(Uri contentUri) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
// Starting from Android Q, working with the ACTION_MEDIA_SCANNER_SCAN_FILE intent is deprecated
mediaScanIntent.setData(contentUri);
this.cordova.getActivity().sendBroadcast(mediaScanIntent);
}
/**
* Converts output image format int value to string value of mime type.
* @return String String value of mime type or empty string if mime type is not supported
*/
private String getMimetypeForEncodingType() {
if (encodingType == PNG) return PNG_MIME_TYPE;
if (encodingType == JPEG) return JPEG_MIME_TYPE;
return "";
}
private String outputModifiedBitmap(Bitmap bitmap, Uri uri, String mimeTypeOfOriginalFile) throws IOException {
// Some content: URIs do not map to file paths (e.g. picasa).
String realPath = FileHelper.getRealPath(uri, this.cordova);
String fileName = calculateModifiedBitmapOutputFileName(mimeTypeOfOriginalFile, realPath);
String modifiedPath = getTempDirectoryPath() + "/" + fileName;
OutputStream os = new FileOutputStream(modifiedPath);
CompressFormat compressFormat = getCompressFormatForEncodingType(this.encodingType);
bitmap.compress(compressFormat, this.mQuality, os);
os.close();
if (exifData != null && this.encodingType == JPEG) {
try {
if (this.correctOrientation && this.orientationCorrected) {
exifData.resetOrientation();
}
exifData.createOutFile(modifiedPath);
exifData.writeExifData();
exifData = null;
} catch (IOException e) {
e.printStackTrace();
}
}
return modifiedPath;
}
private String calculateModifiedBitmapOutputFileName(String mimeTypeOfOriginalFile, String realPath) {
if (realPath == null) {
return "modified" + getExtensionForEncodingType();
}
String fileName = realPath.substring(realPath.lastIndexOf('/') + 1);
if (getMimetypeForEncodingType().equals(mimeTypeOfOriginalFile)) {
return fileName;
}
// if the picture is not a jpeg or png, (a .heic for example) when processed to a bitmap
// the file extension is changed to the output format, f.e. an input file my_photo.heic could become my_photo.jpg
return fileName.substring(fileName.lastIndexOf(".") + 1) + getExtensionForEncodingType();
}
private String getExtensionForEncodingType() {
return this.encodingType == JPEG ? JPEG_EXTENSION : PNG_EXTENSION;
}
/**
* Applies all needed transformation to the image received from the gallery.
*
* @param destType In which form should we return the image
* @param intent An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
*/
private void processResultFromGallery(int destType, Intent intent) {
Uri uri = intent.getData();
if (uri == null) {
if (croppedUri != null) {
uri = croppedUri;
} else {
this.failPicture("null data from photo library");
return;
}
}
String uriString = uri.toString();
String mimeTypeOfGalleryFile = FileHelper.getMimeType(uriString, this.cordova);
InputStream input;
try {
input = cordova.getActivity().getContentResolver().openInputStream(uri);
} catch (FileNotFoundException e) {
this.failPicture("Unable to open gallery input stream");
return;
}
if (input == null) {
this.failPicture("Unable to open gallery input stream");
return;
}
try {
byte[] data = readData(input);
// If you ask for video or the selected file cannot be processed
// there will be no attempt to resize any returned data.
if (this.mediaType == VIDEO || !isImageMimeTypeProcessable(mimeTypeOfGalleryFile)) {
this.callbackContext.success(uriString);
} else {
Bitmap bitmap = null;
// This is a special case to just return the path as no scaling,
// rotating, nor compressing needs to be done
if (this.targetHeight == -1 && this.targetWidth == -1 &&
destType == FILE_URI && !this.correctOrientation &&
getMimetypeForEncodingType().equalsIgnoreCase(mimeTypeOfGalleryFile)) {
this.callbackContext.success(uriString);
} else {
try {
bitmap = getScaledAndRotatedBitmap(data, mimeTypeOfGalleryFile);
} catch (IOException e) {
e.printStackTrace();
}
if (bitmap == null) {
LOG.d(LOG_TAG, "I either have an unreadable uri or null bitmap");
this.failPicture("Unable to create bitmap!");
return;
}
// If sending base64 image back
if (destType == DATA_URL) {
this.processPicture(bitmap, this.encodingType);
}
// If sending filename back
else if (destType == FILE_URI) {
// Did we modify the image?
if ((this.targetHeight > 0 && this.targetWidth > 0) ||
(this.correctOrientation && this.orientationCorrected) ||
!mimeTypeOfGalleryFile.equalsIgnoreCase(getMimetypeForEncodingType())) {
try {
String modifiedPath = this.outputModifiedBitmap(bitmap, uri, mimeTypeOfGalleryFile);
// The modified image is cached by the app in order to get around this and not have to delete you
// application cache I'm adding the current system time to the end of the file url.
this.callbackContext.success("file://" + modifiedPath + "?" + System.currentTimeMillis());
} catch (Exception e) {
e.printStackTrace();
this.failPicture("Error retrieving image: " + e.getLocalizedMessage());
}
} else {
this.callbackContext.success(uriString);
}
}
if (bitmap != null) {
bitmap.recycle();
bitmap = null;
}
System.gc();
}
if (bitmap == null) {
LOG.d(LOG_TAG, "I either have an unreadable uri or null bitmap");
this.failPicture("Unable to create bitmap!");
return;
}
// If sending base64 image back
if (destType == DATA_URL) {
this.processPicture(bitmap, this.encodingType);
}
// If sending filename back
else if (destType == FILE_URI) {
// Did we modify the image?
if ( (this.targetHeight > 0 && this.targetWidth > 0) ||
(this.correctOrientation && this.orientationCorrected) ||
!mimeTypeOfGalleryFile.equalsIgnoreCase(getMimetypeForEncodingType()))
{
try {
String modifiedPath = this.outputModifiedBitmap(bitmap, uri, mimeTypeOfGalleryFile);
// The modified image is cached by the app in order to get around this and not have to delete you
// application cache I'm adding the current system time to the end of the file url.
this.callbackContext.success("file://" + modifiedPath + "?" + System.currentTimeMillis());
} catch (Exception e) {
e.printStackTrace();
this.failPicture("Error retrieving image: "+e.getLocalizedMessage());
}
} else {
this.callbackContext.success(uriString);
}
}
if (bitmap != null) {
bitmap.recycle();
bitmap = null;
}
System.gc();
}
input.close();
}
catch (Exception e) {
try {
input.close();
} catch (IOException ex) {
ex.printStackTrace();
}
this.failPicture(e.getLocalizedMessage());
}
}
/**
* JPEG, PNG and HEIC mime types (images) can be scaled, decreased in quantity, corrected by orientation.
* But f.e. an image/gif cannot be scaled, but is can be selected through the PHOTOLIBRARY.
*
* @param mimeType The mimeType to check
* @return if the mimeType is a processable image mime type
*/
private boolean isImageMimeTypeProcessable(String mimeType) {
return JPEG_MIME_TYPE.equalsIgnoreCase(mimeType) || PNG_MIME_TYPE.equalsIgnoreCase(mimeType)
|| HEIC_MIME_TYPE.equalsIgnoreCase(mimeType);
}
/**
* Called when the camera view exits.
*
* @param requestCode The request code originally supplied to startActivityForResult(),
* allowing you to identify who this result came from.
* @param resultCode The integer result code returned by the child activity through its setResult().
* @param intent An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
*/
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
// Get src and dest types from request code for a Camera Activity
int srcType = (requestCode / 16) - 1;
int destType = (requestCode % 16) - 1;
// If Camera Crop
if (requestCode >= CROP_CAMERA) {
if (resultCode == Activity.RESULT_OK) {
// Because of the inability to pass through multiple intents, this hack will allow us
// to pass arcane codes back.
destType = requestCode - CROP_CAMERA;
try {
processResultFromCamera(destType, intent);
} catch (IOException e) {
e.printStackTrace();
LOG.e(LOG_TAG, "Unable to write to file");
}
}// If cancelled
else if (resultCode == Activity.RESULT_CANCELED) {
this.failPicture("No Image Selected");
}
// If something else
else {
this.failPicture("Did not complete!");
}
}
// If CAMERA
else if (srcType == CAMERA) {
// If image available
if (resultCode == Activity.RESULT_OK) {
try {
if (this.allowEdit) {
Uri tmpFile = FileProvider.getUriForFile(cordova.getActivity(),
applicationId + ".cordova.plugin.camera.provider",
createCaptureFile(this.encodingType));
performCrop(tmpFile, destType, intent);
} else {
this.processResultFromCamera(destType, intent);
}
} catch (IOException e) {
e.printStackTrace();
this.failPicture("Error capturing image: "+e.getLocalizedMessage());
}
}
// If cancelled
else if (resultCode == Activity.RESULT_CANCELED) {
this.failPicture("No Image Selected");
}
// If something else
else {
this.failPicture("Did not complete!");
}
}
// If retrieving photo from library
else if ((srcType == PHOTOLIBRARY) || (srcType == SAVEDPHOTOALBUM)) {
if (resultCode == Activity.RESULT_OK && intent != null) {
final Intent i = intent;
final int finalDestType = destType;
cordova.getThreadPool().execute(new Runnable() {
public void run() {
processResultFromGallery(finalDestType, i);
}
});
} else if (resultCode == Activity.RESULT_CANCELED) {
this.failPicture("No Image Selected");
} else {
this.failPicture("Selection did not complete!");
}
}
}
private int exifToDegrees(int exifOrientation) {
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
return 90;
} else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
return 180;
} else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
return 270;
} else {
return 0;
}
}
/**
* Write an inputstream to local disk
*
* @param fis - The InputStream to write
* @param dest - Destination on disk to write to
* @throws FileNotFoundException
* @throws IOException
*/
private void writeUncompressedImage(InputStream fis, Uri dest) throws FileNotFoundException,
IOException {
OutputStream os = null;
try {
os = this.cordova.getActivity().getContentResolver().openOutputStream(dest);
byte[] buffer = new byte[4096];
int len;
while ((len = fis.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
os.flush();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
LOG.d(LOG_TAG, "Exception while closing output stream.");