mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Added keep aspect ratio parameter to camera.takePicture method.
This commit is contained in:
@@ -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));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user