Merge pull request #285 from NativeScript/nnikolov/CameraImprovements

Added keep aspect ratio parameter to camera.takePicture method.
This commit is contained in:
Nedyalko Nikolov
2015-04-16 18:13:37 +03:00
6 changed files with 75 additions and 15 deletions

View File

@ -722,6 +722,9 @@
<SubType>Designer</SubType>
</Content>
<TypeScriptCompile Include="camera\camera.d.ts" />
<TypeScriptCompile Include="camera\camera-common.ts" >
<DependentUpon>camera.d.ts</DependentUpon>
</TypeScriptCompile>
<TypeScriptCompile Include="camera\camera.android.ts">
<DependentUpon>camera.d.ts</DependentUpon>
</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,21 @@
import imageSource = require("image-source");
import appModule = require("application");
import fileSystem = require("file-system");
import utils = require("utils/utils");
import definition = require("camera");
import common = require("./camera-common");
var REQUEST_IMAGE_CAPTURE = 3453;
export var takePicture = function (width?, height?): Promise<imageSource.ImageSource> {
export var takePicture = function (options?: definition.CameraOptions): Promise<imageSource.ImageSource> {
return new Promise<imageSource.ImageSource>((resolve, reject) => {
try {
var reqWidth = width || 0;
var reqHeight = height || reqWidth;
var density = utils.layout.getDisplayDensity();
if (options) {
var reqWidth = options.width ? options.width * density : 0;
var reqHeight = options.height ? options.height * density : reqWidth;
var shouldKeepAspectRatio = (options.keepAspectRatio === null || options.keepAspectRatio === undefined) ? true : options.keepAspectRatio;
}
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 +38,20 @@ export var takePicture = function (width?, height?): Promise<imageSource.ImageSo
var finalBitmapOptions = new android.graphics.BitmapFactory.Options();
finalBitmapOptions.inSampleSize = sampleSize;
var bitmap = android.graphics.BitmapFactory.decodeFile(tempPicturePath, finalBitmapOptions);
resolve(imageSource.fromNativeSource(bitmap));
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));
}
};

9
camera/camera.d.ts vendored
View File

@ -9,6 +9,13 @@ 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<imageSource.ImageSource>;
export function takePicture(options?: CameraOptions): Promise<imageSource.ImageSource>;
export interface CameraOptions {
width?: number;
height?: number;
keepAspectRatio?: boolean;
}
}

View File

@ -1,5 +1,7 @@
import imageSource = require("image-source");
import frame = require("ui/frame");
import definition = require("camera");
import common = require("./camera-common");
class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePickerControllerDelegate {
public static ObjCProtocols = [UIImagePickerControllerDelegate];
@ -12,16 +14,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 +37,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 +66,20 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic
}
}
export var takePicture = function (width?, height?): Promise<imageSource.ImageSource> {
export var takePicture = function (options?: definition.CameraOptions): Promise<imageSource.ImageSource> {
return new Promise<imageSource.ImageSource>((resolve, reject) => {
var imagePickerController = new UIImagePickerController();
var listener = null;
var reqWidth = width || 0;
var reqHeight = height || reqWidth;
var reqWidth = 0;
var reqHeight = 0;
var keepAspectRatio = true;
if (options) {
reqWidth = options.width || 0;
reqHeight = options.height || reqWidth;
keepAspectRatio = (options.keepAspectRatio === null || options.keepAspectRatio === undefined) ? true : options.keepAspectRatio;
}
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);

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

@ -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
*/