Skip to content

Commit cf3cd35

Browse files
author
Nedyalko Nikolov
committed
Fixed breaking change in camera for android.
1 parent a5e2d46 commit cf3cd35

File tree

5 files changed

+105
-96
lines changed

5 files changed

+105
-96
lines changed

camera/Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
```
33
var camera = require("camera");
44
5-
camera.takePicture({"cameraPosition": camera.CameraPosition.BACK, "flashMode": camera.FlashMode.ON}).then(function (image) {
5+
camera.takePicture({"width": 300, "height": 300, "keepAspectRatio": true, "saveToGallery": true}).then(function (image) {
66
console.log('pic taken - width: ' + image.width + ", height: " + image.height);
77
}).fail(function (error) {
88
console.log('pic canceled');

camera/camera-common.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export function getAspectSafeDimensions(sourceWidth, sourceHeight, reqWidth, reqHeight) {
2-
var widthCoef = sourceWidth / reqWidth;
3-
var heightCoef = sourceHeight / reqHeight;
2+
let widthCoef = sourceWidth / reqWidth;
3+
let heightCoef = sourceHeight / reqHeight;
44

5-
var aspectCoef = widthCoef > heightCoef ? widthCoef : heightCoef;
5+
let aspectCoef = widthCoef > heightCoef ? widthCoef : heightCoef;
66

77
return {
88
width: Math.floor(sourceWidth / aspectCoef),

camera/camera.android.ts

Lines changed: 71 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -9,89 +9,93 @@ var REQUEST_IMAGE_CAPTURE = 3453;
99
export var takePicture = function (options?): Promise<any> {
1010
return new Promise((resolve, reject) => {
1111
try {
12-
var types: typeof typesModule = require("utils/types");
13-
var utils: typeof utilsModule = require("utils/utils");
14-
15-
var density = utils.layout.getDisplayDensity();
12+
let types: typeof typesModule = require("utils/types");
13+
let utils: typeof utilsModule = require("utils/utils");
14+
15+
let saveToGallery;
16+
let reqWidth;
17+
let reqHeight;
18+
let shouldKeepAspectRatio;
19+
20+
let density = utils.layout.getDisplayDensity();
1621
if (options) {
17-
var saveToGallery = options.saveToGallery ? true : false;
18-
var reqWidth = options.width ? options.width * density : 0;
19-
var reqHeight = options.height ? options.height * density : reqWidth;
20-
var shouldKeepAspectRatio = types.isNullOrUndefined(options.keepAspectRatio) ? true : options.keepAspectRatio;
22+
saveToGallery = options.saveToGallery ? true : false;
23+
reqWidth = options.width ? options.width * density : 0;
24+
reqHeight = options.height ? options.height * density : reqWidth;
25+
shouldKeepAspectRatio = types.isNullOrUndefined(options.keepAspectRatio) ? true : options.keepAspectRatio;
2126
}
22-
var takePictureIntent = new android.content.Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
23-
var dateStamp = createDateTimeStamp();
27+
let takePictureIntent = new android.content.Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
28+
let dateStamp = createDateTimeStamp();
29+
30+
let picturePath: string;
31+
let nativeFile;
32+
let tempPictureUri;
2433

2534
if (saveToGallery) {
26-
var picturePath = android.os.Environment.getExternalStoragePublicDirectory(
35+
picturePath = android.os.Environment.getExternalStoragePublicDirectory(
2736
android.os.Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/" + "cameraPicture_" + dateStamp + ".jpg";
28-
var nativeFile = new java.io.File(picturePath);
29-
var tempPictureUri = android.net.Uri.fromFile(nativeFile);
37+
nativeFile = new java.io.File(picturePath);
38+
tempPictureUri = android.net.Uri.fromFile(nativeFile);
3039
takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, tempPictureUri);
3140
} else {
32-
var picturePath = utils.ad.getApplicationContext().getExternalFilesDir(null).getAbsolutePath() + "/" + "cameraPicture_" + dateStamp + ".jpg";
33-
var nativeFile = new java.io.File(picturePath);
34-
var tempPictureUri = android.net.Uri.fromFile(nativeFile);
41+
picturePath = utils.ad.getApplicationContext().getExternalFilesDir(null).getAbsolutePath() + "/" + "cameraPicture_" + dateStamp + ".jpg";
42+
nativeFile = new java.io.File(picturePath);
43+
tempPictureUri = android.net.Uri.fromFile(nativeFile);
3544
takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, tempPictureUri);
3645
}
3746

3847
if (takePictureIntent.resolveActivity(utils.ad.getApplicationContext().getPackageManager()) != null) {
3948

40-
var appModule: typeof applicationModule = require("application");
49+
let appModule: typeof applicationModule = require("application");
4150

42-
var previousResult = appModule.android.onActivityResult;
51+
let previousResult = appModule.android.onActivityResult;
4352
appModule.android.onActivityResult = (requestCode: number, resultCode: number, data: android.content.Intent) => {
4453
appModule.android.onActivityResult = previousResult;
4554

4655
if (requestCode === REQUEST_IMAGE_CAPTURE && resultCode === android.app.Activity.RESULT_OK) {
47-
var imageSource: typeof imageSourceModule = require("image-source");
48-
if (saveToGallery) {
49-
resolve({image:imageSource.fromFile(picturePath) ,path:picturePath});
50-
} else {
51-
var options = new android.graphics.BitmapFactory.Options();
52-
options.inJustDecodeBounds = true;
53-
android.graphics.BitmapFactory.decodeFile(picturePath, options);
54-
55-
var sampleSize = calculateInSampleSize(options.outWidth, options.outHeight, reqWidth, reqHeight);
56-
57-
var finalBitmapOptions = new android.graphics.BitmapFactory.Options();
58-
finalBitmapOptions.inSampleSize = sampleSize;
59-
var bitmap = android.graphics.BitmapFactory.decodeFile(picturePath, finalBitmapOptions);
60-
var scaledSizeImage = null;
61-
if (reqHeight > 0 && reqWidth > 0) {
62-
if (shouldKeepAspectRatio) {
63-
64-
var common: typeof cameraCommonModule = require("./camera-common");
65-
66-
var aspectSafeSize = common.getAspectSafeDimensions(bitmap.getWidth(), bitmap.getHeight(), reqWidth, reqHeight);
67-
scaledSizeImage = android.graphics.Bitmap.createScaledBitmap(bitmap, aspectSafeSize.width, aspectSafeSize.height, true);
68-
}
69-
else {
70-
scaledSizeImage = android.graphics.Bitmap.createScaledBitmap(bitmap, reqWidth, reqHeight, true);
71-
}
56+
let imageSource: typeof imageSourceModule = require("image-source");
57+
let options = new android.graphics.BitmapFactory.Options();
58+
options.inJustDecodeBounds = true;
59+
android.graphics.BitmapFactory.decodeFile(picturePath, options);
60+
61+
let sampleSize = calculateInSampleSize(options.outWidth, options.outHeight, reqWidth, reqHeight);
62+
63+
let finalBitmapOptions = new android.graphics.BitmapFactory.Options();
64+
finalBitmapOptions.inSampleSize = sampleSize;
65+
let bitmap = android.graphics.BitmapFactory.decodeFile(picturePath, finalBitmapOptions);
66+
let scaledSizeImage = null;
67+
if (reqHeight > 0 && reqWidth > 0) {
68+
if (shouldKeepAspectRatio) {
69+
70+
let common: typeof cameraCommonModule = require("./camera-common");
71+
72+
let aspectSafeSize = common.getAspectSafeDimensions(bitmap.getWidth(), bitmap.getHeight(), reqWidth, reqHeight);
73+
scaledSizeImage = android.graphics.Bitmap.createScaledBitmap(bitmap, aspectSafeSize.width, aspectSafeSize.height, true);
7274
}
7375
else {
74-
scaledSizeImage = bitmap;
76+
scaledSizeImage = android.graphics.Bitmap.createScaledBitmap(bitmap, reqWidth, reqHeight, true);
7577
}
78+
}
79+
else {
80+
scaledSizeImage = bitmap;
81+
}
7682

77-
var ei = new android.media.ExifInterface(picturePath);
78-
var orientation = ei.getAttributeInt(android.media.ExifInterface.TAG_ORIENTATION, android.media.ExifInterface.ORIENTATION_NORMAL);
79-
80-
switch (orientation) {
81-
case android.media.ExifInterface.ORIENTATION_ROTATE_90:
82-
scaledSizeImage = rotateBitmap(scaledSizeImage, 90);
83-
break;
84-
case android.media.ExifInterface.ORIENTATION_ROTATE_180:
85-
scaledSizeImage = rotateBitmap(scaledSizeImage, 180);
86-
break;
87-
case android.media.ExifInterface.ORIENTATION_ROTATE_270:
88-
scaledSizeImage = rotateBitmap(scaledSizeImage, 270);
89-
break;
90-
}
91-
92-
resolve({image:imageSource.fromNativeSource(scaledSizeImage), path:picturePath});
93-
83+
let ei = new android.media.ExifInterface(picturePath);
84+
let orientation = ei.getAttributeInt(android.media.ExifInterface.TAG_ORIENTATION, android.media.ExifInterface.ORIENTATION_NORMAL);
85+
86+
switch (orientation) {
87+
case android.media.ExifInterface.ORIENTATION_ROTATE_90:
88+
scaledSizeImage = rotateBitmap(scaledSizeImage, 90);
89+
break;
90+
case android.media.ExifInterface.ORIENTATION_ROTATE_180:
91+
scaledSizeImage = rotateBitmap(scaledSizeImage, 180);
92+
break;
93+
case android.media.ExifInterface.ORIENTATION_ROTATE_270:
94+
scaledSizeImage = rotateBitmap(scaledSizeImage, 270);
95+
break;
9496
}
97+
98+
resolve(imageSource.fromNativeSource(scaledSizeImage));
9599
}
96100
};
97101

@@ -113,10 +117,10 @@ export var isAvailable = function () {
113117
}
114118

115119
var calculateInSampleSize = function (imageWidth, imageHeight, reqWidth, reqHeight) {
116-
var sampleSize = 1;
120+
let sampleSize = 1;
117121
if (imageWidth > reqWidth && imageHeight > reqHeight) {
118-
var halfWidth = imageWidth / 2;
119-
var halfHeight = imageHeight / 2;
122+
let halfWidth = imageWidth / 2;
123+
let halfHeight = imageHeight / 2;
120124
while ((halfWidth / sampleSize) > reqWidth && (halfHeight / sampleSize) > reqHeight) {
121125
sampleSize *= 2;
122126
}
@@ -125,8 +129,8 @@ var calculateInSampleSize = function (imageWidth, imageHeight, reqWidth, reqHeig
125129
}
126130

127131
var createDateTimeStamp = function () {
128-
var result = "";
129-
var date = new Date();
132+
let result = "";
133+
let date = new Date();
130134
result = (date.getDate() < 10 ? "0" + date.getDate().toString() : date.getDate().toString()) +
131135
((date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1).toString() : (date.getMonth() + 1).toString()) +
132136
date.getFullYear().toString() +
@@ -137,7 +141,7 @@ var createDateTimeStamp = function () {
137141
}
138142

139143
var rotateBitmap = function (source, angle) {
140-
var matrix = new android.graphics.Matrix();
144+
let matrix = new android.graphics.Matrix();
141145
matrix.postRotate(angle);
142146
return android.graphics.Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
143147
}

camera/camera.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,10 @@ declare module "camera" {
3636
* This property could affect width or heigth return values.
3737
*/
3838
keepAspectRatio?: boolean;
39+
40+
/**
41+
* Defines if camera picture should be copied to photo Gallery (Android) or Photos (iOS)
42+
*/
43+
saveToGallery?: boolean;
3944
}
4045
}

camera/camera.ios.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,23 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic
2727
if (options) {
2828
this._width = options.width;
2929
this._height = options.height;
30-
this._saveToGallery = options.saveToGallery;
30+
this._saveToGallery = options.saveToGallery;
3131
this._keepAspectRatio = types.isNullOrUndefined(options.keepAspectRatio) ? true : options.keepAspectRatio;
3232
}
3333
return this;
3434
}
3535

3636
imagePickerControllerDidFinishPickingMediaWithInfo(picker, info): void {
3737
if (info) {
38-
var source = info.valueForKey(UIImagePickerControllerOriginalImage);
38+
let source = info.valueForKey(UIImagePickerControllerOriginalImage);
3939
if (source) {
40-
var image = null;
40+
let image = null;
4141
if (this._width || this._height) {
42-
var newSize = null;
42+
let newSize = null;
4343
if (this._keepAspectRatio) {
44-
var common: typeof cameraCommonModule = require("./camera-common");
44+
let common: typeof cameraCommonModule = require("./camera-common");
4545

46-
var aspectSafeSize = common.getAspectSafeDimensions(source.size.width, source.size.height, this._width, this._height);
46+
let aspectSafeSize = common.getAspectSafeDimensions(source.size.width, source.size.height, this._width, this._height);
4747
newSize = CGSizeMake(aspectSafeSize.width, aspectSafeSize.height);
4848
}
4949
else {
@@ -55,15 +55,15 @@ class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePic
5555
UIGraphicsEndImageContext();
5656
}
5757

58-
var imageSource: typeof imageSourceModule = require("image-source");
58+
let imageSource: typeof imageSourceModule = require("image-source");
59+
60+
let imageSourceResult = image ? imageSource.fromNativeSource(image) : imageSource.fromNativeSource(source);
5961

60-
var imageSourceResult = image ? imageSource.fromNativeSource(image) : imageSource.fromNativeSource(source);
61-
6262
if (this._callback) {
6363
this._callback(imageSourceResult);
64-
if(this._saveToGallery) {
65-
UIImageWriteToSavedPhotosAlbum(imageSourceResult.ios, null, null, null);
66-
}
64+
if (this._saveToGallery) {
65+
UIImageWriteToSavedPhotosAlbum(imageSourceResult.ios, null, null, null);
66+
}
6767
}
6868
}
6969
}
@@ -82,29 +82,29 @@ var listener;
8282
export var takePicture = function (options): Promise<any> {
8383
return new Promise((resolve, reject) => {
8484
listener = null;
85-
var imagePickerController = new UIImagePickerController();
86-
var reqWidth = 0;
87-
var reqHeight = 0;
88-
var keepAspectRatio = true;
89-
var saveToGallery = true;
85+
let imagePickerController = new UIImagePickerController();
86+
let reqWidth = 0;
87+
let reqHeight = 0;
88+
let keepAspectRatio = true;
89+
let saveToGallery = true;
9090
if (options) {
9191
reqWidth = options.width || 0;
9292
reqHeight = options.height || reqWidth;
9393
keepAspectRatio = types.isNullOrUndefined(options.keepAspectRatio) ? true : options.keepAspectRatio;
94-
saveToGallery = options.saveToGallery ? true : false;
94+
saveToGallery = options.saveToGallery ? true : false;
9595
}
9696
if (reqWidth && reqHeight) {
9797
listener = UIImagePickerControllerDelegateImpl.new().initWithCallbackAndOptions(resolve, { width: reqWidth, height: reqHeight, keepAspectRatio: keepAspectRatio, saveToGallery: saveToGallery });
9898
} else if (saveToGallery) {
99-
listener = UIImagePickerControllerDelegateImpl.new().initWithCallbackAndOptions(resolve, { saveToGallery: saveToGallery });
100-
}
99+
listener = UIImagePickerControllerDelegateImpl.new().initWithCallbackAndOptions(resolve, { saveToGallery: saveToGallery });
100+
}
101101
else {
102102
listener = UIImagePickerControllerDelegateImpl.new().initWithCallback(resolve);
103103
}
104104
imagePickerController.delegate = listener;
105105

106-
var sourceType = UIImagePickerControllerSourceType.UIImagePickerControllerSourceTypeCamera;
107-
var mediaTypes = UIImagePickerController.availableMediaTypesForSourceType(sourceType);
106+
let sourceType = UIImagePickerControllerSourceType.UIImagePickerControllerSourceTypeCamera;
107+
let mediaTypes = UIImagePickerController.availableMediaTypesForSourceType(sourceType);
108108

109109
if (mediaTypes) {
110110
imagePickerController.mediaTypes = mediaTypes;
@@ -113,11 +113,11 @@ export var takePicture = function (options): Promise<any> {
113113

114114
imagePickerController.modalPresentationStyle = UIModalPresentationStyle.UIModalPresentationCurrentContext;
115115

116-
var frame: typeof frameModule = require("ui/frame");
116+
let frame: typeof frameModule = require("ui/frame");
117117

118-
var topMostFrame = frame.topmost();
118+
let topMostFrame = frame.topmost();
119119
if (topMostFrame) {
120-
var viewController: UIViewController = topMostFrame.currentPage && topMostFrame.currentPage.ios;
120+
let viewController: UIViewController = topMostFrame.currentPage && topMostFrame.currentPage.ios;
121121
if (viewController) {
122122
viewController.presentViewControllerAnimatedCompletion(imagePickerController, true, null);
123123
}

0 commit comments

Comments
 (0)