From 6615f0f57c1b22ffc2e9453e4d408179c1d175a9 Mon Sep 17 00:00:00 2001 From: Osei Fortune Date: Tue, 3 May 2016 23:27:25 -0400 Subject: [PATCH 1/5] Added save to gallery option --- camera/camera.android.ts | 100 +++++++++++++++++++++++---------------- 1 file changed, 58 insertions(+), 42 deletions(-) diff --git a/camera/camera.android.ts b/camera/camera.android.ts index 30a8d46de..2adc6a309 100644 --- a/camera/camera.android.ts +++ b/camera/camera.android.ts @@ -1,4 +1,4 @@ -import * as typesModule from "utils/types"; +import * as typesModule from "utils/types"; import * as utilsModule from "utils/utils"; import * as fileSystemModule from "file-system"; import * as applicationModule from "application"; @@ -15,6 +15,7 @@ export var takePicture = function (options?): Promise { var density = utils.layout.getDisplayDensity(); if (options) { + var saveToGallery = options.saveToGallery ? true : false; var reqWidth = options.width ? options.width * density : 0; var reqHeight = options.height ? options.height * density : reqWidth; var shouldKeepAspectRatio = types.isNullOrUndefined(options.keepAspectRatio) ? true : options.keepAspectRatio; @@ -24,10 +25,20 @@ export var takePicture = function (options?): Promise { var fileSystem: typeof fileSystemModule = require("file-system"); - var tempPicturePath = fileSystem.path.join(utils.ad.getApplicationContext().getExternalFilesDir(null).getAbsolutePath(), "cameraPicture_" + dateStamp + ".jpg"); - var nativeFile = new java.io.File(tempPicturePath); - var tempPictureUri = android.net.Uri.fromFile(nativeFile); - takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, tempPictureUri); + + if (saveToGallery) { + var picturePath = android.os.Environment.getExternalStoragePublicDirectory( + android.os.Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/" + "cameraPicture_" + dateStamp + ".jpg"; + var nativeFile = new java.io.File(picturePath); + var tempPictureUri = android.net.Uri.fromFile(nativeFile); + takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, tempPictureUri); + } else { + picturePath = utils.ad.getApplicationContext().getExternalFilesDir(null).getAbsolutePath() + "/" + "cameraPicture_" + dateStamp + ".jpg"; + var nativeFile = new java.io.File(picturePath); + var tempPictureUri = android.net.Uri.fromFile(nativeFile); + takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, tempPictureUri); + } + if (takePictureIntent.resolveActivity(utils.ad.getApplicationContext().getPackageManager()) != null) { var appModule: typeof applicationModule = require("application"); @@ -37,50 +48,55 @@ export var takePicture = function (options?): Promise { appModule.android.onActivityResult = previousResult; if (requestCode === REQUEST_IMAGE_CAPTURE && resultCode === android.app.Activity.RESULT_OK) { - var options = new android.graphics.BitmapFactory.Options(); - options.inJustDecodeBounds = true; - android.graphics.BitmapFactory.decodeFile(tempPicturePath, options); - var sampleSize = calculateInSampleSize(options.outWidth, options.outHeight, reqWidth, reqHeight); + if (saveToGallery) { + resolve(picturePath); + } else { + var options = new android.graphics.BitmapFactory.Options(); + options.inJustDecodeBounds = true; + android.graphics.BitmapFactory.decodeFile(picturePath, options); - var finalBitmapOptions = new android.graphics.BitmapFactory.Options(); - finalBitmapOptions.inSampleSize = sampleSize; - var bitmap = android.graphics.BitmapFactory.decodeFile(tempPicturePath, finalBitmapOptions); - var scaledSizeImage = null; - if (reqHeight > 0 && reqWidth > 0) { - if (shouldKeepAspectRatio) { + var sampleSize = calculateInSampleSize(options.outWidth, options.outHeight, reqWidth, reqHeight); - var common: typeof cameraCommonModule = require("./camera-common"); + var finalBitmapOptions = new android.graphics.BitmapFactory.Options(); + finalBitmapOptions.inSampleSize = sampleSize; + var bitmap = android.graphics.BitmapFactory.decodeFile(picturePath, finalBitmapOptions); + var scaledSizeImage = null; + if (reqHeight > 0 && reqWidth > 0) { + if (shouldKeepAspectRatio) { - var aspectSafeSize = common.getAspectSafeDimensions(bitmap.getWidth(), bitmap.getHeight(), reqWidth, reqHeight); - scaledSizeImage = android.graphics.Bitmap.createScaledBitmap(bitmap, aspectSafeSize.width, aspectSafeSize.height, true); + var common: typeof cameraCommonModule = require("./camera-common"); + + var aspectSafeSize = common.getAspectSafeDimensions(bitmap.getWidth(), bitmap.getHeight(), reqWidth, reqHeight); + scaledSizeImage = android.graphics.Bitmap.createScaledBitmap(bitmap, aspectSafeSize.width, aspectSafeSize.height, true); + } + else { + scaledSizeImage = android.graphics.Bitmap.createScaledBitmap(bitmap, reqWidth, reqHeight, true); + } } else { - scaledSizeImage = android.graphics.Bitmap.createScaledBitmap(bitmap, reqWidth, reqHeight, true); + scaledSizeImage = bitmap; } + + var ei = new android.media.ExifInterface(picturePath); + var orientation = ei.getAttributeInt(android.media.ExifInterface.TAG_ORIENTATION, android.media.ExifInterface.ORIENTATION_NORMAL); + + switch (orientation) { + case android.media.ExifInterface.ORIENTATION_ROTATE_90: + scaledSizeImage = rotateBitmap(scaledSizeImage, 90); + break; + case android.media.ExifInterface.ORIENTATION_ROTATE_180: + scaledSizeImage = rotateBitmap(scaledSizeImage, 180); + break; + case android.media.ExifInterface.ORIENTATION_ROTATE_270: + scaledSizeImage = rotateBitmap(scaledSizeImage, 270); + break; + } + + var imageSource: typeof imageSourceModule = require("image-source"); + resolve(imageSource.fromNativeSource(scaledSizeImage), picturePath); + } - else { - scaledSizeImage = bitmap; - } - - var ei = new android.media.ExifInterface(tempPicturePath); - var orientation = ei.getAttributeInt(android.media.ExifInterface.TAG_ORIENTATION, android.media.ExifInterface.ORIENTATION_NORMAL); - - switch (orientation) { - case android.media.ExifInterface.ORIENTATION_ROTATE_90: - scaledSizeImage = rotateBitmap(scaledSizeImage, 90); - break; - case android.media.ExifInterface.ORIENTATION_ROTATE_180: - scaledSizeImage = rotateBitmap(scaledSizeImage, 180); - break; - case android.media.ExifInterface.ORIENTATION_ROTATE_270: - scaledSizeImage = rotateBitmap(scaledSizeImage, 270); - break; - } - - var imageSource: typeof imageSourceModule = require("image-source"); - - resolve(imageSource.fromNativeSource(scaledSizeImage)); } }; @@ -123,4 +139,4 @@ var rotateBitmap = function (source, angle) { var matrix = new android.graphics.Matrix(); matrix.postRotate(angle); return android.graphics.Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true); -} \ No newline at end of file +} From babb61728781e2ad998f9d072dda3dcff8d03e2d Mon Sep 17 00:00:00 2001 From: Osei Fortune Date: Mon, 9 May 2016 10:22:00 -0400 Subject: [PATCH 2/5] Update camera.android.ts --- camera/camera.android.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/camera/camera.android.ts b/camera/camera.android.ts index 2adc6a309..a7c1a882c 100644 --- a/camera/camera.android.ts +++ b/camera/camera.android.ts @@ -24,8 +24,6 @@ export var takePicture = function (options?): Promise { var dateStamp = createDateTimeStamp(); var fileSystem: typeof fileSystemModule = require("file-system"); - - if (saveToGallery) { var picturePath = android.os.Environment.getExternalStoragePublicDirectory( android.os.Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/" + "cameraPicture_" + dateStamp + ".jpg"; @@ -33,7 +31,7 @@ export var takePicture = function (options?): Promise { var tempPictureUri = android.net.Uri.fromFile(nativeFile); takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, tempPictureUri); } else { - picturePath = utils.ad.getApplicationContext().getExternalFilesDir(null).getAbsolutePath() + "/" + "cameraPicture_" + dateStamp + ".jpg"; + var picturePath = utils.ad.getApplicationContext().getExternalFilesDir(null).getAbsolutePath() + "/" + "cameraPicture_" + dateStamp + ".jpg"; var nativeFile = new java.io.File(picturePath); var tempPictureUri = android.net.Uri.fromFile(nativeFile); takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, tempPictureUri); From 5daa847c0dd835b1e6a55127ab372dd93c80a608 Mon Sep 17 00:00:00 2001 From: Osei Fortune Date: Mon, 9 May 2016 11:02:46 -0400 Subject: [PATCH 3/5] Update camera.android.ts --- camera/camera.android.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/camera/camera.android.ts b/camera/camera.android.ts index a7c1a882c..a8025a29e 100644 --- a/camera/camera.android.ts +++ b/camera/camera.android.ts @@ -23,7 +23,6 @@ export var takePicture = function (options?): Promise { var takePictureIntent = new android.content.Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); var dateStamp = createDateTimeStamp(); - var fileSystem: typeof fileSystemModule = require("file-system"); if (saveToGallery) { var picturePath = android.os.Environment.getExternalStoragePublicDirectory( android.os.Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/" + "cameraPicture_" + dateStamp + ".jpg"; From 4d14166db6363b9f0af911eb085bf945fafdaea1 Mon Sep 17 00:00:00 2001 From: Osei Fortune Date: Mon, 9 May 2016 11:17:34 -0400 Subject: [PATCH 4/5] Update camera.android.ts --- camera/camera.android.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/camera/camera.android.ts b/camera/camera.android.ts index a8025a29e..f1fa8e8dd 100644 --- a/camera/camera.android.ts +++ b/camera/camera.android.ts @@ -1,6 +1,5 @@ import * as typesModule from "utils/types"; import * as utilsModule from "utils/utils"; -import * as fileSystemModule from "file-system"; import * as applicationModule from "application"; import * as imageSourceModule from "image-source"; import * as cameraCommonModule from "./camera-common"; From fb19294fcca95fe355f335c58fa379ebb0cc0410 Mon Sep 17 00:00:00 2001 From: Osei Fortune Date: Mon, 9 May 2016 12:15:31 -0400 Subject: [PATCH 5/5] Update camera.android.ts --- camera/camera.android.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/camera/camera.android.ts b/camera/camera.android.ts index f1fa8e8dd..82a7485bf 100644 --- a/camera/camera.android.ts +++ b/camera/camera.android.ts @@ -44,9 +44,9 @@ export var takePicture = function (options?): Promise { appModule.android.onActivityResult = previousResult; if (requestCode === REQUEST_IMAGE_CAPTURE && resultCode === android.app.Activity.RESULT_OK) { - + var imageSource: typeof imageSourceModule = require("image-source"); if (saveToGallery) { - resolve(picturePath); + resolve({image:imageSource.fromFile(picturePath) ,path:picturePath}); } else { var options = new android.graphics.BitmapFactory.Options(); options.inJustDecodeBounds = true; @@ -89,8 +89,7 @@ export var takePicture = function (options?): Promise { break; } - var imageSource: typeof imageSourceModule = require("image-source"); - resolve(imageSource.fromNativeSource(scaledSizeImage), picturePath); + resolve({image:imageSource.fromNativeSource(scaledSizeImage), path:picturePath}); } }