initial camera for iOS code, spike quality

This commit is contained in:
Stanimir Karoserov
2014-05-20 17:44:47 +03:00
parent 1054566b4c
commit 58dd62753b
5 changed files with 119 additions and 8 deletions

View File

@ -218,6 +218,9 @@
<TypeScriptCompile Include="libjs.d.ts" />
</ItemGroup>
<ItemGroup>
<TypeScriptCompile Include="camera\camera-types.ts">
<DependentUpon>camera.d.ts</DependentUpon>
</TypeScriptCompile>
<Content Include="_references.ts" />
<Content Include="image-source\Readme.md" />
<Content Include="http\Readme.md" />

View File

@ -13,15 +13,14 @@
// </snippet>
import imageSource = require("image-source/image-source");
import app = require("application/application");
import fs = require("file-system/file-system");
import app = require("application/application");
import TKUnit = require("Tests/TKUnit");
export var testFromResource = function () {
// <snippet name="image-source">
// ### Load image using resource name
// this similar to loading Bitmap from `R.drawable.logo` on Android or calling `[UIImage imageNamed@"logo"]` on iOS
// This is similar to loading Bitmap from `R.drawable.logo` on Android or calling `[UIImage imageNamed@"logo"]` on iOS
// ``` JavaScript
var img = imageSource.fromResource("logo");
// ```

23
camera/camera-types.ts Normal file
View File

@ -0,0 +1,23 @@

export enum CameraPosition {
FRONT = 0,
BACK = 1,
}
export enum FlashMode {
AUTO = 0, // default
ON = 1,
OFF = 2
}
export interface Options {
/**
* Specifies which Camera to use
*/
cameraPosition?: CameraPosition;
/**
* Specifies flash mode
*/
flashMode?: FlashMode;
}

22
camera/camera.d.ts vendored
View File

@ -1,4 +1,8 @@
export declare enum CameraPosition {

import promises = require("promises/promises");
import imageSource = require("image-source/image-source");
export declare enum CameraPosition {
FRONT = 0,
BACK = 1,
}
@ -9,10 +13,24 @@ export declare enum FlashMode {
OFF = 2
}
export interface Options {
/**
* Specifies which Camera to use
*/
cameraPosition?: CameraPosition;
/**
* Specifies flash mode
*/
flashMode?: FlashMode;
}
// TODO most of hardware related parts need to handle onPause and onResume of the calling activities
export declare class CameraManager {
takePicture(params: any, onSuccess: (imageData: any) => any, onError?: (error: any) => any);
// options { useSavedPhotos: true }
pictureFromLibrary(params: any, onSuccess: (imageData: any) => any, onError?: (error: any) => any);
}
}
export declare var takePicture: (options?: Options) => promises.Promise<imageSource.ImageSource>;

View File

@ -1,5 +1,9 @@
var REQUEST_IMAGE_CAPTURE: number = 1;
var REQUEST_SELECT_PICTURE: number = 2;

import promises = require("promises/promises");
import imageSource = require("image-source/image-source");
import types = require("camera/camera-types");
var imagePickerController;
export class CameraManager {
public takePicture(params: any, onSuccess: (imageData: any) => any, onError?: (error: any) => any) {
@ -8,4 +12,68 @@ export class CameraManager {
// options { useSavedPhotos: true }
public pictureFromLibrary(params: any, onSuccess: (imageData: any) => any, onError?: (error: any) => any) {
}
}
}
function topViewController():UIKit.UIViewController {
return topViewControllerWithRootViewController(UIKit.UIApplication.sharedApplication().keyWindow.rootViewController);
}
function topViewControllerWithRootViewController(rootViewController: any) {
if (rootViewController.isKindOfClass(UIKit.UITabBarController.class())) {
// console.log("Using tab view controller");
return topViewControllerWithRootViewController(<UIKit.UITabBarController>rootViewController.selectedViewController);
} else if (rootViewController.isKindOfClass(UIKit.UINavigationController.class())) {
// console.log("Using navigation view controller");
return topViewControllerWithRootViewController(<UIKit.UINavigationController>rootViewController.visibleViewController);
} else if (rootViewController.presentedViewController) {
// console.log("Using presented view controller");
return topViewControllerWithRootViewController(rootViewController.presentedViewController);
} else {
// console.log("Using view controller");
return rootViewController;
}
}
export var takePicture = function (options?: types.Options): promises.Promise<imageSource.ImageSource> {
var d = promises.defer<imageSource.ImageSource>();
// FIXME: this is done to prevent listener from being freed by JS garbage collector
// we will try to fix this at JS Bridge level
var listener;
var ImagePickerControllerListener = Foundation.NSObject.extends({
}, {}).implements({
protocol: "UIImagePickerControllerDelegate",
implementation: {
imagePickerControllerDidFinishPickingMediaWithInfo: function (picker, info) {
console.log('takeImage received');
picker.presentingViewController.dismissViewControllerAnimatedCompletion(true, null);
// FIXME: do not work with listener here
listener = null;
var image = imageSource.fromNativeSource(info.valueForKey(UIKit.UIImagePickerControllerOriginalImage));
d.resolve(image);
},
imagePickerControllerDidCancel: function (picker) {
console.info('takeImage canceled');
picker.presentingViewController.dismissViewControllerAnimatedCompletion(true, null);
// FIXME: do not work with listener here
listener = null;
d.reject(new Error('takePicture canceled by user'));
}
}
});
imagePickerController = new UIKit.UIImagePickerController();
listener = new ImagePickerControllerListener();
imagePickerController.delegate = listener;
imagePickerController.mediaTypes = UIKit.UIImagePickerController.availableMediaTypesForSourceType(UIKit.UIImagePickerControllerSourceType.UIImagePickerControllerSourceTypeCamera);
imagePickerController.sourceType = UIKit.UIImagePickerControllerSourceType.UIImagePickerControllerSourceTypeCamera;
imagePickerController.modalPresentationStyle = UIKit.UIModalPresentationStyle.UIModalPresentationCurrentContext;
topViewController().presentViewControllerAnimatedCompletion(imagePickerController, true, null);
return d.promise();
}