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