imports improved

This commit is contained in:
Vladimir Enchev
2015-12-14 17:08:24 +02:00
parent cbf69e60e8
commit 074ac89871
90 changed files with 529 additions and 266 deletions

View File

@@ -1,16 +1,18 @@
import imageSource = require("image-source");
import appModule = require("application");
import fileSystem = require("file-system");
import utils = require("utils/utils");
import types = require("utils/types");
import definition = require("camera");
import common = require("./camera-common");
import * as typesModule from "utils/types";
import * as utilsModule from "utils/utils";
import * as fileSystemModule from "file-system";
import * as applicationModule from "application";
import * as imageSourceModule from "image-source";
import * as cameraCommonModule from "./camera-common";
var REQUEST_IMAGE_CAPTURE = 3453;
export var takePicture = function (options?: definition.CameraOptions): Promise<imageSource.ImageSource> {
return new Promise<imageSource.ImageSource>((resolve, reject) => {
export var takePicture = function (options?): Promise<any> {
return new Promise((resolve, reject) => {
try {
var types: typeof typesModule = require("utils/types");
var utils: typeof utilsModule = require("utils/utils");
var density = utils.layout.getDisplayDensity();
if (options) {
var reqWidth = options.width ? options.width * density : 0;
@@ -19,12 +21,17 @@ export var takePicture = function (options?: definition.CameraOptions): Promise<
}
var takePictureIntent = new android.content.Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
var dateStamp = createDateTimeStamp();
var fileSystem: typeof fileSystemModule = require("file-system");
var tempPicturePath = fileSystem.path.join(utils.ad.getApplicationContext().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(utils.ad.getApplicationContext().getPackageManager()) != null) {
var appModule: typeof applicationModule = require("application");
var previousResult = appModule.android.onActivityResult;
appModule.android.onActivityResult = (requestCode: number, resultCode: number, data: android.content.Intent) => {
appModule.android.onActivityResult = previousResult;
@@ -42,6 +49,9 @@ export var takePicture = function (options?: definition.CameraOptions): Promise<
var scaledSizeImage = null;
if (reqHeight > 0 && reqWidth > 0) {
if (shouldKeepAspectRatio) {
var common: typeof cameraCommonModule = require("./camera-common");
var aspectSafeSize = common.getAspectSafeDimensions(bitmap.getWidth(), bitmap.getHeight(), reqWidth, reqHeight);
scaledSizeImage = android.graphics.Bitmap.createScaledBitmap(bitmap, aspectSafeSize.width, aspectSafeSize.height, true);
}
@@ -52,6 +62,9 @@ export var takePicture = function (options?: definition.CameraOptions): Promise<
else {
scaledSizeImage = bitmap;
}
var imageSource: typeof imageSourceModule = require("image-source");
resolve(imageSource.fromNativeSource(scaledSizeImage));
}
};
@@ -72,21 +85,21 @@ var calculateInSampleSize = function (imageWidth, imageHeight, reqWidth, reqHeig
if (imageWidth > reqWidth && imageHeight > reqHeight) {
var halfWidth = imageWidth / 2;
var halfHeight = imageHeight / 2;
while((halfWidth / sampleSize) > reqWidth && (halfHeight / sampleSize) > reqHeight) {
while ((halfWidth / sampleSize) > reqWidth && (halfHeight / sampleSize) > reqHeight) {
sampleSize *= 2;
}
}
return sampleSize;
}
var createDateTimeStamp = function() {
var createDateTimeStamp = function () {
var result = "";
var date = new Date();
result = (date.getDate() < 10 ? "0" + date.getDate().toString() : date.getDate().toString())+
((date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1).toString() : (date.getMonth() + 1).toString()) +
date.getFullYear().toString() +
date.getHours().toString() +
date.getMinutes().toString() +
date.getSeconds().toString();
result = (date.getDate() < 10 ? "0" + date.getDate().toString() : date.getDate().toString()) +
((date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1).toString() : (date.getMonth() + 1).toString()) +
date.getFullYear().toString() +
date.getHours().toString() +
date.getMinutes().toString() +
date.getSeconds().toString();
return result;
}

View File

@@ -1,8 +1,7 @@
import imageSource = require("image-source");
import frame = require("ui/frame");
import definition = require("camera");
import common = require("./camera-common");
import types = require("utils/types");
var types = require("utils/types");
import * as cameraCommonModule from "./camera-common";
import * as imageSourceModule from "image-source";
import * as frameModule from "ui/frame";
class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePickerControllerDelegate {
public static ObjCProtocols = [UIImagePickerControllerDelegate];
@@ -11,22 +10,23 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic
return <UIImagePickerControllerDelegateImpl>super.new();
}
private _callback: (result?: imageSource.ImageSource) => void;
private _callback: (result?) => void;
private _width: number;
private _height: number;
private _keepAspectRatio: boolean;
public initWithCallback(callback: (result?: imageSource.ImageSource) => void): UIImagePickerControllerDelegateImpl {
public initWithCallback(callback: (result?) => void): UIImagePickerControllerDelegateImpl {
this._callback = callback;
return this;
}
public initWithCallbackAndOptions(callback: (result?: imageSource.ImageSource) => void, options?): UIImagePickerControllerDelegateImpl {
public initWithCallbackAndOptions(callback: (result?) => void, options?): UIImagePickerControllerDelegateImpl {
this._callback = callback;
if (options) {
this._width = options.width;
this._height = options.height;
this._keepAspectRatio = types.isNullOrUndefined(options.keepAspectRatio) ? true : options.keepAspectRatio;
}
return this;
@@ -40,6 +40,8 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic
if (this._width || this._height) {
var newSize = null;
if (this._keepAspectRatio) {
var common: typeof cameraCommonModule = require("./camera-common");
var aspectSafeSize = common.getAspectSafeDimensions(source.size.width, source.size.height, this._width, this._height);
newSize = CGSizeMake(aspectSafeSize.width, aspectSafeSize.height);
}
@@ -52,6 +54,8 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic
UIGraphicsEndImageContext();
}
var imageSource: typeof imageSourceModule = require("image-source");
var imageSourceResult = image ? imageSource.fromNativeSource(image) : imageSource.fromNativeSource(source);
if (this._callback) {
@@ -71,8 +75,8 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic
var listener;
export var takePicture = function (options?: definition.CameraOptions): Promise<imageSource.ImageSource> {
return new Promise<imageSource.ImageSource>((resolve, reject) => {
export var takePicture = function (options): Promise<any> {
return new Promise((resolve, reject) => {
listener = null;
var imagePickerController = new UIImagePickerController();
var reqWidth = 0;
@@ -101,6 +105,8 @@ export var takePicture = function (options?: definition.CameraOptions): Promise<
imagePickerController.modalPresentationStyle = UIModalPresentationStyle.UIModalPresentationCurrentContext;
var frame: typeof frameModule = require("ui/frame");
var topMostFrame = frame.topmost();
if (topMostFrame) {
var viewController: UIViewController = topMostFrame.currentPage && topMostFrame.currentPage.ios;