From 95da9d9f6f432cee160eabb1227162ca7701969e Mon Sep 17 00:00:00 2001 From: Nedyalko Nikolov Date: Thu, 16 Apr 2015 15:38:59 +0300 Subject: [PATCH] Added keep aspect ratio parameter to camera.takePicture method. --- CrossPlatformModules.csproj | 3 +++ camera/camera-common.ts | 11 +++++++++++ camera/camera.android.ts | 25 +++++++++++++++++++++---- camera/camera.d.ts | 3 ++- camera/camera.ios.ts | 24 ++++++++++++++++++------ ui/enums/enums.d.ts | 4 ++-- 6 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 camera/camera-common.ts diff --git a/CrossPlatformModules.csproj b/CrossPlatformModules.csproj index c87ba9a84..cd05937ca 100644 --- a/CrossPlatformModules.csproj +++ b/CrossPlatformModules.csproj @@ -722,6 +722,9 @@ Designer + + camera.d.ts + camera.d.ts diff --git a/camera/camera-common.ts b/camera/camera-common.ts new file mode 100644 index 000000000..383bc18d7 --- /dev/null +++ b/camera/camera-common.ts @@ -0,0 +1,11 @@ +export function getAspectSafeDimensions(sourceWidth, sourceHeight, reqWidth, reqHeight) { + var widthCoef = sourceWidth / reqWidth; + var heightCoef = sourceHeight / reqHeight; + + var aspectCoef = widthCoef > heightCoef ? widthCoef : heightCoef; + + return { + width: Math.floor(sourceWidth / aspectCoef), + height: Math.floor(sourceHeight / aspectCoef) + }; +} \ No newline at end of file diff --git a/camera/camera.android.ts b/camera/camera.android.ts index ed66dbaa1..5088c6207 100644 --- a/camera/camera.android.ts +++ b/camera/camera.android.ts @@ -1,14 +1,17 @@ import imageSource = require("image-source"); import appModule = require("application"); import fileSystem = require("file-system"); +import utils = require("utils/utils"); +import common = require("./camera-common"); var REQUEST_IMAGE_CAPTURE = 3453; -export var takePicture = function (width?, height?): Promise { +export var takePicture = function (width?, height?, keepAspectRatio?): Promise { return new Promise((resolve, reject) => { try { - var reqWidth = width || 0; - var reqHeight = height || reqWidth; + var density = utils.layout.getDisplayDensity(); + var reqWidth = width ? width * density : 0; + var reqHeight = height ? height * density : reqWidth; var takePictureIntent = new android.content.Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); var dateStamp = createDateTimeStamp(); var tempPicturePath = fileSystem.path.join(appModule.android.currentContext.getExternalFilesDir(null).getAbsolutePath(), "cameraPicture_" + dateStamp + ".jpg"); @@ -31,7 +34,21 @@ export var takePicture = function (width?, height?): Promise 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); + } + else { + scaledSizeImage = android.graphics.Bitmap.createScaledBitmap(bitmap, reqWidth, reqHeight, true); + } + } + else { + scaledSizeImage = bitmap; + } + resolve(imageSource.fromNativeSource(scaledSizeImage)); } }; diff --git a/camera/camera.d.ts b/camera/camera.d.ts index 45cffabac..c4dc90fd8 100644 --- a/camera/camera.d.ts +++ b/camera/camera.d.ts @@ -9,6 +9,7 @@ declare module "camera" { * Take a photo using the camera. * @param width - Optional parameter which defines the required width of the taken picture. * @param height - Optional parameter which defines the required height of the taken picture. + * @param keepAspectRatio - Optional parameter which controls if the result picture will keep the aspect ratio of the picture taken by camera. */ - export function takePicture(width?: number, heigth?: number): Promise; + export function takePicture(width?: number, heigth?: number, keepAspectRatio?: boolean): Promise; } diff --git a/camera/camera.ios.ts b/camera/camera.ios.ts index e3b46da41..5bf3cfbb9 100644 --- a/camera/camera.ios.ts +++ b/camera/camera.ios.ts @@ -1,5 +1,6 @@ import imageSource = require("image-source"); import frame = require("ui/frame"); +import common = require("./camera-common"); class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePickerControllerDelegate { public static ObjCProtocols = [UIImagePickerControllerDelegate]; @@ -12,16 +13,20 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic private _width: number; private _height: number; + private _keepAspectRatio: boolean; public initWithCallback(callback: (result?: imageSource.ImageSource) => void): UIImagePickerControllerDelegateImpl { this._callback = callback; return this; } - public initWithCallbackWidthAndHeight(callback: (result?: imageSource.ImageSource) => void, width, height): UIImagePickerControllerDelegateImpl { + public initWithCallbackAndOptions(callback: (result?: imageSource.ImageSource) => void, options?): UIImagePickerControllerDelegateImpl { this._callback = callback; - this._width = width; - this._height = height; + if (options) { + this._width = options.width; + this._height = options.height; + this._keepAspectRatio = (options.keepAspectRatio === null || options.keepAspectRatio === undefined) ? true : options.keepAspectRatio; + } return this; } @@ -31,7 +36,14 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic if (source) { var image = null; if (this._width || this._height) { - var newSize = CGSizeMake(this._width, this._height); + var newSize = null; + if (this._keepAspectRatio) { + var aspectSafeSize = common.getAspectSafeDimensions(source.size.width, source.size.height, this._width, this._height); + newSize = CGSizeMake(aspectSafeSize.width, aspectSafeSize.height); + } + else { + newSize = CGSizeMake(this._width, this._height); + } UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0); source.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height)); image = UIGraphicsGetImageFromCurrentImageContext(); @@ -53,14 +65,14 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic } } -export var takePicture = function (width?, height?): Promise { +export var takePicture = function (width?, height?, keepAspectRatio?): Promise { return new Promise((resolve, reject) => { var imagePickerController = new UIImagePickerController(); var listener = null; var reqWidth = width || 0; var reqHeight = height || reqWidth; if (reqWidth && reqHeight) { - listener = UIImagePickerControllerDelegateImpl.new().initWithCallbackWidthAndHeight(resolve, reqWidth, reqHeight); + listener = UIImagePickerControllerDelegateImpl.new().initWithCallbackAndOptions(resolve, { width: reqWidth, height: reqHeight, keepAspectRatio: keepAspectRatio }); } else { listener = UIImagePickerControllerDelegateImpl.new().initWithCallback(resolve); diff --git a/ui/enums/enums.d.ts b/ui/enums/enums.d.ts index ca2dc60f8..1d73cd395 100644 --- a/ui/enums/enums.d.ts +++ b/ui/enums/enums.d.ts @@ -164,12 +164,12 @@ export var none: string; /** - * The image is resized to fit in the destination dimensions while it preserves its native aspect ratio. + * The image is resized to fill in the destination dimensions while it preserves its native aspect ratio. */ export var aspectFill: string; /** - * The image is resized to fill the destination dimensions while it preserves + * The image is resized to fit the destination dimensions while it preserves * its native aspect ratio. If the aspect ratio of the destination rectangle differs from the image, * the image is clipped to fit in the destination */