I cannot Print Images with blue_thermal_printer (Print Random Characters) Flutter - 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.

Related

Convert memory image to base64 in 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);

Flutter Audio file to text

How can I get the sound I recorded in a file in flutter as a string(text) every word of it?
as an example, he will say hello world in the audio file.How can I get this as a string
String getText = "hello world";
i know about google's speech-to-text product, but it seems too expensive, isn't there another way for me to do it?
Try this package
google_speech: ^2.0.1
To convert audio to text use the code below
Future<List<int>> _getAudioContent(String name) async {
final directory = await getApplicationDocumentsDirectory();
final path = directory.path + '/$name';
return File(path).readAsBytesSync().toList();
}
final audio = await _getAudioContent('test.wav');
final response = await speechToText.recognize(config, audio);
print(response);

How to Overwrite or Update Cache files in Flutter while using Path Provider

Below Code i have created for local caching using path provider
String fileName="pathString.json";
var dir=await getTemporaryDirectory();
File file=File(dir.path+"/"+fileName);
if(!file.existsSync()) {
final http.Response response = await http.get(
Uri.parse("https://*************.herokuapp.com/*********"));
file.writeAsStringSync(
response.body, flush: true, mode: FileMode.write);
final data = file.readAsStringSync();
responseData = json.decode(data);
}else{
final data = file.readAsStringSync();
responseData = json.decode(data);
final http.Response response = await http.get(
Uri.parse("https://**************.herokuapp.com/*********"));
file.writeAsStringSync(
response.body, flush: true, mode: FileMode.write);
}
for first time, File can be Created. But For Second time.. Once File is created, and API fetches latest response with updated data, Cache file not get overwritten.
It would be great, if anyone can help on this..
Thanks in Advance.
i suppose that in your case happening something similar, to my situation:
For flutter Image class i found that, it use ImageCache for caching images, so when you rewrite Image and save it in filesystem, image will look the same until you restart app or invalidate cache.
Code that i wrote for cache invalidation for image:
import 'dart:io';
import 'package:flutter/material.dart';
// imgBytes - raw image bytes here empty just for example
// location - path to image in fs
updateImage(Uint8List imgBytes) async {
var file = File(location);
await file.writeAsBytes(imgBytes, mode: FileMode.write); // rewrite file content
var img = Image.file(file); // create Image class
img.image.evict(); // invalidate cache key for this image
}

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