forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.android.ts
More file actions
147 lines (123 loc) · 7.06 KB
/
Copy pathcamera.android.ts
File metadata and controls
147 lines (123 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import * as typesModule from "utils/types";
import * as utilsModule from "utils/utils";
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?): Promise<any> {
return new Promise((resolve, reject) => {
try {
let types: typeof typesModule = require("utils/types");
let utils: typeof utilsModule = require("utils/utils");
let saveToGallery;
let reqWidth;
let reqHeight;
let shouldKeepAspectRatio;
let density = utils.layout.getDisplayDensity();
if (options) {
saveToGallery = options.saveToGallery ? true : false;
reqWidth = options.width ? options.width * density : 0;
reqHeight = options.height ? options.height * density : reqWidth;
shouldKeepAspectRatio = types.isNullOrUndefined(options.keepAspectRatio) ? true : options.keepAspectRatio;
}
let takePictureIntent = new android.content.Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
let dateStamp = createDateTimeStamp();
let picturePath: string;
let nativeFile;
let tempPictureUri;
if (saveToGallery) {
picturePath = android.os.Environment.getExternalStoragePublicDirectory(
android.os.Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/" + "cameraPicture_" + dateStamp + ".jpg";
nativeFile = new java.io.File(picturePath);
tempPictureUri = android.net.Uri.fromFile(nativeFile);
takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, tempPictureUri);
} else {
picturePath = utils.ad.getApplicationContext().getExternalFilesDir(null).getAbsolutePath() + "/" + "cameraPicture_" + dateStamp + ".jpg";
nativeFile = new java.io.File(picturePath);
tempPictureUri = android.net.Uri.fromFile(nativeFile);
takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, tempPictureUri);
}
if (takePictureIntent.resolveActivity(utils.ad.getApplicationContext().getPackageManager()) != null) {
let appModule: typeof applicationModule = require("application");
let previousResult = appModule.android.onActivityResult;
appModule.android.onActivityResult = (requestCode: number, resultCode: number, data: android.content.Intent) => {
appModule.android.onActivityResult = previousResult;
if (requestCode === REQUEST_IMAGE_CAPTURE && resultCode === android.app.Activity.RESULT_OK) {
let imageSource: typeof imageSourceModule = require("image-source");
let options = new android.graphics.BitmapFactory.Options();
options.inJustDecodeBounds = true;
android.graphics.BitmapFactory.decodeFile(picturePath, options);
let sampleSize = calculateInSampleSize(options.outWidth, options.outHeight, reqWidth, reqHeight);
let finalBitmapOptions = new android.graphics.BitmapFactory.Options();
finalBitmapOptions.inSampleSize = sampleSize;
let bitmap = android.graphics.BitmapFactory.decodeFile(picturePath, finalBitmapOptions);
let scaledSizeImage = null;
if (reqHeight > 0 && reqWidth > 0) {
if (shouldKeepAspectRatio) {
let common: typeof cameraCommonModule = require("./camera-common");
let aspectSafeSize = common.getAspectSafeDimensions(bitmap.getWidth(), bitmap.getHeight(), reqWidth, reqHeight);
scaledSizeImage = android.graphics.Bitmap.createScaledBitmap(bitmap, aspectSafeSize.width, aspectSafeSize.height, true);
}
else {
scaledSizeImage = android.graphics.Bitmap.createScaledBitmap(bitmap, reqWidth, reqHeight, true);
}
}
else {
scaledSizeImage = bitmap;
}
let ei = new android.media.ExifInterface(picturePath);
let orientation = ei.getAttributeInt(android.media.ExifInterface.TAG_ORIENTATION, android.media.ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case android.media.ExifInterface.ORIENTATION_ROTATE_90:
scaledSizeImage = rotateBitmap(scaledSizeImage, 90);
break;
case android.media.ExifInterface.ORIENTATION_ROTATE_180:
scaledSizeImage = rotateBitmap(scaledSizeImage, 180);
break;
case android.media.ExifInterface.ORIENTATION_ROTATE_270:
scaledSizeImage = rotateBitmap(scaledSizeImage, 270);
break;
}
resolve(imageSource.fromNativeSource(scaledSizeImage));
}
};
appModule.android.foregroundActivity.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
} catch (e) {
if (reject) {
reject(e);
}
}
});
}
export var isAvailable = function () {
var utils: typeof utilsModule = require("utils/utils");
return utils.ad.getApplicationContext().getPackageManager().hasSystemFeature(android.content.pm.PackageManager.FEATURE_CAMERA)
}
var calculateInSampleSize = function (imageWidth, imageHeight, reqWidth, reqHeight) {
let sampleSize = 1;
if (imageWidth > reqWidth && imageHeight > reqHeight) {
let halfWidth = imageWidth / 2;
let halfHeight = imageHeight / 2;
while ((halfWidth / sampleSize) > reqWidth && (halfHeight / sampleSize) > reqHeight) {
sampleSize *= 2;
}
}
return sampleSize;
}
var createDateTimeStamp = function () {
let result = "";
let 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();
return result;
}
var rotateBitmap = function (source, angle) {
let matrix = new android.graphics.Matrix();
matrix.postRotate(angle);
return android.graphics.Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}