Image compression takes too long on Flutter Web - flutter

I tried to compress web image using the below code (with image_compression_flutter package), however it takes about 1 minute to compress a 6 mb image.
Future<Uint8List?> compressImage(Uint8List imgBytes,
{required String path, int quality = 70}) async {
final input = ImageFile(
rawBytes: imgBytes,
filePath: path,
);
Configuration config = Configuration(
outputType: ImageOutputType.jpg,
// can only be true for Android and iOS while using ImageOutputType.jpg or ImageOutputType.pngÏ
useJpgPngNativeCompressor: false,
// set quality between 0-100
quality: quality,
);
final param = ImageFileConfiguration(input: input, config: config);
final output = await compressor.compress(param);
return output.rawBytes;
}
Is it because of web limitations it takes such a long time to compress?
Is there any workaround regarding this issue

Eventually, I found a solution for compressing web image. Image picker now has XFile type which can be directly compressed in web. It took about 2 seconds to compress a 12mb image to 50kb
final XFile? pickedImageFile = await ImagePicker().pickImage(
source: isCamera ? ImageSource.camera : ImageSource.gallery,
imageQuality: imageQuality,
maxWidth: maxWidth,
preferredCameraDevice: CameraDevice.rear);
While uploading to a server there is no need to convert XFile to any type. Just need to read bytes from pickedImageFile
await pickedImageFile.readAsBytes();

Related

How can I compress images from API without saving it in file system in Dart/Flutter?

Is it possible to compress without saving in files images from API to resize image?
I found one of packages which is doing this job - flutter_native_image: ^0.0.6 but all of these packages works onle if I am going to pick uup images from file storage and after that to reduce size to dowload it to my app.
So can I reduce image weight? Now it is 2MB images which comes from API.
I was trying this thing:
if (hubConnection.state == HubConnectionState.Connected) {
await hubConnection.invoke('GetImage', args: [carId]).then((value) {
carImage = value as String;
});
var compressed = await FlutterNativeImage.compressImage(carImage ?? '',
quality: 50, percentage: 30);
print(compressed);
print('is it compressed?');
}
But it gives me an error: '[ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: PlatformException(file does not exist, iVBORw0KGgoAAAANSUhEUgAAB4AAAAQ4CAIAAABnsVYUAAAgAElEQVR4AXzBi65tWZqd1e/rY659TkRmpatsGXFHyI+BQOJJAPHkIHGxJVOUMzMizl5z/J2xduQ+kVUUtOaP/+N/90V3+x+4mA0XRxdHw1F'
I understood it says about my carImage variable is not File type, but Base64 type, so if there is any chance to make image size smaller without reaching out backend devs?

Flutter Unit8List Resize Photo

I want to change the size of the photos I take with flutter unit8list as http extension, for example, the size of the photo is 1280x800, but I want to adjust its size manually. Is this possible?
String imgurl = "http://.....jpg";
Uint8List bytes = (await NetworkAssetBundle(Uri.parse(imgurl),)
.load(imgurl))
.buffer
.asUint8List();

flutter image picker reduce file size with provider and flutter_image_compress

I am using image picker to pick image from device(App) and after that stored that file in provider class like that.
File? _img;
get img=> _img;
void putbannerimg(File img) {
_img = img;
notifyListeners();
}
I found out that image picker does not compress png images, I tried compressing it with flutter_image_compress
compressFile() async {
final formservice = Provider.of<PostForm>(context, listen: false);
File file = formservice.bannerfile;
final result = await FlutterImageCompress.compressWithFile(
file.absolute.path,
quality: 54,
);
formservice.putbannerimg(File.fromRawPath(result!));
}
I tried this way And other multiple ways but getting different different errors I want to upload this file in firebase storage like this
var task = storageicon.putFile(formservice.iconfile);
please tell me where I am going wrong, without compress file all things are working fine
Edit: I found out that path should be a string how can I parse local code file in that
If you are using the "flutter_image_compress" package
You may have to use the compress function for files stored on the phone like this:
Future<File> testCompressAndGetFile(File file, String targetPath) async {
var result = await FlutterImageCompress.compressAndGetFile(
file.absolute.path, targetPath,
quality: 88,
rotate: 180,
);
print(file.lengthSync());
print(result.lengthSync());
return result;
}
"compressAndGetFile()" returns a file while "compressWithFile()" returns a Uint8List.
If i understand your last question right, you want to use an image built into your application by default.
For this, you have to open your "pubsepc.yaml", go to the "flutter:" mark and add "assets:" like so:
flutter:
assets:
- path
"path", in my example, describes a top level folder containing assets like pictures.
You can freely describe its name.

Flutter imagepicker.pickvideo return jpg

I'm using this to return the video file but I got .jpg
Future<File> getVideo() async {
var video = await ImagePicker.pickVideo(
source: ImageSource.gallery);
return video;
}
I want to ImagePicker.pickVideo() return video file instead of .jpg file so I can upload this file to firebase, how can I achieve that?
I'm assuming that you're using the package:
https://pub.dev/packages/image_picker
pickVideo() method has been decrecated, and you will need to replace these apis with getVideo()
As explained the repositories' documentation:
https://github.com/flutter/plugins/tree/master/packages/image_picker/image_picker
Write this:
final _picker = ImagePicker();
PickedFile video = await _picker.getVideo(...)
However I would suggest to use this package as an alternative:
https://pub.dev/packages/flutter_document_picker
This package will allow you to select all videos on the device, including those taken from a users' Google Drive or iCloud providers. In this case write this:
FlutterDocumentPickerParams params = FlutterDocumentPickerParams(
allowedUtiTypes: [
'public.video',
'public.mpeg',
'public.mpeg-4-audio',
'com.apple.protected-​mpeg-4-audio'
],
allowedMimeTypes: [
'video/mpeg',
'video/x-flv',
'video/mp4',
'application/x-mpegURL',
'video/quicktime',
'video/x-msvideo',
'video/x-ms-wmv',
'video/ogg',
'video/mp2t',
'video/3gpp'
],
invalidFileNameSymbols: ['/'],
);
return await FlutterDocumentPicker.openDocument(params: params);
You will need to make sure that the Mimes and Uti types for videos on iOS & Android are set correctly.

How I can convert an image from gallery or camera to binary string?

I used image_picker to choose an image from gallery or take a photo with camera and now I have to send it on request to backend as an array of bytes. My issue is that I don't know How I can encode the image. Can anyone tell me what I should use and how?
var image = await ImagePicker.pickImage(source: ImageSource.camera);
File imageFile=image ;
List<int> imageBytes = imageFile.readAsBytesSync();
String base64Image = base64UrlEncode(imageBytes);