@@ -9,89 +9,93 @@ var REQUEST_IMAGE_CAPTURE = 3453;
99export var takePicture = function ( options ?) : Promise < any > {
1010 return new Promise ( ( resolve , reject ) => {
1111 try {
12- var types : typeof typesModule = require ( "utils/types" ) ;
13- var utils : typeof utilsModule = require ( "utils/utils" ) ;
14-
15- var density = utils . layout . getDisplayDensity ( ) ;
12+ let types : typeof typesModule = require ( "utils/types" ) ;
13+ let utils : typeof utilsModule = require ( "utils/utils" ) ;
14+
15+ let saveToGallery ;
16+ let reqWidth ;
17+ let reqHeight ;
18+ let shouldKeepAspectRatio ;
19+
20+ let density = utils . layout . getDisplayDensity ( ) ;
1621 if ( options ) {
17- var saveToGallery = options . saveToGallery ? true : false ;
18- var reqWidth = options . width ? options . width * density : 0 ;
19- var reqHeight = options . height ? options . height * density : reqWidth ;
20- var shouldKeepAspectRatio = types . isNullOrUndefined ( options . keepAspectRatio ) ? true : options . keepAspectRatio ;
22+ saveToGallery = options . saveToGallery ? true : false ;
23+ reqWidth = options . width ? options . width * density : 0 ;
24+ reqHeight = options . height ? options . height * density : reqWidth ;
25+ shouldKeepAspectRatio = types . isNullOrUndefined ( options . keepAspectRatio ) ? true : options . keepAspectRatio ;
2126 }
22- var takePictureIntent = new android . content . Intent ( android . provider . MediaStore . ACTION_IMAGE_CAPTURE ) ;
23- var dateStamp = createDateTimeStamp ( ) ;
27+ let takePictureIntent = new android . content . Intent ( android . provider . MediaStore . ACTION_IMAGE_CAPTURE ) ;
28+ let dateStamp = createDateTimeStamp ( ) ;
29+
30+ let picturePath : string ;
31+ let nativeFile ;
32+ let tempPictureUri ;
2433
2534 if ( saveToGallery ) {
26- var picturePath = android . os . Environment . getExternalStoragePublicDirectory (
35+ picturePath = android . os . Environment . getExternalStoragePublicDirectory (
2736 android . os . Environment . DIRECTORY_PICTURES ) . getAbsolutePath ( ) + "/" + "cameraPicture_" + dateStamp + ".jpg" ;
28- var nativeFile = new java . io . File ( picturePath ) ;
29- var tempPictureUri = android . net . Uri . fromFile ( nativeFile ) ;
37+ nativeFile = new java . io . File ( picturePath ) ;
38+ tempPictureUri = android . net . Uri . fromFile ( nativeFile ) ;
3039 takePictureIntent . putExtra ( android . provider . MediaStore . EXTRA_OUTPUT , tempPictureUri ) ;
3140 } else {
32- var picturePath = utils . ad . getApplicationContext ( ) . getExternalFilesDir ( null ) . getAbsolutePath ( ) + "/" + "cameraPicture_" + dateStamp + ".jpg" ;
33- var nativeFile = new java . io . File ( picturePath ) ;
34- var tempPictureUri = android . net . Uri . fromFile ( nativeFile ) ;
41+ picturePath = utils . ad . getApplicationContext ( ) . getExternalFilesDir ( null ) . getAbsolutePath ( ) + "/" + "cameraPicture_" + dateStamp + ".jpg" ;
42+ nativeFile = new java . io . File ( picturePath ) ;
43+ tempPictureUri = android . net . Uri . fromFile ( nativeFile ) ;
3544 takePictureIntent . putExtra ( android . provider . MediaStore . EXTRA_OUTPUT , tempPictureUri ) ;
3645 }
3746
3847 if ( takePictureIntent . resolveActivity ( utils . ad . getApplicationContext ( ) . getPackageManager ( ) ) != null ) {
3948
40- var appModule : typeof applicationModule = require ( "application" ) ;
49+ let appModule : typeof applicationModule = require ( "application" ) ;
4150
42- var previousResult = appModule . android . onActivityResult ;
51+ let previousResult = appModule . android . onActivityResult ;
4352 appModule . android . onActivityResult = ( requestCode : number , resultCode : number , data : android . content . Intent ) => {
4453 appModule . android . onActivityResult = previousResult ;
4554
4655 if ( requestCode === REQUEST_IMAGE_CAPTURE && resultCode === android . app . Activity . RESULT_OK ) {
47- var imageSource : typeof imageSourceModule = require ( "image-source" ) ;
48- if ( saveToGallery ) {
49- resolve ( { image :imageSource . fromFile ( picturePath ) , path :picturePath } ) ;
50- } else {
51- var options = new android . graphics . BitmapFactory . Options ( ) ;
52- options . inJustDecodeBounds = true ;
53- android . graphics . BitmapFactory . decodeFile ( picturePath , options ) ;
54-
55- var sampleSize = calculateInSampleSize ( options . outWidth , options . outHeight , reqWidth , reqHeight ) ;
56-
57- var finalBitmapOptions = new android . graphics . BitmapFactory . Options ( ) ;
58- finalBitmapOptions . inSampleSize = sampleSize ;
59- var bitmap = android . graphics . BitmapFactory . decodeFile ( picturePath , finalBitmapOptions ) ;
60- var scaledSizeImage = null ;
61- if ( reqHeight > 0 && reqWidth > 0 ) {
62- if ( shouldKeepAspectRatio ) {
63-
64- var common : typeof cameraCommonModule = require ( "./camera-common" ) ;
65-
66- var aspectSafeSize = common . getAspectSafeDimensions ( bitmap . getWidth ( ) , bitmap . getHeight ( ) , reqWidth , reqHeight ) ;
67- scaledSizeImage = android . graphics . Bitmap . createScaledBitmap ( bitmap , aspectSafeSize . width , aspectSafeSize . height , true ) ;
68- }
69- else {
70- scaledSizeImage = android . graphics . Bitmap . createScaledBitmap ( bitmap , reqWidth , reqHeight , true ) ;
71- }
56+ let imageSource : typeof imageSourceModule = require ( "image-source" ) ;
57+ let options = new android . graphics . BitmapFactory . Options ( ) ;
58+ options . inJustDecodeBounds = true ;
59+ android . graphics . BitmapFactory . decodeFile ( picturePath , options ) ;
60+
61+ let sampleSize = calculateInSampleSize ( options . outWidth , options . outHeight , reqWidth , reqHeight ) ;
62+
63+ let finalBitmapOptions = new android . graphics . BitmapFactory . Options ( ) ;
64+ finalBitmapOptions . inSampleSize = sampleSize ;
65+ let bitmap = android . graphics . BitmapFactory . decodeFile ( picturePath , finalBitmapOptions ) ;
66+ let scaledSizeImage = null ;
67+ if ( reqHeight > 0 && reqWidth > 0 ) {
68+ if ( shouldKeepAspectRatio ) {
69+
70+ let common : typeof cameraCommonModule = require ( "./camera-common" ) ;
71+
72+ let aspectSafeSize = common . getAspectSafeDimensions ( bitmap . getWidth ( ) , bitmap . getHeight ( ) , reqWidth , reqHeight ) ;
73+ scaledSizeImage = android . graphics . Bitmap . createScaledBitmap ( bitmap , aspectSafeSize . width , aspectSafeSize . height , true ) ;
7274 }
7375 else {
74- scaledSizeImage = bitmap ;
76+ scaledSizeImage = android . graphics . Bitmap . createScaledBitmap ( bitmap , reqWidth , reqHeight , true ) ;
7577 }
78+ }
79+ else {
80+ scaledSizeImage = bitmap ;
81+ }
7682
77- var ei = new android . media . ExifInterface ( picturePath ) ;
78- var orientation = ei . getAttributeInt ( android . media . ExifInterface . TAG_ORIENTATION , android . media . ExifInterface . ORIENTATION_NORMAL ) ;
79-
80- switch ( orientation ) {
81- case android . media . ExifInterface . ORIENTATION_ROTATE_90 :
82- scaledSizeImage = rotateBitmap ( scaledSizeImage , 90 ) ;
83- break ;
84- case android . media . ExifInterface . ORIENTATION_ROTATE_180 :
85- scaledSizeImage = rotateBitmap ( scaledSizeImage , 180 ) ;
86- break ;
87- case android . media . ExifInterface . ORIENTATION_ROTATE_270 :
88- scaledSizeImage = rotateBitmap ( scaledSizeImage , 270 ) ;
89- break ;
90- }
91-
92- resolve ( { image :imageSource . fromNativeSource ( scaledSizeImage ) , path :picturePath } ) ;
93-
83+ let ei = new android . media . ExifInterface ( picturePath ) ;
84+ let orientation = ei . getAttributeInt ( android . media . ExifInterface . TAG_ORIENTATION , android . media . ExifInterface . ORIENTATION_NORMAL ) ;
85+
86+ switch ( orientation ) {
87+ case android . media . ExifInterface . ORIENTATION_ROTATE_90 :
88+ scaledSizeImage = rotateBitmap ( scaledSizeImage , 90 ) ;
89+ break ;
90+ case android . media . ExifInterface . ORIENTATION_ROTATE_180 :
91+ scaledSizeImage = rotateBitmap ( scaledSizeImage , 180 ) ;
92+ break ;
93+ case android . media . ExifInterface . ORIENTATION_ROTATE_270 :
94+ scaledSizeImage = rotateBitmap ( scaledSizeImage , 270 ) ;
95+ break ;
9496 }
97+
98+ resolve ( imageSource . fromNativeSource ( scaledSizeImage ) ) ;
9599 }
96100 } ;
97101
@@ -113,10 +117,10 @@ export var isAvailable = function () {
113117}
114118
115119var calculateInSampleSize = function ( imageWidth , imageHeight , reqWidth , reqHeight ) {
116- var sampleSize = 1 ;
120+ let sampleSize = 1 ;
117121 if ( imageWidth > reqWidth && imageHeight > reqHeight ) {
118- var halfWidth = imageWidth / 2 ;
119- var halfHeight = imageHeight / 2 ;
122+ let halfWidth = imageWidth / 2 ;
123+ let halfHeight = imageHeight / 2 ;
120124 while ( ( halfWidth / sampleSize ) > reqWidth && ( halfHeight / sampleSize ) > reqHeight ) {
121125 sampleSize *= 2 ;
122126 }
@@ -125,8 +129,8 @@ var calculateInSampleSize = function (imageWidth, imageHeight, reqWidth, reqHeig
125129}
126130
127131var createDateTimeStamp = function ( ) {
128- var result = "" ;
129- var date = new Date ( ) ;
132+ let result = "" ;
133+ let date = new Date ( ) ;
130134 result = ( date . getDate ( ) < 10 ? "0" + date . getDate ( ) . toString ( ) : date . getDate ( ) . toString ( ) ) +
131135 ( ( date . getMonth ( ) + 1 ) < 10 ? "0" + ( date . getMonth ( ) + 1 ) . toString ( ) : ( date . getMonth ( ) + 1 ) . toString ( ) ) +
132136 date . getFullYear ( ) . toString ( ) +
@@ -137,7 +141,7 @@ var createDateTimeStamp = function () {
137141}
138142
139143var rotateBitmap = function ( source , angle ) {
140- var matrix = new android . graphics . Matrix ( ) ;
144+ let matrix = new android . graphics . Matrix ( ) ;
141145 matrix . postRotate ( angle ) ;
142146 return android . graphics . Bitmap . createBitmap ( source , 0 , 0 , source . getWidth ( ) , source . getHeight ( ) , matrix , true ) ;
143147}
0 commit comments