Updated camera module to support full size images (Android) and custom size images.

This commit is contained in:
Nedyalko Nikolov
2015-04-15 15:31:51 +03:00
parent ca03b69e6e
commit 2fafb81d8f
3 changed files with 82 additions and 9 deletions

View File

@ -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<imageSource.ImageSource> {
export var takePicture = function (width?, height?): Promise<imageSource.ImageSource> {
return new Promise<imageSource.ImageSource>((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<imageSource.ImageSource> {
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;
}

4
camera/camera.d.ts vendored
View File

@ -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<imageSource.ImageSource>;
export function takePicture(width?: number, heigth?: number): Promise<imageSource.ImageSource>;
}

View File

@ -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<imageSource.ImageSource> {
export var takePicture = function (width?, height?): Promise<imageSource.ImageSource> {
return new Promise<imageSource.ImageSource>((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") {