mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 11:42:04 +08:00
Added keep aspect ratio parameter to camera.takePicture method.
This commit is contained in:
@ -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
11
camera/camera-common.ts
Normal 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)
|
||||
};
|
||||
}
|
@ -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<imageSource.ImageSource> {
|
||||
export var takePicture = function (width?, height?, keepAspectRatio?): Promise<imageSource.ImageSource> {
|
||||
return new Promise<imageSource.ImageSource>((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<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 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
3
camera/camera.d.ts
vendored
@ -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<imageSource.ImageSource>;
|
||||
export function takePicture(width?: number, heigth?: number, keepAspectRatio?: boolean): Promise<imageSource.ImageSource>;
|
||||
}
|
||||
|
@ -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<imageSource.ImageSource> {
|
||||
export var takePicture = function (width?, height?, keepAspectRatio?): 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;
|
||||
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
4
ui/enums/enums.d.ts
vendored
@ -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
|
||||
*/
|
||||
|
Reference in New Issue
Block a user