Dart: How to Encode Decode animated webp files? - flutter

I am trying to decode animated webp image file and encode it's frames to animated webp image file in Dart (cli)
Dart Code:
import 'dart:io';
import 'package:image/image.dart';
void main() {
final image = 'asset/test.webp';
File bytes = File(image);
List<int> list = bytes.readAsBytesSync();
var anim = decodeWebPAnimation(list)!;
var obj = WebPEncoder();
for (var frame in anim) {
obj.addFrame(frame, duration: 1);
}
var finished = obj.finish();
print(finished); // [] empty, no frames get added
// final encodedAnim = obj.encodeAnimation();
File('asset/output.webp').writeAsBytesSync(finished!,
flush: true); // pass something like encodeWebp(input)
}
pubspec.yaml
dependencies:
image: ^3.1.3
input file: https://raw.githubusercontent.com/WhatsApp/stickers/main/Android/app/src/main/assets/2/07_OK.webp
Output:
There is zero frame after calling finish(), so the output image file is invalid. i.e. file size: 0 byte
Ref of Dart APIs:
WebpEncoder:
https://pub.dev/documentation/image/latest/image/WebPEncoder-class.html
DecodeWebpAnimation: https://pub.dev/documentation/image/latest/image/decodeWebPAnimation.html
What went wrong? How to fix this?
Thank You!

Related

Flutter : JPEG image is not loading from decoded base64string (Image.memory)

I'm getting the UserPic from API in base64 format. (So, I do not encode the image to base64.) Then I convert it to Image using base64Decode.
String normalized = base64.normalize(encodedBase64);
return Image.memory(base64Decode(normalized),height: 275, width: 255);
But I am getting the following error: EncodingError: Failed to decode frame at index 0.
After doing some research, I saw that the problem was related to the renderer. Using html renderer solved the problem but it also broke the display of other widgets. That's why I want to render with canvaskit by default. How can I solve the problem?
Encode Image Path to base64 String:
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
String imagepath = " /data/user/img.jpg";
//image path, you can get it with image_picker package
File imagefile = File(imagepath); //convert Path to File
Uint8List imagebytes = await imagefile.readAsBytes(); //convert to bytes
String base64string = base64.encode(imagebytes); //convert bytes to base64 string
print(base64string);
decode base64 Image to Bytes:
import 'dart:convert';
import 'dart:typed_data';
Uint8List decodedbytes = base64.decode(base64string); //decode base64 stirng to bytes
Image.memory(decodedbytes,height: 275, width: 255)

How do i open srt file on flutter?

I am trying to load subtitle to a video using the flutter video player package it works good for short files but it stopped as the file get bigger
I trayed subtitle_wrapper package but it has many bugs
Future<ClosedCaptionFile> getSubtitle(String url) async {
final data = NetworkAssetBundle(Uri(path: url));
final newdata = await data.load(url);
String fileContents = getStringFromBytes(newdata);
return captionFile = SubRipCaptionFile(fileContents);
}
this is getStringFromBytes function
getStringFromBytes(ByteData data) { final buffer = data.buffer;
var list = buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
return utf8.decode(list); }
it wasn't the size after all I tested on some srt files that have a blank space for some duration and the flutter converter do a check on every element if the length is<3 it break on get out of the loop

FileSystemException: Cannot open file, path = 'assets/image/download.png' (OS Error: No such file or directory, errno = 2))

I to run the zxing package example and i get this error.
Below is my example code
import 'dart:io';
import 'package:image/image.dart' as img;
import 'package:zxing2/qrcode.dart';
getQRtext() {
var image = img.decodePng(File('assets/image/download.png').readAsBytesSync())!;
LuminanceSource source = RGBLuminanceSource(image.width, image.height,
image.getBytes(format: img.Format.abgr).buffer.asInt32List());
var bitmap = BinaryBitmap(HybridBinarizer(source));
var reader = QRCodeReader();
var result = reader.decode(bitmap);
return result.text;
}
and this my image store file.
can anyone share me the solution. thank you
If you just want bytedata from asset image file,
you can use 'rootBundle'.
You need to confirm whether assets/image folder is added in pubspec.yaml.
import 'package:flutter/services.dart' show rootBundle;
ByteData bytes = await rootBundle.load('assets/image/download.png');

Converting Image FlutterWebImagePicker Output to File

I'm using Flutter web for a webapp and having trouble converting an image from the image picker to a file in order to upload it to my server. I display the image in Image.file(xxx) but I get the error:
Error while trying to load an asset: FormatException: Illegal scheme
character (at character 6)
Image(image:%20MemoryImage(Uint8List%234267a,%20scale:%201),%20frameBuilder...
Here is the code I'm trying:
Future getImage(bool isCamera) async {
Image image;
if (isCamera) {
image = await FlutterWebImagePicker.getImage;
} else {
}
var bytes = await rootBundle.load('$image');
String tempPath = (await getTemporaryDirectory()).path;
File file = File('$tempPath/profile.png');
await file.writeAsBytes(
bytes.buffer.asUint8List(bytes.offsetInBytes, bytes.lengthInBytes));
setState(() {
currentSelfie = file;
_accDetails['customer_selfie'] = currentSelfie;
});
}
Thanks in advance
I've tested this package and was very happy with the result imagePickerWebit returns 3 different types it can be in the form of Image(widget for preview), byte, File(upload)
then you can use this to get the values
html.File _cloudFile;
var _fileBytes;
Image _imageWidget;
Future<void> getMultipleImageInfos() async {
var mediaData = await ImagePickerWeb.getImageInfo;
String mimeType = mime(Path.basename(mediaData.fileName));
html.File mediaFile =
new html.File(mediaData.data, mediaData.fileName, {'type': mimeType});
if (mediaFile != null) {
setState(() {
_cloudFile = mediaFile;
_fileBytes = mediaData.data;
_imageWidget = Image.memory(mediaData.data);
});
}
I havent used the plugin although your code has 2 issues. One is the if statement and the second one is using Rootbundle. If you are picking from the filesystem, my assumption isCamera would be false. You have not added any logic for the falsy condition.
if (isCamera) {// This would be true if the source was camera
image = await FlutterWebImagePicker.getImage;
} else {
}
Additionally,
var bytes = await rootBundle.load('$image');
From the flutter documentation, rootbundle contains the resources that were packaged with the application when it was built. Those are assets that you define in your pubspec. yaml. You are selecting an image at runtime hence its not bundled as an asset.
Since the package appears to return an image object, use the toByteData method on the image i.e
image = await FlutterWebImagePicker.getImage;
await image.toByteData();//This method has some parameters. Look into them

Flutter Image format into byte fomat

How to get image in byte format using flutter on_click of button and also how to stored image in gallery on any devices please help out on this.
onPressed: () {
isVideo = false;
_onImageButtonPressed(ImageSource.camera);
}
This should get the bytes:
onImageButtonPressed(Future<File> futureFile) {
var file = await futureFile;
var bytes = file.readAsBytesSync();
}