mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 04:41:36 +08:00
BCL: updated camera
This commit is contained in:
@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
export enum CameraPosition {
|
export enum CameraPosition {
|
||||||
FRONT = 0,
|
BACK = 0,
|
||||||
BACK = 1,
|
FRONT = 1,
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum FlashMode {
|
export enum FlashMode {
|
||||||
|
@ -1,11 +1,21 @@
|
|||||||
import appModule = require("application");
|
|
||||||
|
import promises = require("promises");
|
||||||
|
import imageSource = require("image-source");
|
||||||
|
import types = require("camera/camera-types");
|
||||||
|
import appModule = require("application");
|
||||||
|
import fs = require("file-system");
|
||||||
|
|
||||||
var REQUEST_IMAGE_CAPTURE: number = 1;
|
// merge the exports of the types module with the exports of this file
|
||||||
|
import merger = require("utils/module-merge");
|
||||||
|
declare var exports;
|
||||||
|
merger.merge(types, exports);
|
||||||
|
|
||||||
|
var REQUEST_IMAGE_CAPTURE: number = 2332;
|
||||||
var REQUEST_SELECT_PICTURE: number = 2;
|
var REQUEST_SELECT_PICTURE: number = 2;
|
||||||
|
|
||||||
export class CameraManager {
|
export class CameraManager {
|
||||||
public takePicture(params: any, onSuccess: (imageData: any) => any, onError?: (error: any) => any) {
|
public takePicture(params: any, onSuccess: (imageData: any) => any, onError?: (error: any) => any) {
|
||||||
var takePictureIntent = new android.content.Intent('android.media.action.IMAGE_CAPTURE');
|
var takePictureIntent = new android.content.Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
|
||||||
var androidApp = appModule.android;
|
var androidApp = appModule.android;
|
||||||
|
|
||||||
if (takePictureIntent.resolveActivity(androidApp.context.getPackageManager()) !== null) {
|
if (takePictureIntent.resolveActivity(androidApp.context.getPackageManager()) !== null) {
|
||||||
@ -19,9 +29,74 @@ export class CameraManager {
|
|||||||
var androidApp = appModule.android;
|
var androidApp = appModule.android;
|
||||||
|
|
||||||
readPictureIntent.setType('image/*');
|
readPictureIntent.setType('image/*');
|
||||||
readPictureIntent.setAction('android.intent.action.GET_CONTENT');
|
readPictureIntent.setAction(android.content.Intent.ACTION_GET_CONTENT);
|
||||||
|
|
||||||
androidApp.currentActivity.startActivityForResult(android.content.Intent.createChooser(readPictureIntent, 'Select Picture'),
|
androidApp.currentActivity.startActivityForResult(android.content.Intent.createChooser(readPictureIntent, 'Select Picture'),
|
||||||
REQUEST_SELECT_PICTURE);
|
REQUEST_SELECT_PICTURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getTempFile()
|
||||||
|
{
|
||||||
|
var timeStamp = new java.text.SimpleDateFormat("yyyyMMdd_HHmmss").format(new java.util.Date());
|
||||||
|
var imageFileName = "JPEG_" + timeStamp + "_";
|
||||||
|
// FIXME add pictures to knownFolders of file-system?
|
||||||
|
var documents = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_PICTURES);
|
||||||
|
var imagePath = java.io.File.createTempFile(imageFileName, ".jpg", documents);
|
||||||
|
imagePath.deleteOnExit();
|
||||||
|
// console.log("image path: " + imagePath);
|
||||||
|
return imagePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
export var takePicture = function (options?: types.Options): promises.Promise<imageSource.ImageSource> {
|
||||||
|
var d = promises.defer<imageSource.ImageSource>();
|
||||||
|
|
||||||
|
var takePictureIntent = new android.content.Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
|
||||||
|
var androidApp = appModule.android;
|
||||||
|
var imageFile;
|
||||||
|
|
||||||
|
if (takePictureIntent.resolveActivity(androidApp.context.getPackageManager()) !== null) {
|
||||||
|
var broadcastReceiver;
|
||||||
|
var localBroadcast;
|
||||||
|
var CameraBroadcastReceiver = (<any>android.content.BroadcastReceiver).extends({
|
||||||
|
onReceive: function (context: android.content.Context, intent: android.content.Intent) {
|
||||||
|
// console.log("onReceive - file is: " + imageFile);
|
||||||
|
localBroadcast.unregisterReceiver(broadcastReceiver);
|
||||||
|
var requestCode = intent.getIntExtra("_requestCode", -1);
|
||||||
|
var requestResult = intent.getIntExtra("_resultCode", -1);
|
||||||
|
// console.log("requestCode: " + requestCode + ", requestResult: " + requestResult);
|
||||||
|
if (requestCode === REQUEST_IMAGE_CAPTURE) {
|
||||||
|
if (requestResult == android.app.Activity.RESULT_OK) {
|
||||||
|
var image = imageSource.fromFile(imageFile.getAbsolutePath());
|
||||||
|
if (image && image.android) {
|
||||||
|
d.resolve(image);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
d.reject(new Error("invalid image at: " + imageFile));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
d.reject(new Error("user cancel"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (imageFile) {
|
||||||
|
imageFile.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
var broadcastReceiver = new CameraBroadcastReceiver();
|
||||||
|
var localBroadcast = (<any>android).support.v4.content.LocalBroadcastManager.getInstance(androidApp.currentActivity);
|
||||||
|
localBroadcast.registerReceiver(broadcastReceiver, new android.content.IntentFilter("inline-data"));
|
||||||
|
imageFile = getTempFile();
|
||||||
|
takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, android.net.Uri.fromFile(imageFile));
|
||||||
|
|
||||||
|
// warning this code might not work on all devices
|
||||||
|
if (options && options.cameraPosition && (types.CameraPosition.FRONT === options.cameraPosition)) {
|
||||||
|
takePictureIntent.putExtra("android.intent.extras.CAMERA_FACING", 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
androidApp.currentActivity.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
return d.promise();
|
||||||
|
}
|
||||||
|
11
camera/camera.d.ts
vendored
11
camera/camera.d.ts
vendored
@ -8,14 +8,15 @@ declare module "camera" {
|
|||||||
* Specifies a camera position on a device.
|
* Specifies a camera position on a device.
|
||||||
*/
|
*/
|
||||||
enum CameraPosition {
|
enum CameraPosition {
|
||||||
/**
|
|
||||||
* The camera is located at the front of the device, facing the user.
|
|
||||||
*/
|
|
||||||
FRONT = 0,
|
|
||||||
/**
|
/**
|
||||||
* The camera is located at the back of the device.
|
* The camera is located at the back of the device.
|
||||||
*/
|
*/
|
||||||
BACK = 1,
|
BACK = 0,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The camera is located at the front of the device, facing the user.
|
||||||
|
*/
|
||||||
|
FRONT = 1,
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -46,7 +46,7 @@ export var takePicture = function (options?: types.Options): promises.Promise<im
|
|||||||
|
|
||||||
implementation: {
|
implementation: {
|
||||||
imagePickerControllerDidFinishPickingMediaWithInfo: function (picker, info) {
|
imagePickerControllerDidFinishPickingMediaWithInfo: function (picker, info) {
|
||||||
console.log('takeImage received');
|
// console.log('takeImage received');
|
||||||
picker.presentingViewController.dismissViewControllerAnimatedCompletion(true, null);
|
picker.presentingViewController.dismissViewControllerAnimatedCompletion(true, null);
|
||||||
listener = null;
|
listener = null;
|
||||||
var image = imageSource.fromNativeSource(info.valueForKey(UIKit.UIImagePickerControllerOriginalImage));
|
var image = imageSource.fromNativeSource(info.valueForKey(UIKit.UIImagePickerControllerOriginalImage));
|
||||||
@ -54,7 +54,7 @@ export var takePicture = function (options?: types.Options): promises.Promise<im
|
|||||||
},
|
},
|
||||||
|
|
||||||
imagePickerControllerDidCancel: function (picker) {
|
imagePickerControllerDidCancel: function (picker) {
|
||||||
console.info('takeImage canceled');
|
// console.info('takeImage canceled');
|
||||||
picker.presentingViewController.dismissViewControllerAnimatedCompletion(true, null);
|
picker.presentingViewController.dismissViewControllerAnimatedCompletion(true, null);
|
||||||
listener = null;
|
listener = null;
|
||||||
d.reject(new Error('takePicture canceled by user'));
|
d.reject(new Error('takePicture canceled by user'));
|
||||||
|
Reference in New Issue
Block a user