How can I get a Uint8List image size? - flutter

I want to find out the size of an Uint8List image before I upload it to firebase storage using
StorageUploadTask uploadTask = reference.putData(uint8ListImage);
wherein uint8ListImage is a Uint8List. Is there a way to find out the size of the Uint8List?

try this,
print(uint8ListImage.lengthInBytes);
this will print the size in bytes

Related

How do decode base64 video and play the video using flutter?

am recording the video using camera plugin and saved the video in internal storage path then i have encoded video using base64.Now i need to decode the encoded value and play the decoded value in flutter application.If any one can help me. thanks in advance.
_fileConvert(String path) {
File file = File(path);
List<int> fileInByte = file.readAsBytesSync();
String fileInBase64 = base64Encode(fileInByte);
print("fileInBase64............");
print(fileInBase64);
}

How to get `Uint8List` from `ImmutableBuffer`?

The new Flutter 3.3 deprecates the method load of class ImageProvider and now some my code became not working because load had a parameter bytes of type Uit8List but loadBuffer has a parameter buffer of ImmutableBuffer.
I used Uint8List to decode image using image package
final bytes = img.decodeImage(bytes); // bytes is instance of Uit8List
The complete code can be read from the article Remove a color from an image in Flutter starting from section 4. Decode.
And now I need to convert ImmutableBuffer to Uint8List. Does someone know how to do this?

Flutter camera package, how can I rotate the image?

My code is simle;
final XFile image=await cameraController!.takePicture();
takenImage=await image.readAsBytes();
and when I am just using this two line, I can see the image with Image.memory(_vm.takenImage!, fit: BoxFit.fitWidth)
But i need to change picture rotation,
for this reason, I am using image package https://pub.dev/packages/image
so, why I am getting invalid image data after just these lines;
final XFile image=await cameraController!.takePicture();
takenImage=await image.readAsBytes();
img.Image? decodedImage=img.decodeImage(takenImage!);
takenImage=decodedImage!.getBytes();
If I delete the last two sentence it's working correctlye but if I add the last lines I am getting Exceltion: invalid image data, but why ? I mean .getBytes returning Uint8List, takenImage type is Uint8List? ...
if I can convert like my second code review, I can use the; img.copyRotate(originalImage!, 90).getBytes(); function, so please help me for understand.
For my problem related image type cause as you can see on question I wasn't encode my image with any type, so the solution and working code now can seen on below;
final XFile image=await cameraController!.takePicture();
img.Image? _capturedImage=img.decodeImage(await image.readAsBytes());
_capturedImage=img.copyRotate(_capturedImage!, 90);
takenImage=Uint8List.fromList(img.encodePng(_capturedImage));

Reduce size image from bytes Uint8List

I am using the file_picker package to import images before uploading them to Firebase Storage, because it allows me to choose the formats what is not possible with image_picker.
I would like to be able to reduce the size of these images to reduce their weight. The flutter_image_compress package allows to compress images but it is not available for Flutter Web.
How can I reduce the size of an image imported in Flutter Web knowing that the format is Uint8List?
You can use the archive package to compress any data. The following is an example function taking Uint8List and compressing it:
Uint8List compress(Uint8List input) {
final encoder = ZLibEncoder();
return encoder.encode(input) as Uint8List;
}
You'll want to do this in a separate isolate since this could noticeably block your UI, but this won't make any difference if your application is web-only.
if you know the image type you can convert it to jpeg and then compress it using the flutter image package.
import 'package:image/image.dart' as img;
Future<List<int>> _pngToJpg(Uint8List pngBytes) async {
final pngImage = img.decodePng(pngBytes);
final pngImageResized = img.copyResize(pngImage!, width: 800);
return img.encodeJpg(pngImageResized, quality: 90);
}
you can use any other decoder depending on your image type. Expect a freeze in UI. You can use compute to minimize it.

How to Save ByteData (Image) into Download directory in Flutter?

I'm trying to save a ByteData image generated from canvas into the device download folder. However, I was only able to save it into my apps android/data/ directory using the code below...
Future<void> saveImage(
String fileName,
ByteData image,
) async {
final directoryName = "Generated";
Directory directory = await getExternalStorageDirectory();
String path = directory.path;
await Directory('$path/$directoryName').create(recursive: true);
File('$path/$directoryName/$fileName.png').writeAsBytesSync(image.buffer.asInt8List());
}
but, it's not convenient for the user to go into this deep directory just to get the image, so I want to save it into the Download directory but I don't know how. I have tried several approaches but I keep on receiving permission errors. Like for example using this code below (downloads_path_provider)...
Directory directory = await DownloadsPathProvider.downloadsDirectory;
String path = directory.path;
File('$path/$fileName.png').writeAsBytesSync(image.buffer.asInt8List());
PS. It would be much better if there's a way to save it somewhere that will show in the Phone's Gallery
You can save images to gallery with this package on pub.dev : 'gallery_saver' : https://pub.dev/packages/gallery_saver