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);
Related
Actually I'm very new in Flutter-Dart.
And I'm writing an web app. So I should pick images from file path in web. And I should convert the image to base64 code. because I will add the image to my PHP server with my php codes. PHP code is ready. but I don't know a lot of information about pick images and convert base64. Can anybody help me.
Add file_picker: ^4.3.0 to pubspec.yaml and try this:
void pickFile(){
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
);
if (result != null) {
Uint8List? file = result.files.first.bytes!;
final x = base64Encode(file);
//do what you want with x.
}
}
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
I am using this https://github.com/brendan-duncan/image/wiki to try and draw text on my image my question is how do I get the image needed for the first parameter in Image.drawString() when the image I am trying to pass is the one that is captured by the camera plugin?
final path = join(
// Store the picture in the temp directory.
// Find the temp directory using the `path_provider` plugin.
(await getTemporaryDirectory()).path,
'${DateTime.now()}.png',
);
// Attempt to take a picture and log where it's been saved.
await _controller.takePicture(path);
//this doesnt work
img.Image image = Image.file(File(path))
img.drawString(image, img.arial_24, 50, 50, "Hello World");
I am getting this error:
'Image (where Image is defined in
myflutterpath/packages/flutter/lib/src/widgets/image.dart)' can't be
assigned to a variable of type 'Image (where Image is defined in
/myflutterpath/flutter/.pub-cache/hosted/pub.dartlang.org/image-2.1.4/lib/src/image.dart)'
You're assigning an Image widget from Flutter to a img.Image variable from the image package. These are completely separate data types.
To do what you want, you need to create an img.Image object. Since your image source is a PNG file(guessing based on file extension), you'll need to decode it to raw pixel data. This can be done with the PngDecoder that's already included in the image package that you have.
img.Image image = img.PngDecoder().decodeImage(await File(path).readAsBytes());
This creates an img.Image from your PNG file instead of an Image widget.
How can I convert a PDF file into an image with Flutter?
I want to print the image to an ESC/POS printer using esc_pos_printer. This package won't accept PDFImage, it needs to be a Flutter Image.
I see plenty of PHP plugins that do this but nothing for Flutter.
edit: There is an answer to another question here which shows some code to decode an image from "pdf64" but I can't figure out exactly what "pdf64" is.
I created a PDF from html using flutter_html_to_pdflike this:
Directory appDocDir = await getApplicationDocumentsDirectory();
var targetPath = appDocDir.path;
var generatedPdfFile = await FlutterHtmlToPdf.convertFromHtmlContent(
htmlContent, targetPath, targetFileName);
generatedPdfFilePath = generatedPdfFile.path;
Now I need to know how to create a Flutter Image from that PDF or the bytecode to send raw to the printer.
You can use https://pub.dev/packages/printing:
await for (var page in Printing.raster(document)) {
final image = page.asImage();
...
}
This plugin can also convert your Html to Pdf with this:
final pdf = await Printing.convertHtml(
format: PdfPageFormat.a4,
html: '<html><body><p>Hello!</p></body></html>',
));
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.