Added keep aspect ratio parameter to camera.takePicture method.

This commit is contained in:
Nedyalko Nikolov
2015-04-16 15:38:59 +03:00
parent caf41f95e6
commit 95da9d9f6f
6 changed files with 57 additions and 13 deletions

View File

@ -722,6 +722,9 @@
<SubType>Designer</SubType> <SubType>Designer</SubType>
</Content> </Content>
<TypeScriptCompile Include="camera\camera.d.ts" /> <TypeScriptCompile Include="camera\camera.d.ts" />
<TypeScriptCompile Include="camera\camera-common.ts" >
<DependentUpon>camera.d.ts</DependentUpon>
</TypeScriptCompile>
<TypeScriptCompile Include="camera\camera.android.ts"> <TypeScriptCompile Include="camera\camera.android.ts">
<DependentUpon>camera.d.ts</DependentUpon> <DependentUpon>camera.d.ts</DependentUpon>
</TypeScriptCompile> </TypeScriptCompile>

11
camera/camera-common.ts Normal file
View File

@ -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)
};
}

View File

@ -1,14 +1,17 @@
import imageSource = require("image-source"); import imageSource = require("image-source");
import appModule = require("application"); import appModule = require("application");
import fileSystem = require("file-system"); import fileSystem = require("file-system");
import utils = require("utils/utils");
import common = require("./camera-common");
var REQUEST_IMAGE_CAPTURE = 3453; var REQUEST_IMAGE_CAPTURE = 3453;
export var takePicture = function (width?, height?): Promise<imageSource.ImageSource> { export var takePicture = function (width?, height?, keepAspectRatio?): Promise<imageSource.ImageSource> {
return new Promise<imageSource.ImageSource>((resolve, reject) => { return new Promise<imageSource.ImageSource>((resolve, reject) => {
try { try {
var reqWidth = width || 0; var density = utils.layout.getDisplayDensity();
var reqHeight = height || reqWidth; 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 takePictureIntent = new android.content.Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
var dateStamp = createDateTimeStamp(); var dateStamp = createDateTimeStamp();
var tempPicturePath = fileSystem.path.join(appModule.android.currentContext.getExternalFilesDir(null).getAbsolutePath(), "cameraPicture_" + dateStamp + ".jpg"); 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<imageSource.ImageSo
var finalBitmapOptions = new android.graphics.BitmapFactory.Options(); var finalBitmapOptions = new android.graphics.BitmapFactory.Options();
finalBitmapOptions.inSampleSize = sampleSize; finalBitmapOptions.inSampleSize = sampleSize;
var bitmap = android.graphics.BitmapFactory.decodeFile(tempPicturePath, finalBitmapOptions); var bitmap = android.graphics.BitmapFactory.decodeFile(tempPicturePath, finalBitmapOptions);
resolve(imageSource.fromNativeSource(bitmap)); var shouldKeepAspectRatio = (keepAspectRatio === null || keepAspectRatio === undefined) ? true : keepAspectRatio;
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);
}
else {
scaledSizeImage = android.graphics.Bitmap.createScaledBitmap(bitmap, reqWidth, reqHeight, true);
}
}
else {
scaledSizeImage = bitmap;
}
resolve(imageSource.fromNativeSource(scaledSizeImage));
} }
}; };

3
camera/camera.d.ts vendored
View File

@ -9,6 +9,7 @@ declare module "camera" {
* Take a photo using the camera. * Take a photo using the camera.
* @param width - Optional parameter which defines the required width of the taken picture. * @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 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<imageSource.ImageSource>; export function takePicture(width?: number, heigth?: number, keepAspectRatio?: boolean): Promise<imageSource.ImageSource>;
} }

View File

@ -1,5 +1,6 @@
import imageSource = require("image-source"); import imageSource = require("image-source");
import frame = require("ui/frame"); import frame = require("ui/frame");
import common = require("./camera-common");
class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePickerControllerDelegate { class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePickerControllerDelegate {
public static ObjCProtocols = [UIImagePickerControllerDelegate]; public static ObjCProtocols = [UIImagePickerControllerDelegate];
@ -12,16 +13,20 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic
private _width: number; private _width: number;
private _height: number; private _height: number;
private _keepAspectRatio: boolean;
public initWithCallback(callback: (result?: imageSource.ImageSource) => void): UIImagePickerControllerDelegateImpl { public initWithCallback(callback: (result?: imageSource.ImageSource) => void): UIImagePickerControllerDelegateImpl {
this._callback = callback; this._callback = callback;
return this; 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._callback = callback;
this._width = width; if (options) {
this._height = height; this._width = options.width;
this._height = options.height;
this._keepAspectRatio = (options.keepAspectRatio === null || options.keepAspectRatio === undefined) ? true : options.keepAspectRatio;
}
return this; return this;
} }
@ -31,7 +36,14 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic
if (source) { if (source) {
var image = null; var image = null;
if (this._width || this._height) { 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); UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0);
source.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height)); source.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height));
image = UIGraphicsGetImageFromCurrentImageContext(); image = UIGraphicsGetImageFromCurrentImageContext();
@ -53,14 +65,14 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic
} }
} }
export var takePicture = function (width?, height?): Promise<imageSource.ImageSource> { export var takePicture = function (width?, height?, keepAspectRatio?): Promise<imageSource.ImageSource> {
return new Promise<imageSource.ImageSource>((resolve, reject) => { return new Promise<imageSource.ImageSource>((resolve, reject) => {
var imagePickerController = new UIImagePickerController(); var imagePickerController = new UIImagePickerController();
var listener = null; var listener = null;
var reqWidth = width || 0; var reqWidth = width || 0;
var reqHeight = height || reqWidth; var reqHeight = height || reqWidth;
if (reqWidth && reqHeight) { if (reqWidth && reqHeight) {
listener = UIImagePickerControllerDelegateImpl.new().initWithCallbackWidthAndHeight(resolve, reqWidth, reqHeight); listener = UIImagePickerControllerDelegateImpl.new().initWithCallbackAndOptions(resolve, { width: reqWidth, height: reqHeight, keepAspectRatio: keepAspectRatio });
} }
else { else {
listener = UIImagePickerControllerDelegateImpl.new().initWithCallback(resolve); listener = UIImagePickerControllerDelegateImpl.new().initWithCallback(resolve);

4
ui/enums/enums.d.ts vendored
View File

@ -164,12 +164,12 @@
export var none: string; 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; 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, * 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 * the image is clipped to fit in the destination
*/ */