mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 04:41:36 +08:00
Updated camera module to support full size images (Android) and custom size images.
This commit is contained in:
@ -1,12 +1,20 @@
|
|||||||
import imageSource = require("image-source");
|
import imageSource = require("image-source");
|
||||||
import appModule = require("application");
|
import appModule = require("application");
|
||||||
|
import fileSystem = require("file-system");
|
||||||
|
|
||||||
var REQUEST_IMAGE_CAPTURE = 3453;
|
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) => {
|
return new Promise<imageSource.ImageSource>((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
|
var reqWidth = width || 0;
|
||||||
|
var reqHeight = height || reqWidth;
|
||||||
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 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) {
|
if (takePictureIntent.resolveActivity(appModule.android.context.getPackageManager()) != null) {
|
||||||
|
|
||||||
var previousResult = appModule.android.onActivityResult;
|
var previousResult = appModule.android.onActivityResult;
|
||||||
@ -14,15 +22,50 @@ export var takePicture = function (): Promise<imageSource.ImageSource> {
|
|||||||
appModule.android.onActivityResult = previousResult;
|
appModule.android.onActivityResult = previousResult;
|
||||||
|
|
||||||
if (requestCode === REQUEST_IMAGE_CAPTURE && resultCode === android.app.Activity.RESULT_OK) {
|
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);
|
appModule.android.foregroundActivity.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
|
||||||
|
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} 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
4
camera/camera.d.ts
vendored
@ -7,6 +7,8 @@ declare module "camera" {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Take a photo using the 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>;
|
||||||
}
|
}
|
||||||
|
@ -10,19 +10,39 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic
|
|||||||
|
|
||||||
private _callback: (result?: imageSource.ImageSource) => void;
|
private _callback: (result?: imageSource.ImageSource) => void;
|
||||||
|
|
||||||
|
private _width: number;
|
||||||
|
private _height: number;
|
||||||
|
|
||||||
public initWithCallback(callback: (result?: imageSource.ImageSource) => void): UIImagePickerControllerDelegateImpl {
|
public initWithCallback(callback: (result?: imageSource.ImageSource) => void): UIImagePickerControllerDelegateImpl {
|
||||||
this._callback = callback;
|
this._callback = callback;
|
||||||
return this;
|
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 {
|
imagePickerControllerDidFinishPickingMediaWithInfo(picker, info): void {
|
||||||
if (info) {
|
if (info) {
|
||||||
var source = info.valueForKey(UIImagePickerControllerOriginalImage);
|
var source = info.valueForKey(UIImagePickerControllerOriginalImage);
|
||||||
if (source) {
|
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) {
|
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) => {
|
return new Promise<imageSource.ImageSource>((resolve, reject) => {
|
||||||
var imagePickerController = new UIImagePickerController();
|
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;
|
imagePickerController.delegate = listener;
|
||||||
|
|
||||||
if (UIDevice.currentDevice().model !== "iPhone Simulator") {
|
if (UIDevice.currentDevice().model !== "iPhone Simulator") {
|
||||||
|
Reference in New Issue
Block a user