How to display an image from AttachedPicture | Flutter - 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),
);

Related

Flutter - How to convert Image to List<Int>?

I want to make the following code work and I do not understand image conversion properly. Would really like someone to point me the right direction to learn this please along with providing a solution to my following problem.
I display the image as a container background image using:
MemoryImage(base64Decode(image)) // My image is in base64 String format here
The above works fine.
Now I have an image path: example-
'/data/user/0/....../cache/scaled_image_picker2751194612758734223.jpg'
How do I go from getting image from this path to base64Encode(image)?
I tried the following up to now and have run out of ideas here:
final Image? image = await Image.file(File(_imagePath));
if (image != null){
setState(() => imageDisplayInContainer = base64Encode(image)); <----Doesn't work
}
Get the file object:
File imageFile = File(_imagePath);
Get the bytes:
List<int> imageBytes = imageFile.readAsBytesSync();
Give the bytes the MemoryImage
MemoryImage(imageBytes);

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

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));

Getting FirebaseVisionImage from manipulated image in Flutter

I'm trying to do some pre-processing in my image before using Firebase MLkit. Previously, I was simply loading my image and then passing it directly like this.
File file = await FilePicker.getFile(type: FileType.IMAGE);
final FirebaseVisionImage visionImage = FirebaseVisionImage.fromFile(file);
VisionText visionText = await textRecognizer.processImage(visionImage);
But now, I wanted to do some processing on the image. I did this using Flutter's Image package like this.
import 'package:image/image.dart' as Img;
File file = await FilePicker.getFile(type: FileType.IMAGE);
Img.Image image = Img.decodeImage(file.readAsBytesSync());
var img = Img.invert(image);
Now, how do I pass this new object to FirebaseVisionImage. I see that there is this function:
FirebaseVisionImage.fromBytes(bytes, metadata)
I'm guessing I can get bytes from the above image object using img.getBytes() but I'm confused how to set the metadata.
EDIT:
I see that one can write the image to a temporary file doing
img.writeAsBytesSync(decoded.ToList());
and then use that image again using FirebaseVisionImage.fromFile() but I would really prefer not to do that.