From 2fafb81d8f8a6336e2ce380d6f11e35a725b942c Mon Sep 17 00:00:00 2001 From: Nedyalko Nikolov Date: Wed, 15 Apr 2015 15:31:51 +0300 Subject: [PATCH] Updated camera module to support full size images (Android) and custom size images. --- camera/camera.android.ts | 51 ++++++++++++++++++++++++++++++++++++---- camera/camera.d.ts | 4 +++- camera/camera.ios.ts | 36 ++++++++++++++++++++++++---- 3 files changed, 82 insertions(+), 9 deletions(-) diff --git a/camera/camera.android.ts b/camera/camera.android.ts index f956b5663..ed66dbaa1 100644 --- a/camera/camera.android.ts +++ b/camera/camera.android.ts @@ -1,12 +1,20 @@ import imageSource = require("image-source"); import appModule = require("application"); +import fileSystem = require("file-system"); var REQUEST_IMAGE_CAPTURE = 3453; -export var takePicture = function (): Promise { +export var takePicture = function (width?, height?): Promise { return new Promise((resolve, reject) => { try { + var reqWidth = width || 0; + var reqHeight = height || 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"); + var nativeFile = new java.io.File(tempPicturePath); + var tempPictureUri = android.net.Uri.fromFile(nativeFile); + takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, tempPictureUri); if (takePictureIntent.resolveActivity(appModule.android.context.getPackageManager()) != null) { var previousResult = appModule.android.onActivityResult; @@ -14,15 +22,50 @@ export var takePicture = function (): Promise { appModule.android.onActivityResult = previousResult; if (requestCode === REQUEST_IMAGE_CAPTURE && resultCode === android.app.Activity.RESULT_OK) { - resolve(imageSource.fromNativeSource(data.getExtras().get("data"))) - } + var options = new android.graphics.BitmapFactory.Options(); + options.inJustDecodeBounds = true; + android.graphics.BitmapFactory.decodeFile(tempPicturePath, options); + + var sampleSize = calculateInSampleSize(options.outWidth, options.outHeight, reqWidth, reqHeight); + + var finalBitmapOptions = new android.graphics.BitmapFactory.Options(); + finalBitmapOptions.inSampleSize = sampleSize; + var bitmap = android.graphics.BitmapFactory.decodeFile(tempPicturePath, finalBitmapOptions); + resolve(imageSource.fromNativeSource(bitmap)); + } }; appModule.android.foregroundActivity.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); } } catch (e) { - reject(e); + if (reject) { + reject(e); + } } }); +} + +var calculateInSampleSize = function (imageWidth, imageHeight, reqWidth, reqHeight) { + var sampleSize = 1; + if (imageWidth > reqWidth && imageHeight > reqHeight) { + var halfWidth = imageWidth / 2; + var halfHeight = imageHeight / 2; + while((halfWidth / sampleSize) > reqWidth && (halfHeight / sampleSize) > reqHeight) { + sampleSize *= 2; + } + } + return sampleSize; +} + +var createDateTimeStamp = function() { + var result = ""; + var date = new Date(); + result = date.getDate().toString() + + (date.getMonth() + 1).toString() + + date.getFullYear().toString() + + date.getHours().toString() + + date.getMinutes().toString() + + date.getSeconds().toString(); + return result; } \ No newline at end of file diff --git a/camera/camera.d.ts b/camera/camera.d.ts index cb3f6ae3f..45cffabac 100644 --- a/camera/camera.d.ts +++ b/camera/camera.d.ts @@ -7,6 +7,8 @@ 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. */ - export function takePicture(): Promise; + export function takePicture(width?: number, heigth?: number): Promise; } diff --git a/camera/camera.ios.ts b/camera/camera.ios.ts index 6bf35d1eb..cbd9c1764 100644 --- a/camera/camera.ios.ts +++ b/camera/camera.ios.ts @@ -10,19 +10,39 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic private _callback: (result?: imageSource.ImageSource) => void; + private _width: number; + private _height: number; + public initWithCallback(callback: (result?: imageSource.ImageSource) => void): UIImagePickerControllerDelegateImpl { this._callback = callback; return this; } + public initWithCallbackWidthAndHeight(callback: (result?: imageSource.ImageSource) => void, width, height): UIImagePickerControllerDelegateImpl { + this._callback = callback; + this._width = width; + this._height = height; + return this; + } + imagePickerControllerDidFinishPickingMediaWithInfo(picker, info): void { if (info) { var source = info.valueForKey(UIImagePickerControllerOriginalImage); if (source) { - var image = imageSource.fromNativeSource(source); + var image = null; + if (this._width || this._height) { + console.log("Image resized!!!"); + var newSize = CGSizeMake(this._width, this._height); + UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0); + source.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height)); + image = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + } + + var imageSourceResult = image ? imageSource.fromNativeSource(image) : imageSource.fromNativeSource(source); if (this._callback) { - this._callback(image); + this._callback(imageSourceResult); } } } @@ -34,10 +54,18 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic } } -export var takePicture = function (): Promise { +export var takePicture = function (width?, height?): Promise { return new Promise((resolve, reject) => { var imagePickerController = new UIImagePickerController(); - var listener = UIImagePickerControllerDelegateImpl.new().initWithCallback(resolve); + var listener = null; + var reqWidth = width || 0; + var reqHeight = height || reqWidth; + if (reqWidth && reqHeight) { + listener = UIImagePickerControllerDelegateImpl.new().initWithCallbackWidthAndHeight(resolve, reqWidth, reqHeight); + } + else { + listener = UIImagePickerControllerDelegateImpl.new().initWithCallback(resolve); + } imagePickerController.delegate = listener; if (UIDevice.currentDevice().model !== "iPhone Simulator") {