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

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)

Related

Convert base64 to pdf in flutter and open(without saving the file itself) in a modal window

I have a problem, I can't figure out how to convert. I have an entity (for a block). In the entity, I have a variable where I store a pdf file in base64 format. I want to convert a string (from base 64) into a PDF and display this PDF in a modal window. If someone has done this, I will be grateful for help.
My goal is to convert base 64, which is in pdfBase, into a file, in order to display this pdf file in the widget
I just want to say the file (but it is not necessary to save it).
My entity
import 'dart:convert';
import 'dart:typed_data';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'warranty_pdf_entity.freezed.dart';
#freezed
class WarrantyPdfEntity with _$WarrantyPdfEntity {
const WarrantyPdfEntity._();
const factory WarrantyPdfEntity({
#Default('') String? pdfBase,
final dynamic filePdf,
}) = _WarrantyPdfEntity;
PdfImage copyWithSelected() {
var pdfDataBytes = base64.decode(pdfgarranty!);
var img = decodeImage(pdfDataBytes);
PdfImage image = PdfImage(
pdf,
image: img!.data.buffer.asUint8List(),
width: img.width,
height: img.height);
return image;
}
}
I think this question is a dublicate. Here is the answer about how to convert base64 to pdf in flutter: https://stackoverflow.com/a/66997078/15776812

How can one convert an XFile image from any format to a JPG image for upload?

Right now I'm able to chose an image as XFile using MultiImage picker as follows:
final List<XFile>? images = await _picker.pickMultiImage(maxWidth: _maxWidth, maxHeight:_maxHeight);
which then gives me a list of images which I am able to send into my upload service for uploading to my server
for (var image in images)
{
await myUploadService(image); //image is an XFile
}
Now what I need to do is convert the images, no matter what format they are in, into a JPG image before the upload
So I have tried to convert the images, using the image Flutter package (https://github.com/brendan-duncan/image) as follows:
import 'package:image/image.dart' as ImageConvert;
for (var image in images)
{
//Convert all images to JPG
final newImage = ImageConvert.decodeImage(File(image.path).readAsBytesSync())!;
var newImg = ImageConvert.encodeJpg(newImage);
var newImg2 = Image.memory(Uint8List.fromList(newImg)) as XFile; // <--- the main problem
await myUploadService(newImg2);
}
But this isn't working, mainly at the point where I try to put the image back into an XFile format for upload with my upload service
So the main question is here is how can I convert the newly encoded JPG image back to XFile format?
Open to totally different strategies of dealing with this issue as well

Dart: How to Encode Decode animated webp files?

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!

How to display an image from AttachedPicture | Flutter

I'm trying to display a cover image from an MP3 file. I'm getting the ID3 tags from https://github.com/NiKoTron/dart-tags.
My Code:
TagProcessor tp = TagProcessor();
tp.getTagsFromByteArray(bytes).then((l) async {
AttachedPicture picture = l[1].tags['picture']['Cover (front)'];
log(picture.toString()); // ->[log] {mime:image/png, description:, bitmap: iVBORw0KGgoAAAANSUhEUgAABLAAAASw...}
});
The mime: image/png is just a string, so I don't exactly know how to get the image.
AttachedPicture has property imageData which is of type List<int>.
You could use Image.memory to display imageData by using Uint8List.fromList.
import 'dart:typed_data';
Image.memory(
Uint8List.fromList(imageData),
);

Flutter: Convert Base64 String image url to File and use it in FileImage or related widgets

I've tried this way, and use it in FileImage but haven't found a rally point
Uint8List bytes = base64.decode(imageBase64);
File file = File.fromRawPath(bytes);
imageBase64 is the image url that has been encode.
The point of this thing is, i want to use it in FileImage or another widget image file but i don't want this image is saved to device and upload this file to the attachments file Rest API
can anyone help me.
If what you want to do once you have the bytes is to show the image, then you can use Image.memory.
var imageWidget = Image.memory(bytes);
OTOH If you're trying to upload the image, you don't need to convert it into file. You can use bytes directly assuming you can't upload it using base64 enconding. Package http
import 'package:http/http.dart';
...
//create multipart using filepath, string or bytes
MultipartFile multipartFile = MultipartFile.fromBytes(
'file',
byteData,
filename: fileName,
);
//add multipart to request
request.files.add(multipartFile);
var response = await request.send();
If you have an UintList8 you can use MemoryImage(bytes).
Uint8List bytes = base64.decode(imageBase64);
Image(image: MemoryImage(bytes));