Skip to content

Commit

Permalink
Merge pull request #30 from Ahmadre/fix/29
Browse files Browse the repository at this point in the history
Fix/29
  • Loading branch information
TesteurManiak authored Dec 14, 2021
2 parents e53bc11 + 208f421 commit 08c9f5a
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 112 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ doc/api/
*.js_
*.js.deps
*.js.map

example/ios/Runner/

example/ios/Flutter/

example/android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java

example/android/local.properties
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.1.0 - [14/12/2021]

* Deprecated `getImage`, `getMultiImages` and `getVideo` methods.
* Added methods `getImageAsBytes`, `getImageAsWidget`, `getImageAsFile`, `getMultiImagesAsBytes`, `getMultiImagesAsWidget`, `getMultiImagesAsFile`, `getVideoAsFile` and `getVideoAsBytes` ([#29](https://github.com/Ahmadre/image_picker_web/issues/29))

## 2.0.3+1 - [27/06/2021]

* Fixed a typo in the `README.md`
Expand Down
74 changes: 19 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,82 +15,52 @@ This Web-Plugin allows Flutter Web to pick images (as File, Widget or Uint8List)

## Getting Started

Add ` ` ` image_picker_web ` ` ` to your pubspec.yaml:
Add `image_picker_web` to your pubspec.yaml:

```yaml
image_picker_web: any
image_picker_web: any
```
## Picking Images
### Pick an image
Load Image as Image Widget:
Load image as `Image.memory` Widget:

```dart
Image fromPicker = await ImagePickerWeb.getImage(outputType: ImageType.widget);

if (fromPicker != null) {
setState(() {
pickedImage = fromPicker;
});
}
Image? fromPicker = await ImagePickerWeb.getImageAsWidget();
```

Setting ` ` ` outputType ` ` ` to ` ` ` ImageType.bytes ` ` ` :
Load image as bytes:

```dart
Uint8List bytesFromPicker =
await ImagePickerWeb.getImage(outputType: ImageType.bytes);
if (bytesFromPicker != null) {
debugPrint(bytesFromPicker.toString());
}
Uint8List? bytesFromPicker = await ImagePickerWeb.getImageAsBytes();
```

Setting ` ` ` outputType ` ` ` to ` ` ` ImageType.file ` ` ` :
Load image as and `html.File` object :

```dart
html.File imageFile =
await ImagePickerWeb.getImage(outputType: ImageType.file);
if (imageFile != null) {
debugPrint(imageFile.name.toString());
}
html.File? imageFile = await ImagePickerWeb.getMultiImagesAsFile();
```

### Pick multiple images

Load Images as Image Widgets:
Load images as a `List<Image>` :

```dart
List<Image> fromPicker = await ImagePickerWeb.getMultiImages(outputType: ImageType.widget);
if (fromPicker != null) {
setState(() => pickedImages = fromPicker);
}
List<Image>? fromPicker = await ImagePickerWeb.getMultiImagesAsWidget();
```

Setting ` ` ` outputType ` ` ` to ` ` ` ImageType.bytes ` ` ` :
Load images as bytes :

```dart
List<Uint8List> bytesFromPicker =
await ImagePickerWeb.getMultiImages(outputType: ImageType.bytes);
if (bytesFromPicker != null) {
bytesFromPicker.forEach((bytes) => debugPrint(bytes.toString()));
}
List<Uint8List>? bytesFromPicker = await ImagePickerWeb.getMultiImagesAsBytes();
```

Setting ` ` ` outputType ` ` ` to ` ` ` ImageType.file ` ` ` :
Load images as `List<html.File>` objects :

```dart
List<html.File> imageFiles =
await ImagePickerWeb.getMultiImages(outputType: ImageType.file);
if (imageFiles != null) {
imageFiles.forEach((image) => debugPrint(image.name.toString()));
}
List<html.File> imageFiles = await ImagePickerWeb.getMultiImagesAsFile();
```

## How do I get all Informations out of my Image/Video (e.g. Image AND File in one run)?
Expand Down Expand Up @@ -133,25 +103,19 @@ With `getMultipleImageInfos()` you can get all three available types in one call

## Picking Videos

To load a video as html. File do:
To load a video as `html.File` do:

```dart
html.File videoFile = await ImagePickerWeb.getVideo(outputType: VideoType.file);
debugPrint('---Picked Video File---');
debugPrint(videoFile.name.toString());
html.File? videoFile = await ImagePickerWeb.getVideoAsFile();
```

To load a video as Uint8List do:
To load a video as `Uint8List` do:

```dart
Uint8List videoData = await ImagePickerWeb.getVideo(outputType: VideoType.bytes);
debugPrint('---Picked Video Bytes---');
debugPrint(videoData.toString());
Uint8List? videoData = await ImagePickerWeb.getVideoAsBytes();
```

Reminder: Don't use Uint8List retreivement for big video files! Flutter can't handle that. Pick bigger sized videos and high-resolution videos as html. File!
**Reminder**: Don't use `Uint8List` retreivement for big video files! Flutter can't handle that. Pick bigger sized videos and high-resolution videos as `html.File` !

After you uploaded your video somewhere hosted, you can retreive the network url to play it.

Expand Down
3 changes: 1 addition & 2 deletions example/lib/photoHistory_add_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ class _ImagePickerWidgetState extends State<ImagePickerWidget> {
_pageStatus = PageStatus.LOADING;
});

final image =
(await ImagePickerWeb.getImage(outputType: ImageType.widget)) as Image;
final image = await ImagePickerWeb.getImageAsWidget();
print(image);

if (image != null) {
Expand Down
13 changes: 4 additions & 9 deletions example/lib/sample_page.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import 'package:image_picker_web/image_picker_web.dart';
import 'dart:html' as html;

class SamplePage extends StatefulWidget {
@override
Expand All @@ -14,9 +13,7 @@ class _SamplePageState extends State<SamplePage> {
String _imageInfo = '';

Future<void> _pickImage() async {
Image fromPicker =
await ImagePickerWeb.getImage(outputType: ImageType.widget);

final fromPicker = await ImagePickerWeb.getImageAsWidget();
if (fromPicker != null) {
setState(() {
_pickedImages.clear();
Expand All @@ -26,8 +23,7 @@ class _SamplePageState extends State<SamplePage> {
}

Future<void> _pickVideo() async {
final videoMetaData =
await ImagePickerWeb.getVideo(outputType: VideoType.bytes);
final videoMetaData = await ImagePickerWeb.getVideoAsBytes();
if (videoMetaData != null) {
setState(() {
_pickedVideos.clear();
Expand All @@ -37,16 +33,15 @@ class _SamplePageState extends State<SamplePage> {
}

Future<void> _pickMultiImages() async {
List<Image> images =
await ImagePickerWeb.getMultiImages(outputType: ImageType.widget);
final images = await ImagePickerWeb.getMultiImagesAsWidget();
setState(() {
_pickedImages.clear();
if (images != null) _pickedImages.addAll(images);
});
}

Future<void> _getImgFile() async {
html.File infos = await ImagePickerWeb.getImage(outputType: ImageType.file);
final infos = await ImagePickerWeb.getImageAsFile();
setState(() => _imageInfo =
'Name: ${infos.name}\nRelative Path: ${infos.relativePath}');
}
Expand Down
138 changes: 93 additions & 45 deletions lib/image_picker_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -150,27 +150,46 @@ class ImagePickerWeb {
/// - [ImageType.widget] return an [Image.memory] using the image's bytes.
///
/// ```dart
/// html.File imgFile = await getImage(ImageType.file);
/// Uint8List imgBytes = await getImage(ImageType.bytes);
/// Image imgWidget = await getImage(ImageType.widget);
/// html.File? imgFile = await getImage(ImageType.file);
/// Uint8List? imgBytes = await getImage(ImageType.bytes);
/// Image? imgWidget = await getImage(ImageType.widget);
/// ```
@Deprecated(
'Use [getImageAsBytes], [getImageAsWidget] or [getImageAsFile] instead.',
)
static Future<Object?> getImage({required ImageType outputType}) async {
if (!(outputType is ImageType)) {
throw ArgumentError(
'outputType has to be from Type: ImageType if you call getImage()');
}
final file = await ImagePickerWeb._pickFile('image');
if (file == null) return null;
switch (outputType) {
case ImageType.file:
return file;
return getImageAsFile();
case ImageType.bytes:
return file.asBytes();
return getImageAsBytes();
case ImageType.widget:
return Image.memory(await file.asBytes(), semanticLabel: file.name);
return getImageAsWidget();
}
}

/// Picker that close after selecting 1 image and return a [Uint8List] of the
/// selected image.
static Future<Uint8List?> getImageAsBytes() async {
final file = await ImagePickerWeb._pickFile('image');
return file?.asBytes();
}

/// Picker that close after selecting 1 image and return an [Image.memory]
/// using the image's bytes.
static Future<Image?> getImageAsWidget() async {
final file = await ImagePickerWeb._pickFile('image');
return file != null
? Image.memory(await file.asBytes(), semanticLabel: file.name)
: null;
}

/// Picker that close after selecting 1 image and return a [html.File] of the
/// selected image.
static Future<html.File?> getImageAsFile() {
return ImagePickerWeb._pickFile('image');
}

/// Help to retrieve further image's informations about your picked source.
///
/// Return an object [MediaInfo] containing image's informations.
Expand All @@ -180,49 +199,65 @@ class ImagePickerWeb {
return MediaInfo.fromJson(data!);
}

// Picker allow multi-image selection. Here are the different instance
/// Picker that allows multi-image selection. Here are the different instance
/// of Future returned depending on [outputType] :
///
/// - [ImageType.file] return a [html.File] list of the selected images.
///
/// - [ImageType.bytes] return a [Uint8List] list of the selected images.
///
/// - [ImageType.widget] return an [Image.memory] list using the images'
/// bytes.
///
/// ```dart
/// List<html.File> imgFiles = await getMultiImages(ImageType.file);
/// List<Uint8List> imgBytes = await getMultiImages(ImageType.bytes);
/// List<Image> imgWidgets = await getMultiImages(ImageType.widget);
/// List<html.File>? imgFiles = await getMultiImages(ImageType.file);
/// List<Uint8List>? imgBytes = await getMultiImages(ImageType.bytes);
/// List<Image>? imgWidgets = await getMultiImages(ImageType.widget);
/// ```
@Deprecated(
'Use [getMultiImagesAsBytes], [getMultiImagesAsWidget] or '
'[getMultiImagesAsFile] instead.',
)
static Future<List?> getMultiImages({required ImageType outputType}) async {
if (!(outputType is ImageType)) {
throw ArgumentError(
'outputType has to be from Type: ImageType if you call getImage()');
}
final images = await ImagePickerWeb()._pickMultiFiles('image');
if (images == null) return null;
switch (outputType) {
case ImageType.file:
return images;
return getMultiImagesAsFile();
case ImageType.bytes:
var files = <Uint8List>[];
for (final img in images) {
files.add(await img.asBytes());
}
return files.isEmpty ? null : files;
return getMultiImagesAsBytes();
case ImageType.widget:
var files = <Uint8List>[];
for (final img in images) {
files.add(await img.asBytes());
}
if (files.isEmpty) return null;
return files.map<Image>((e) => Image.memory(e)).toList();
default:
return null;
return getMultiImagesAsWidget();
}
}

/// Picker that allows multi-image selection and return a [Uint8List] list of
/// the selected images.
static Future<List<Uint8List>?> getMultiImagesAsBytes() async {
final images = await ImagePickerWeb()._pickMultiFiles('image');
if (images == null) return null;
var files = <Uint8List>[];
for (final img in images) {
files.add(await img.asBytes());
}
return files.isEmpty ? null : files;
}

/// Picker that allows multi-image selection and return an [Image.memory] list
/// using the images' bytes.
static Future<List<Image>?> getMultiImagesAsWidget() async {
final images = await ImagePickerWeb()._pickMultiFiles('image');
if (images == null) return null;
var files = <Uint8List>[];
for (final img in images) {
files.add(await img.asBytes());
}
if (files.isEmpty) return null;
return files.map<Image>((e) => Image.memory(e)).toList();
}

/// Picker that allows multi-image selection and return a [html.File] list of
/// the selected images.
static Future<List<html.File>?> getMultiImagesAsFile() {
return ImagePickerWeb()._pickMultiFiles('image');
}

/// Picker that close after selecting 1 video. Here are the different instance
/// of Future returned depending on [outputType] :
///
Expand All @@ -234,20 +269,33 @@ class ImagePickerWeb {
/// html.File videoFile = await getVideo(VideoType.file);
/// Uint8List videoBytes = await getVideo(VideoType.bytes);
/// ```
@Deprecated(
'Use [getVideoAsBytes] or [getVideoAsFile] instead.',
)
static Future<dynamic> getVideo({required VideoType outputType}) async {
switch (outputType) {
case VideoType.file:
return ImagePickerWeb._pickFile('video');
return getImageAsFile();
case VideoType.bytes:
final data =
await _methodChannel.invokeMapMethod<String, dynamic>('pickVideo');
final imageData = base64.decode(data!['data']);
return imageData;
default:
return null;
return getVideoAsBytes();
}
}

/// Picker that close after selecting 1 video and return a [Uint8List] of the
/// selected video.
static Future<Uint8List?> getVideoAsBytes() async {
final data =
await _methodChannel.invokeMapMethod<String, dynamic>('pickVideo');
final imageData = base64.decode(data!['data']);
return imageData;
}

/// Picker that close after selecting 1 video and return a [html.File] of the
/// selected video.
static Future<html.File?> getVideoAsFile() {
return ImagePickerWeb._pickFile('video');
}

/// Help to retrieve further video's informations about your picked source.
///
/// Return an object [MediaInfo] containing video's informations.
Expand Down
Loading

0 comments on commit 08c9f5a

Please sign in to comment.