image_picker_ohos.dart
11 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'src/messages.g.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:image_picker_platform_interface/image_picker_platform_interface.dart';
class ImagePickerOhos extends ImagePickerPlatform {
ImagePickerOhos({@visibleForTesting ImagePickerApi? api})
: _hostApi = api ?? ImagePickerApi();
final ImagePickerApi _hostApi;
bool useOhosPhotoPicker = false;
static void registerWith() {
ImagePickerPlatform.instance = ImagePickerOhos();
}
@override
Future<PickedFile?> pickImage({
required ImageSource source,
double? maxWidth,
double? maxHeight,
int? imageQuality,
CameraDevice preferredCameraDevice = CameraDevice.rear,
}) async {
final String? path = await _getImagePath(
source: source,
maxWidth: maxWidth,
maxHeight: maxHeight,
imageQuality: imageQuality,
preferredCameraDevice: preferredCameraDevice,
);
return path != null ? PickedFile(path) : null;
}
@override
Future<List<PickedFile>?> pickMultiImage({
double? maxWidth,
double? maxHeight,
int? imageQuality,
}) async {
final List<dynamic> paths = await _getMultiImagePath(
maxWidth: maxWidth,
maxHeight: maxHeight,
imageQuality: imageQuality,
);
if (paths.isEmpty) {
return null;
}
return paths.map((dynamic path) => PickedFile(path as String)).toList();
}
Future<List<dynamic>> _getMultiImagePath({
double? maxWidth,
double? maxHeight,
int? imageQuality,
int? limit,
}) {
if (imageQuality != null && (imageQuality < 0 || imageQuality > 100)) {
throw ArgumentError.value(
imageQuality, 'imageQuality', 'must be between 0 and 100');
}
if (maxWidth != null && maxWidth < 0) {
throw ArgumentError.value(maxWidth, 'maxWidth', 'cannot be negative');
}
if (maxHeight != null && maxHeight < 0) {
throw ArgumentError.value(maxHeight, 'maxHeight', 'cannot be negative');
}
if (limit != null && limit < 2) {
throw ArgumentError.value(limit, 'limit', 'cannot be lower than 2');
}
return _hostApi.pickImages(
SourceSpecification(type: SourceType.gallery),
ImageSelectionOptions(
maxWidth: maxWidth,
maxHeight: maxHeight,
quality: imageQuality ?? 100),
GeneralOptions(
allowMultiple: true,
usePhotoPicker: useOhosPhotoPicker,
limit: limit,
),
);
}
Future<String?> _getImagePath({
required ImageSource source,
double? maxWidth,
double? maxHeight,
int? imageQuality,
CameraDevice preferredCameraDevice = CameraDevice.rear,
bool requestFullMetadata = true,
}) async {
if (imageQuality != null && (imageQuality < 0 || imageQuality > 100)) {
throw ArgumentError.value(
imageQuality, 'imageQuality', 'must be between 0 and 100');
}
if (maxWidth != null && maxWidth < 0) {
throw ArgumentError.value(maxWidth, 'maxWidth', 'cannot be negative');
}
if (maxHeight != null && maxHeight < 0) {
throw ArgumentError.value(maxHeight, 'maxHeight', 'cannot be negative');
}
final List<String?> paths = await _hostApi.pickImages(
_buildSourceSpec(source, preferredCameraDevice),
ImageSelectionOptions(
maxWidth: maxWidth,
maxHeight: maxHeight,
quality: imageQuality ?? 100),
GeneralOptions(
allowMultiple: false,
usePhotoPicker: useOhosPhotoPicker,
),
);
return paths.isEmpty ? null : paths.first;
}
@override
Future<PickedFile?> pickVideo({
required ImageSource source,
CameraDevice preferredCameraDevice = CameraDevice.rear,
Duration? maxDuration,
}) async {
final String? path = await _getVideoPath(
source: source,
maxDuration: maxDuration,
preferredCameraDevice: preferredCameraDevice,
);
return path != null ? PickedFile(path) : null;
}
Future<String?> _getVideoPath({
required ImageSource source,
CameraDevice preferredCameraDevice = CameraDevice.rear,
Duration? maxDuration,
}) async {
final List<String?> paths = await _hostApi.pickVideos(
_buildSourceSpec(source, preferredCameraDevice),
VideoSelectionOptions(maxDurationSeconds: maxDuration?.inSeconds),
GeneralOptions(
allowMultiple: false,
usePhotoPicker: useOhosPhotoPicker,
),
);
return paths.isEmpty ? null : paths.first;
}
@override
Future<XFile?> getImage({
required ImageSource source,
double? maxWidth,
double? maxHeight,
int? imageQuality,
CameraDevice preferredCameraDevice = CameraDevice.rear,
}) async {
final String? path = await _getImagePath(
source: source,
maxWidth: maxWidth,
maxHeight: maxHeight,
imageQuality: imageQuality,
preferredCameraDevice: preferredCameraDevice,
);
return path != null ? XFile(path) : null;
}
@override
Future<XFile?> getImageFromSource({
required ImageSource source,
ImagePickerOptions options = const ImagePickerOptions(),
}) async {
final String? path = await _getImagePath(
source: source,
maxHeight: options.maxHeight,
maxWidth: options.maxWidth,
imageQuality: options.imageQuality,
preferredCameraDevice: options.preferredCameraDevice,
requestFullMetadata: options.requestFullMetadata,
);
return path != null ? XFile(path) : null;
}
@override
Future<List<XFile>?> getMultiImage({
double? maxWidth,
double? maxHeight,
int? imageQuality,
}) async {
final List<dynamic> paths = await _getMultiImagePath(
maxWidth: maxWidth,
maxHeight: maxHeight,
imageQuality: imageQuality,
);
if (paths.isEmpty) {
return null;
}
return paths.map((dynamic path) => XFile(path as String)).toList();
}
@override
Future<List<XFile>> getMultiImageWithOptions({
MultiImagePickerOptions options = const MultiImagePickerOptions(),
}) async {
final List<dynamic> paths = await _getMultiImagePath(
maxWidth: options.imageOptions.maxWidth,
maxHeight: options.imageOptions.maxHeight,
imageQuality: options.imageOptions.imageQuality,
limit: options.limit,
);
if (paths.isEmpty) {
return <XFile>[];
}
return paths.map((dynamic path) => XFile(path as String)).toList();
}
@override
Future<List<XFile>> getMedia({
required MediaOptions options,
}) async {
return (await _hostApi.pickMedia(
_mediaOptionsToMediaSelectionOptions(options),
_mediaOptionsToGeneralOptions(options),
))
.map((String? path) => XFile(path!))
.toList();
}
@override
Future<XFile?> getVideo({
required ImageSource source,
CameraDevice preferredCameraDevice = CameraDevice.rear,
Duration? maxDuration,
}) async {
final String? path = await _getVideoPath(
source: source,
maxDuration: maxDuration,
preferredCameraDevice: preferredCameraDevice,
);
return path != null ? XFile(path) : null;
}
MediaSelectionOptions _mediaOptionsToMediaSelectionOptions(
MediaOptions mediaOptions) {
final ImageSelectionOptions imageSelectionOptions =
_imageOptionsToImageSelectionOptionsWithValidator(
mediaOptions.imageOptions);
return MediaSelectionOptions(
imageSelectionOptions: imageSelectionOptions,
);
}
ImageSelectionOptions _imageOptionsToImageSelectionOptionsWithValidator(
ImageOptions? imageOptions) {
final double? maxHeight = imageOptions?.maxHeight;
final double? maxWidth = imageOptions?.maxWidth;
final int? imageQuality = imageOptions?.imageQuality;
if (imageQuality != null && (imageQuality < 0 || imageQuality > 100)) {
throw ArgumentError.value(
imageQuality, 'imageQuality', 'must be between 0 and 100');
}
if (maxWidth != null && maxWidth < 0) {
throw ArgumentError.value(maxWidth, 'maxWidth', 'cannot be negative');
}
if (maxHeight != null && maxHeight < 0) {
throw ArgumentError.value(maxHeight, 'maxHeight', 'cannot be negative');
}
return ImageSelectionOptions(
quality: imageQuality ?? 100, maxHeight: maxHeight, maxWidth: maxWidth);
}
GeneralOptions _mediaOptionsToGeneralOptions(MediaOptions options) {
final bool allowMultiple = options.allowMultiple;
final int? limit = options.limit;
if (!allowMultiple && limit != null) {
throw ArgumentError.value(
allowMultiple,
'allowMultiple',
'cannot be false, when limit is not null',
);
}
if (limit != null && limit < 2) {
throw ArgumentError.value(limit, 'limit', 'cannot be lower then 2');
}
return GeneralOptions(
allowMultiple: allowMultiple,
usePhotoPicker: useOhosPhotoPicker,
limit: limit,
);
}
@override
Future<LostData> retrieveLostData() async {
final LostDataResponse result = await getLostData();
if (result.isEmpty) {
return LostData.empty();
}
return LostData(
file: result.file != null ? PickedFile(result.file!.path) : null,
exception: result.exception,
type: result.type,
);
}
@override
Future<LostDataResponse> getLostData() async {
final CacheRetrievalResult? result = await _hostApi.retrieveLostResults();
if (result == null) {
return LostDataResponse.empty();
}
assert(result.paths.isEmpty != (result.error == null));
final CacheRetrievalError? error = result.error;
final PlatformException? exception = error == null
? null
: PlatformException(code: error.code, message: error.message);
final List<XFile> pickedFileList =
result.paths.map((String? path) => XFile(path!)).toList();
return LostDataResponse(
file: pickedFileList.isEmpty ? null : pickedFileList.last,
exception: exception,
type: _retrieveTypeForCacheType(result.type),
files: pickedFileList,
);
}
SourceSpecification _buildSourceSpec(
ImageSource source, CameraDevice device) {
return SourceSpecification(
type: _sourceSpecTypeForSource(source),
camera: _sourceSpecCameraForDevice(device));
}
SourceType _sourceSpecTypeForSource(ImageSource source) {
switch (source) {
case ImageSource.camera:
return SourceType.camera;
case ImageSource.gallery:
return SourceType.gallery;
}
return SourceType.gallery;
}
SourceCamera _sourceSpecCameraForDevice(CameraDevice device) {
switch (device) {
case CameraDevice.front:
return SourceCamera.front;
case CameraDevice.rear:
return SourceCamera.rear;
}
return SourceCamera.rear;
}
RetrieveType _retrieveTypeForCacheType(CacheRetrievalType type) {
switch (type) {
case CacheRetrievalType.image:
return RetrieveType.image;
case CacheRetrievalType.video:
return RetrieveType.video;
}
return RetrieveType.image;
}
}