-
Notifications
You must be signed in to change notification settings - Fork 88
/
image.js
executable file
·133 lines (101 loc) · 2.98 KB
/
image.js
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
var logicalDensityFactor;
exports.crop = crop;
exports.dpToPixels = dpToPixels;
exports.select = select;
function crop(blob, options, height) {
if (typeof options !== 'object') {
options = {
width: options,
height: height
}
}
if (!blob.width || !blob.width) {
return blob;
}
// https://jira.appcelerator.org/browse/TIMOB-4865
if (options.fix !== false) {
blob = blob.imageAsResized(blob.width, blob.height);
}
if (options.hires !== false) {
options = dpToPixels(options);
}
if (options.width && options.height) {
var blob_ratio = blob.width / blob.height;
var ratio = options.width / options.height;
if (blob_ratio !== ratio) {
// Cut left and right
if (blob_ratio > ratio) {
blob = blob.imageAsCropped({
width: Math.round(blob.height * ratio),
});
}
// Cut top and bottom
else {
blob = blob.imageAsCropped({
height: Math.round(blob.width / ratio)
});
}
}
if (blob.width !== options.width || blob.height !== options.height) {
blob = blob.imageAsResized(options.width, options.height);
}
return blob
} else {
return blob.imageAsCropped(options);
}
}
function dpToPixels(dimension) {
if (!logicalDensityFactor) {
logicalDensityFactor = Ti.Platform.displayCaps.logicalDensityFactor;
}
if (typeof dimension === 'number') {
return dimension * logicalDensityFactor;
}
if (dimension.width) {
dimension.width = dimension.width * logicalDensityFactor;
}
if (dimension.height) {
dimension.height = dimension.height * logicalDensityFactor;
}
return dimension;
}
function select(opts, callback) {
if (arguments.length === 1) {
callback = opts;
opts = {};
}
if (!Ti.Media.isCameraSupported) {
return _select('openPhotoGallery', opts, callback);
}
var dialog = Ti.UI.createOptionDialog({
options: [L('image_select_camera'), L('image_select_gallery'), L('cancel')],
cancel: 2
});
dialog.addEventListener('click', function (e) {
if (e.index === 0) {
_select('showCamera', opts, _callback);
} else if (e.index === 1) {
_select('openPhotoGallery', opts, _callback);
}
});
dialog.show();
}
function _select(fn, opts, callback) {
if (arguments.length === 2 && _.isFunction(opts)) {
callback = opts;
opts = {};
}
var defaults = {
mediaTypes: [Ti.Media.MEDIA_TYPE_PHOTO]
};
if (callback) {
defaults.success = function (e) {
callback(null, e.media);
};
defaults.error = function (e) {
callback(e.error);
};
}
opts = _.defaults(opts || {}, defaults);
Ti.Media[fn](opts);
}