-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasset.dart
166 lines (138 loc) · 4.79 KB
/
asset.dart
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:multi_image_picker/multi_image_picker.dart';
class Asset {
/// The resource identifier
String _identifier;
/// The resource file name
String _name;
/// Original image width
int _originalWidth;
/// Original image height
int _originalHeight;
Asset(
this._identifier,
this._name,
this._originalWidth,
this._originalHeight,
);
/// The BinaryChannel name this asset is listening on.
String get _channel {
return 'multi_image_picker/image/$_identifier';
}
String get _thumbChannel => '$_channel.thumb';
String get _originalChannel => '$_channel.original';
/// Returns the original image width
int get originalWidth {
return _originalWidth;
}
/// Returns the original image height
int get originalHeight {
return _originalHeight;
}
/// Returns true if the image is landscape
bool get isLandscape {
return _originalWidth > _originalHeight;
}
/// Returns true if the image is Portrait
bool get isPortrait {
return _originalWidth < _originalHeight;
}
/// Returns the image identifier
String get identifier {
return _identifier;
}
/// Returns the image name
String get name {
return _name;
}
/// Requests a thumbnail for the [Asset] with give [width] and [hegiht].
///
/// The method returns a Future with the [ByteData] for the thumb,
/// as well as storing it in the _thumbData property which can be requested
/// later again, without need to call this method again.
///
/// You can also pass the optional parameter [quality] to reduce the quality
/// and the size of the returned image if needed. The value should be between
/// 0 and 100. By default it set to 100 (max quality).
///
/// Once you don't need this thumb data it is a good practice to release it,
/// by calling releaseThumb() method.
Future<ByteData> getThumbByteData(int width, int height,
{int quality = 100}) async {
assert(width != null);
assert(height != null);
if (width != null && width < 0) {
throw new ArgumentError.value(width, 'width cannot be negative');
}
if (height != null && height < 0) {
throw new ArgumentError.value(height, 'height cannot be negative');
}
if (quality < 0 || quality > 100) {
throw new ArgumentError.value(
quality, 'quality should be in range 0-100');
}
Completer completer = new Completer<ByteData>();
ServicesBinding.instance.defaultBinaryMessenger
.setMessageHandler(_thumbChannel, (ByteData message) async {
completer.complete(message);
ServicesBinding.instance.defaultBinaryMessenger
.setMessageHandler(_thumbChannel, null);
return message;
});
await MultiImagePicker.requestThumbnail(
_identifier, width, height, quality);
return completer.future;
}
/// Requests the original image for that asset.
///
/// You can also pass the optional parameter [quality] to reduce the quality
/// and the size of the returned image if needed. The value should be between
/// 0 and 100. By default it set to 100 (max quality).
///
/// The method returns a Future with the [ByteData] for the image,
/// as well as storing it in the _imageData property which can be requested
/// later again, without need to call this method again.
Future<ByteData> getByteData({int quality = 100}) async {
if (quality < 0 || quality > 100) {
throw new ArgumentError.value(
quality, 'quality should be in range 0-100');
}
Completer completer = new Completer<ByteData>();
ServicesBinding.instance.defaultBinaryMessenger
.setMessageHandler(_originalChannel, (ByteData message) async {
completer.complete(message);
ServicesBinding.instance.defaultBinaryMessenger
.setMessageHandler(_originalChannel, null);
return message;
});
await MultiImagePicker.requestOriginal(_identifier, quality);
return completer.future;
}
/// Requests the original image meta data
Future<Metadata> get metadata {
return MultiImagePicker.requestMetadata(_identifier);
}
@Deprecated(
'This method will be deprecated in the next major release. Please use getByteData method instead.',
)
Future<ByteData> requestOriginal({int quality = 100}) {
return getByteData(quality: quality);
}
@Deprecated(
'This method will be deprecated in the next major release. Please use getThumbByteData method instead.',
)
Future<ByteData> requestThumbnail(
int width,
int height, {
int quality = 100,
}) async {
return getThumbByteData(width, height, quality: quality);
}
@Deprecated(
'This method will be deprecated in the next major release. Please use metadata getter instead.',
)
Future<Metadata> requestMetadata() {
return metadata;
}
}