Fixed breaking change in camera for android.

This commit is contained in:
Nedyalko Nikolov
2016-05-14 11:15:29 +03:00
parent a5e2d46553
commit cf3cd35a4f
5 changed files with 101 additions and 92 deletions

View File

@ -2,7 +2,7 @@
``` ```
var camera = require("camera"); var camera = require("camera");
camera.takePicture({"cameraPosition": camera.CameraPosition.BACK, "flashMode": camera.FlashMode.ON}).then(function (image) { camera.takePicture({"width": 300, "height": 300, "keepAspectRatio": true, "saveToGallery": true}).then(function (image) {
console.log('pic taken - width: ' + image.width + ", height: " + image.height); console.log('pic taken - width: ' + image.width + ", height: " + image.height);
}).fail(function (error) { }).fail(function (error) {
console.log('pic canceled'); console.log('pic canceled');

View File

@ -1,8 +1,8 @@
export function getAspectSafeDimensions(sourceWidth, sourceHeight, reqWidth, reqHeight) { export function getAspectSafeDimensions(sourceWidth, sourceHeight, reqWidth, reqHeight) {
var widthCoef = sourceWidth / reqWidth; let widthCoef = sourceWidth / reqWidth;
var heightCoef = sourceHeight / reqHeight; let heightCoef = sourceHeight / reqHeight;
var aspectCoef = widthCoef > heightCoef ? widthCoef : heightCoef; let aspectCoef = widthCoef > heightCoef ? widthCoef : heightCoef;
return { return {
width: Math.floor(sourceWidth / aspectCoef), width: Math.floor(sourceWidth / aspectCoef),

View File

@ -9,61 +9,67 @@ var REQUEST_IMAGE_CAPTURE = 3453;
export var takePicture = function (options?): Promise<any> { export var takePicture = function (options?): Promise<any> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
try { try {
var types: typeof typesModule = require("utils/types"); let types: typeof typesModule = require("utils/types");
var utils: typeof utilsModule = require("utils/utils"); let utils: typeof utilsModule = require("utils/utils");
var density = utils.layout.getDisplayDensity(); let saveToGallery;
let reqWidth;
let reqHeight;
let shouldKeepAspectRatio;
let density = utils.layout.getDisplayDensity();
if (options) { if (options) {
var saveToGallery = options.saveToGallery ? true : false; saveToGallery = options.saveToGallery ? true : false;
var reqWidth = options.width ? options.width * density : 0; reqWidth = options.width ? options.width * density : 0;
var reqHeight = options.height ? options.height * density : reqWidth; reqHeight = options.height ? options.height * density : reqWidth;
var shouldKeepAspectRatio = types.isNullOrUndefined(options.keepAspectRatio) ? true : options.keepAspectRatio; shouldKeepAspectRatio = types.isNullOrUndefined(options.keepAspectRatio) ? true : options.keepAspectRatio;
} }
var takePictureIntent = new android.content.Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); let takePictureIntent = new android.content.Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
var dateStamp = createDateTimeStamp(); let dateStamp = createDateTimeStamp();
let picturePath: string;
let nativeFile;
let tempPictureUri;
if (saveToGallery) { if (saveToGallery) {
var picturePath = android.os.Environment.getExternalStoragePublicDirectory( picturePath = android.os.Environment.getExternalStoragePublicDirectory(
android.os.Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/" + "cameraPicture_" + dateStamp + ".jpg"; android.os.Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/" + "cameraPicture_" + dateStamp + ".jpg";
var nativeFile = new java.io.File(picturePath); nativeFile = new java.io.File(picturePath);
var tempPictureUri = android.net.Uri.fromFile(nativeFile); tempPictureUri = android.net.Uri.fromFile(nativeFile);
takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, tempPictureUri); takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, tempPictureUri);
} else { } else {
var picturePath = utils.ad.getApplicationContext().getExternalFilesDir(null).getAbsolutePath() + "/" + "cameraPicture_" + dateStamp + ".jpg"; picturePath = utils.ad.getApplicationContext().getExternalFilesDir(null).getAbsolutePath() + "/" + "cameraPicture_" + dateStamp + ".jpg";
var nativeFile = new java.io.File(picturePath); nativeFile = new java.io.File(picturePath);
var tempPictureUri = android.net.Uri.fromFile(nativeFile); tempPictureUri = android.net.Uri.fromFile(nativeFile);
takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, tempPictureUri); takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, tempPictureUri);
} }
if (takePictureIntent.resolveActivity(utils.ad.getApplicationContext().getPackageManager()) != null) { if (takePictureIntent.resolveActivity(utils.ad.getApplicationContext().getPackageManager()) != null) {
var appModule: typeof applicationModule = require("application"); let appModule: typeof applicationModule = require("application");
var previousResult = appModule.android.onActivityResult; let previousResult = appModule.android.onActivityResult;
appModule.android.onActivityResult = (requestCode: number, resultCode: number, data: android.content.Intent) => { appModule.android.onActivityResult = (requestCode: number, resultCode: number, data: android.content.Intent) => {
appModule.android.onActivityResult = previousResult; appModule.android.onActivityResult = previousResult;
if (requestCode === REQUEST_IMAGE_CAPTURE && resultCode === android.app.Activity.RESULT_OK) { if (requestCode === REQUEST_IMAGE_CAPTURE && resultCode === android.app.Activity.RESULT_OK) {
var imageSource: typeof imageSourceModule = require("image-source"); let imageSource: typeof imageSourceModule = require("image-source");
if (saveToGallery) { let options = new android.graphics.BitmapFactory.Options();
resolve({image:imageSource.fromFile(picturePath) ,path:picturePath});
} else {
var options = new android.graphics.BitmapFactory.Options();
options.inJustDecodeBounds = true; options.inJustDecodeBounds = true;
android.graphics.BitmapFactory.decodeFile(picturePath, options); android.graphics.BitmapFactory.decodeFile(picturePath, options);
var sampleSize = calculateInSampleSize(options.outWidth, options.outHeight, reqWidth, reqHeight); let sampleSize = calculateInSampleSize(options.outWidth, options.outHeight, reqWidth, reqHeight);
var finalBitmapOptions = new android.graphics.BitmapFactory.Options(); let finalBitmapOptions = new android.graphics.BitmapFactory.Options();
finalBitmapOptions.inSampleSize = sampleSize; finalBitmapOptions.inSampleSize = sampleSize;
var bitmap = android.graphics.BitmapFactory.decodeFile(picturePath, finalBitmapOptions); let bitmap = android.graphics.BitmapFactory.decodeFile(picturePath, finalBitmapOptions);
var scaledSizeImage = null; let scaledSizeImage = null;
if (reqHeight > 0 && reqWidth > 0) { if (reqHeight > 0 && reqWidth > 0) {
if (shouldKeepAspectRatio) { if (shouldKeepAspectRatio) {
var common: typeof cameraCommonModule = require("./camera-common"); let common: typeof cameraCommonModule = require("./camera-common");
var aspectSafeSize = common.getAspectSafeDimensions(bitmap.getWidth(), bitmap.getHeight(), reqWidth, reqHeight); let aspectSafeSize = common.getAspectSafeDimensions(bitmap.getWidth(), bitmap.getHeight(), reqWidth, reqHeight);
scaledSizeImage = android.graphics.Bitmap.createScaledBitmap(bitmap, aspectSafeSize.width, aspectSafeSize.height, true); scaledSizeImage = android.graphics.Bitmap.createScaledBitmap(bitmap, aspectSafeSize.width, aspectSafeSize.height, true);
} }
else { else {
@ -74,8 +80,8 @@ export var takePicture = function (options?): Promise<any> {
scaledSizeImage = bitmap; scaledSizeImage = bitmap;
} }
var ei = new android.media.ExifInterface(picturePath); let ei = new android.media.ExifInterface(picturePath);
var orientation = ei.getAttributeInt(android.media.ExifInterface.TAG_ORIENTATION, android.media.ExifInterface.ORIENTATION_NORMAL); let orientation = ei.getAttributeInt(android.media.ExifInterface.TAG_ORIENTATION, android.media.ExifInterface.ORIENTATION_NORMAL);
switch (orientation) { switch (orientation) {
case android.media.ExifInterface.ORIENTATION_ROTATE_90: case android.media.ExifInterface.ORIENTATION_ROTATE_90:
@ -89,9 +95,7 @@ export var takePicture = function (options?): Promise<any> {
break; break;
} }
resolve({image:imageSource.fromNativeSource(scaledSizeImage), path:picturePath}); resolve(imageSource.fromNativeSource(scaledSizeImage));
}
} }
}; };
@ -113,10 +117,10 @@ export var isAvailable = function () {
} }
var calculateInSampleSize = function (imageWidth, imageHeight, reqWidth, reqHeight) { var calculateInSampleSize = function (imageWidth, imageHeight, reqWidth, reqHeight) {
var sampleSize = 1; let sampleSize = 1;
if (imageWidth > reqWidth && imageHeight > reqHeight) { if (imageWidth > reqWidth && imageHeight > reqHeight) {
var halfWidth = imageWidth / 2; let halfWidth = imageWidth / 2;
var halfHeight = imageHeight / 2; let halfHeight = imageHeight / 2;
while ((halfWidth / sampleSize) > reqWidth && (halfHeight / sampleSize) > reqHeight) { while ((halfWidth / sampleSize) > reqWidth && (halfHeight / sampleSize) > reqHeight) {
sampleSize *= 2; sampleSize *= 2;
} }
@ -125,8 +129,8 @@ var calculateInSampleSize = function (imageWidth, imageHeight, reqWidth, reqHeig
} }
var createDateTimeStamp = function () { var createDateTimeStamp = function () {
var result = ""; let result = "";
var date = new Date(); let date = new Date();
result = (date.getDate() < 10 ? "0" + date.getDate().toString() : date.getDate().toString()) + result = (date.getDate() < 10 ? "0" + date.getDate().toString() : date.getDate().toString()) +
((date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1).toString() : (date.getMonth() + 1).toString()) + ((date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1).toString() : (date.getMonth() + 1).toString()) +
date.getFullYear().toString() + date.getFullYear().toString() +
@ -137,7 +141,7 @@ var createDateTimeStamp = function () {
} }
var rotateBitmap = function (source, angle) { var rotateBitmap = function (source, angle) {
var matrix = new android.graphics.Matrix(); let matrix = new android.graphics.Matrix();
matrix.postRotate(angle); matrix.postRotate(angle);
return android.graphics.Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true); return android.graphics.Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
} }

5
camera/camera.d.ts vendored
View File

@ -36,5 +36,10 @@ declare module "camera" {
* This property could affect width or heigth return values. * This property could affect width or heigth return values.
*/ */
keepAspectRatio?: boolean; keepAspectRatio?: boolean;
/**
* Defines if camera picture should be copied to photo Gallery (Android) or Photos (iOS)
*/
saveToGallery?: boolean;
} }
} }

View File

@ -35,15 +35,15 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic
imagePickerControllerDidFinishPickingMediaWithInfo(picker, info): void { imagePickerControllerDidFinishPickingMediaWithInfo(picker, info): void {
if (info) { if (info) {
var source = info.valueForKey(UIImagePickerControllerOriginalImage); let source = info.valueForKey(UIImagePickerControllerOriginalImage);
if (source) { if (source) {
var image = null; let image = null;
if (this._width || this._height) { if (this._width || this._height) {
var newSize = null; let newSize = null;
if (this._keepAspectRatio) { if (this._keepAspectRatio) {
var common: typeof cameraCommonModule = require("./camera-common"); let common: typeof cameraCommonModule = require("./camera-common");
var aspectSafeSize = common.getAspectSafeDimensions(source.size.width, source.size.height, this._width, this._height); let aspectSafeSize = common.getAspectSafeDimensions(source.size.width, source.size.height, this._width, this._height);
newSize = CGSizeMake(aspectSafeSize.width, aspectSafeSize.height); newSize = CGSizeMake(aspectSafeSize.width, aspectSafeSize.height);
} }
else { else {
@ -55,9 +55,9 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic
UIGraphicsEndImageContext(); UIGraphicsEndImageContext();
} }
var imageSource: typeof imageSourceModule = require("image-source"); let imageSource: typeof imageSourceModule = require("image-source");
var imageSourceResult = image ? imageSource.fromNativeSource(image) : imageSource.fromNativeSource(source); let imageSourceResult = image ? imageSource.fromNativeSource(image) : imageSource.fromNativeSource(source);
if (this._callback) { if (this._callback) {
this._callback(imageSourceResult); this._callback(imageSourceResult);
@ -82,11 +82,11 @@ var listener;
export var takePicture = function (options): Promise<any> { export var takePicture = function (options): Promise<any> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
listener = null; listener = null;
var imagePickerController = new UIImagePickerController(); let imagePickerController = new UIImagePickerController();
var reqWidth = 0; let reqWidth = 0;
var reqHeight = 0; let reqHeight = 0;
var keepAspectRatio = true; let keepAspectRatio = true;
var saveToGallery = true; let saveToGallery = true;
if (options) { if (options) {
reqWidth = options.width || 0; reqWidth = options.width || 0;
reqHeight = options.height || reqWidth; reqHeight = options.height || reqWidth;
@ -103,8 +103,8 @@ export var takePicture = function (options): Promise<any> {
} }
imagePickerController.delegate = listener; imagePickerController.delegate = listener;
var sourceType = UIImagePickerControllerSourceType.UIImagePickerControllerSourceTypeCamera; let sourceType = UIImagePickerControllerSourceType.UIImagePickerControllerSourceTypeCamera;
var mediaTypes = UIImagePickerController.availableMediaTypesForSourceType(sourceType); let mediaTypes = UIImagePickerController.availableMediaTypesForSourceType(sourceType);
if (mediaTypes) { if (mediaTypes) {
imagePickerController.mediaTypes = mediaTypes; imagePickerController.mediaTypes = mediaTypes;
@ -113,11 +113,11 @@ export var takePicture = function (options): Promise<any> {
imagePickerController.modalPresentationStyle = UIModalPresentationStyle.UIModalPresentationCurrentContext; imagePickerController.modalPresentationStyle = UIModalPresentationStyle.UIModalPresentationCurrentContext;
var frame: typeof frameModule = require("ui/frame"); let frame: typeof frameModule = require("ui/frame");
var topMostFrame = frame.topmost(); let topMostFrame = frame.topmost();
if (topMostFrame) { if (topMostFrame) {
var viewController: UIViewController = topMostFrame.currentPage && topMostFrame.currentPage.ios; let viewController: UIViewController = topMostFrame.currentPage && topMostFrame.currentPage.ios;
if (viewController) { if (viewController) {
viewController.presentViewControllerAnimatedCompletion(imagePickerController, true, null); viewController.presentViewControllerAnimatedCompletion(imagePickerController, true, null);
} }