Convert memory image to base64 in flutter - flutter

trying to create pdf file using pdf package then convert it to an image and then convert the image to base64 to save it in a QR code. this should be a simple task for URL images and gallery images. But in my case, the image is generated in the flutter from pdf doc as a memory image. my code is
await for (var page in Printing.raster(await doc.save(), pages: [0, 1], dpi: 72)) {final reciptimage = page.toImage();}
tried the below code but it doesn't work
final bytes = await Io.File(reciptimage).readAsBytes();
I couldn't find any way to generate base64 from the memory image reciptimage.
Any help?

may this eample can help you.
RenderRepaintBoundary boundary = _globalKey.currentContext.findRenderObject();
ui.Image image = await boundary.toImage(pixelRatio: 3.0);
ByteData byteData =
await image.toByteData(format: ui.ImageByteFormat.png);
Uint8List pngBytes = byteData.buffer.asUint8List();
String bs64 = base64Encode(pngBytes);

Related

I cannot Print Images with blue_thermal_printer (Print Random Characters) Flutter

what's a problem with my code?
var response = await http.get(Uri.parse(
"https://thumbs.dreamstime.com/b/jpg-file-icon-logo-element-illustration-design-155443757.jpg"));
Uint8List bytesNetwork = response.bodyBytes;
Uint8List imageBytesFromNetwork = bytesNetwork.buffer.asUint8List();
await bluetooth.printImageBytes(imageBytesFromNetwork);
Try lowering the pixels of your image, I had a similar problem.

Flutter Unit8List Resize Photo

I want to change the size of the photos I take with flutter unit8list as http extension, for example, the size of the photo is 1280x800, but I want to adjust its size manually. Is this possible?
String imgurl = "http://.....jpg";
Uint8List bytes = (await NetworkAssetBundle(Uri.parse(imgurl),)
.load(imgurl))
.buffer
.asUint8List();

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.

How I can convert an image from gallery or camera to binary string?

I used image_picker to choose an image from gallery or take a photo with camera and now I have to send it on request to backend as an array of bytes. My issue is that I don't know How I can encode the image. Can anyone tell me what I should use and how?
var image = await ImagePicker.pickImage(source: ImageSource.camera);
File imageFile=image ;
List<int> imageBytes = imageFile.readAsBytesSync();
String base64Image = base64UrlEncode(imageBytes);

Flutter Print Qr Code Image in Flutter to Thermal Printer

I try to print out qr code to thermal printer by using flutter_bluetooth_serial.
I generate the qr code by using following guide
https://medium.com/flutter-community/building-flutter-qr-code-generator-scanner-and-sharing-app-703e73b228d3
I manage to convert the image into Uint8List and send to the printer.
Future<Uint8List> _getQrByte() async {
RenderRepaintBoundary boundary =
globalKey.currentContext.findRenderObject();
var image = await boundary.toImage();
var byteData = await image.toByteData();
return byteData.buffer.asUint8List();
}
and I call function in flutter_bluetooth_serial
await _bluetooth.writeBytes(bytes);
I expect to print a perfect qr code, but the printout is random char and very long.
In android, I manage to print out by sending byte array from bitmap class to the printer
Have you tried esc_pos_bluetooth for a Bluetooth printer (or esc_pos_printer for a WiFi/network printer)? Both packages can print QR codes
You can use this:
final profile = await CapabilityProfile.load(name: 'XP-N160I');
final generator = Generator(PaperSize.mm80, profile);
bytes += generator.qrcode(
'https://github.com/BrunoSantosCosta',
size: const QRSize(30),
);