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

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