From b2411b02e910a318109fb27baf31700006986cfc Mon Sep 17 00:00:00 2001 From: Stanimir Karoserov Date: Tue, 27 May 2014 14:28:11 +0300 Subject: [PATCH] BCL: updated camera --- camera/camera-types.ts | 4 +- camera/camera.android.ts | 85 +++++++++++++++++++++++++++++++++++++--- camera/camera.d.ts | 11 +++--- camera/camera.ios.ts | 4 +- 4 files changed, 90 insertions(+), 14 deletions(-) diff --git a/camera/camera-types.ts b/camera/camera-types.ts index e82bd6f4d..565425d94 100644 --- a/camera/camera-types.ts +++ b/camera/camera-types.ts @@ -1,7 +1,7 @@  export enum CameraPosition { - FRONT = 0, - BACK = 1, + BACK = 0, + FRONT = 1, } export enum FlashMode { diff --git a/camera/camera.android.ts b/camera/camera.android.ts index e78eacc27..be49d12a7 100644 --- a/camera/camera.android.ts +++ b/camera/camera.android.ts @@ -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; export class CameraManager { 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; if (takePictureIntent.resolveActivity(androidApp.context.getPackageManager()) !== null) { @@ -19,9 +29,74 @@ export class CameraManager { var androidApp = appModule.android; 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'), REQUEST_SELECT_PICTURE); } -} \ No newline at end of file +} + +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 { + var d = promises.defer(); + + 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 = (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 = (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(); +} diff --git a/camera/camera.d.ts b/camera/camera.d.ts index d399f6a56..bda4fdb8d 100644 --- a/camera/camera.d.ts +++ b/camera/camera.d.ts @@ -8,14 +8,15 @@ declare module "camera" { * Specifies a camera position on a device. */ 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. */ - BACK = 1, + BACK = 0, + + /** + * The camera is located at the front of the device, facing the user. + */ + FRONT = 1, } /** diff --git a/camera/camera.ios.ts b/camera/camera.ios.ts index 921108aff..a9058779e 100644 --- a/camera/camera.ios.ts +++ b/camera/camera.ios.ts @@ -46,7 +46,7 @@ export var takePicture = function (options?: types.Options): promises.Promise