camera takePicture parameters wrapped in an option object.

This commit is contained in:
Nedyalko Nikolov
2015-04-16 16:50:14 +03:00
committed by Rossen Hristov
parent 267ce57d09
commit d86a19cb28
3 changed files with 24 additions and 8 deletions

View File

@ -2,16 +2,20 @@
import appModule = require("application"); import appModule = require("application");
import fileSystem = require("file-system"); import fileSystem = require("file-system");
import utils = require("utils/utils"); import utils = require("utils/utils");
import definition = require("camera");
import common = require("./camera-common"); import common = require("./camera-common");
var REQUEST_IMAGE_CAPTURE = 3453; var REQUEST_IMAGE_CAPTURE = 3453;
export var takePicture = function (width?, height?, keepAspectRatio?): Promise<imageSource.ImageSource> { export var takePicture = function (options?: definition.CameraOptions): Promise<imageSource.ImageSource> {
return new Promise<imageSource.ImageSource>((resolve, reject) => { return new Promise<imageSource.ImageSource>((resolve, reject) => {
try { try {
var density = utils.layout.getDisplayDensity(); var density = utils.layout.getDisplayDensity();
var reqWidth = width ? width * density : 0; if (options) {
var reqHeight = height ? height * density : reqWidth; 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 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");
@ -34,7 +38,6 @@ export var takePicture = function (width?, height?, keepAspectRatio?): Promise<i
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);
var shouldKeepAspectRatio = (keepAspectRatio === null || keepAspectRatio === undefined) ? true : keepAspectRatio;
var scaledSizeImage = null; var scaledSizeImage = null;
if (reqHeight > 0 && reqWidth > 0) { if (reqHeight > 0 && reqWidth > 0) {
if (shouldKeepAspectRatio) { if (shouldKeepAspectRatio) {

8
camera/camera.d.ts vendored
View File

@ -11,5 +11,11 @@ declare module "camera" {
* @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. * @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, keepAspectRatio?: boolean): 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,6 @@
import imageSource = require("image-source"); import imageSource = require("image-source");
import frame = require("ui/frame"); import frame = require("ui/frame");
import definition = require("camera");
import common = require("./camera-common"); import common = require("./camera-common");
class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePickerControllerDelegate { class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePickerControllerDelegate {
@ -65,12 +66,18 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic
} }
} }
export var takePicture = function (width?, height?, keepAspectRatio?): Promise<imageSource.ImageSource> { export var takePicture = function (options?: definition.CameraOptions): 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 = 0;
var reqHeight = height || reqWidth; 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) { if (reqWidth && reqHeight) {
listener = UIImagePickerControllerDelegateImpl.new().initWithCallbackAndOptions(resolve, { width: reqWidth, height: reqHeight, keepAspectRatio: keepAspectRatio }); listener = UIImagePickerControllerDelegateImpl.new().initWithCallbackAndOptions(resolve, { width: reqWidth, height: reqHeight, keepAspectRatio: keepAspectRatio });
} }