Error on printing our Images on Receipt via POS system - flutter

Here is my code below.
Future<List<int>> getImage() async {
List<int> bytes = [];
CapabilityProfile profile = await CapabilityProfile.load();
final generator = Generator(PaperSize.mm80, profile);
final ByteData data = await rootBundle.load('assets/logo.png');
final buffer = data.buffer;
final image = base64.encode(Uint8List.view(buffer));
bytes += generator.image(image);
return bytes;
}
At
bytes += generator.image(image);
the error said that
Error: The argument type 'String' can't be assigned to the parameter type 'Image'.

from The docs
you need to pass the bytes not the base64
it looks like you miss decodeImage()
Future<List<int>> getImage() async {
List<int> bytes = [];
CapabilityProfile profile = await CapabilityProfile.load();
final generator = Generator(PaperSize.mm80, profile);
final ByteData data = await rootBundle.load('assets/logo.png');
final Uint8List buffer = data.buffer.asUint8List();
final Image image = decodeImage(buffer);
bytes += generator.image(image);
return bytes;
}

Related

How to convert Image Data type from imageUrl

I need to convert this watermark(from assets) image and imageurl(getting from API) to Image data type, because ui.drawImage takes only Image data types
ui.Image? watermarkImage = ui.decodeImage('assets/images/watermark.png');// getting error
ui.Image? lookbookImage = ui.decodeImage(imageurl); // getting error
final originalImage = imgData;
ui.Image image = ui.Image(160, 50);
ui.drawImage(image, watermarkImage);
First you need to get the image bytes
// network
final response = await http.Client().get(Uri.parse(imageurl));
final bytes = response.bodyBytes;
//assets
final byteData = await rootBundle.load('assets/images/watermark.png');
final bytes = byteData.buffer.asUint8List();
Then you can decode ui.Image from the image bytes
final image = await decodeImageFromList(bytes);

Save ImagePicker Image in Shared Preferences - Flutter

I'm trying to save image picked from ImagePicker and store it in shared preferences and then retrieve it from there but no luck so far
To make my question more specific, how to save an image as a string in shared preference and then later retrieve it
Here is my code
File? profileImage;
void saveData(String key, String value) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(key, value);
}
void getData(String key) async {
final prefs = await SharedPreferences.getInstance();
final image = await prefs.getString(key);
setState(() {
profileImage = image; //this would result into error because profileImage expect file type value
});
}
Future pickProfile() async {
final profileImagePicker = await ImagePicker().pickImage(source: ImageSource.gallery);
final File profile = File(profileImagePicker!.path);
final directoryPath = await getApplicationDocumentsDirectory();
final path = directoryPath.path;
final imageFile = await File(profileImagePicker.path).copy('$path/image1.png'); // What am I supposed to do after this step
saveData('profile', path); what value needs to be stored here, it expects a string
setState(() {
profileImage = profile;
});
}
To convert image into String you can use below code
final bytes = imageFile.readAsBytesSync();
String imageString = base64Encode(bytes);
To convert String to Image
Uint8List bytes = BASE64.decode(base64ImageString);
You can use the Image widget to diplay the Image
Image.memory(bytes);

Print QR code using esc_pos_printer flutter

I'm using esc_pos_printer package which can print a receipt over network. I need two features
Save a qr/bar code in the gallery
Print said qr/bar code using the thermal printer/regular printer
For saving the qr code I did:
static Future<File> _saveBarCode(GlobalKey key, String productId) async {
print("save bar code");
RenderRepaintBoundary boundary =
key.currentContext!.findRenderObject() as RenderRepaintBoundary;
ui.Image image = await boundary.toImage();
ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
Uint8List pngBytes = byteData!.buffer.asUint8List();
final tempPath = (await getTemporaryDirectory()).path;
final path = tempPath + "/" + productId + ".png";
File imgFile = File(path);
print(imgFile.path);
return imgFile.writeAsBytes(pngBytes);
}
and
static void save(GlobalKey key, String productId) async {
_saveBarCode(key, productId).then((value) async {
bool? saved = await GallerySaver.saveImage(value.path);
print("saved: $saved");
}).catchError((error) {
print(error);
});
}
But the printing part is giving me trouble:
void printOverNetwork(GlobalKey key, String productId) async {
const PaperSize paperSize = PaperSize.mm80;
final profile = await CapabilityProfile.load();
final printer = NetworkPrinter(paperSize, profile);
final PosPrintResult result =
await printer.connect('192.168.0.123', port: 9100);
_saveBarCode(key, productId).then((value) {
if (result == PosPrintResult.success) {
// print the qr/barcode
}
});
}
How can I solve the issue?

Save Image From ImagePicker Locally as a Memory(cache)

I want to save an Image from ImagePicker as a Memory but error Occured . Can you Please help me with this function and if another function needed to load image please Mentioned it below.
Uint8List? memoryImage;
Future getImage() async {
final picker = ImagePicker();
final image = await picker.getImage(source: ImageSource.camera);
if (image == null) return;
final Directory directory = await getApplicationDocumentsDirectory();
final path=directory.path;
final Filename=basename(image.path);
File file = File('$directory/$Filename.jpg');
final bytes = await file.readAsBytes();
final byte1= file.writeAsBytes(bytes);
setState(() {
memoryImage = byte1 as Uint8List?;
});
}
With this line you can write image bytes as a file.
File imageFile = await File(fileSavePath).writeAsBytes(imageBytes);
To access the Uint8List from the file you need to use
Uint8List memoryImage = File(imagePath).readAsBytesSync();
Or
Uint8List memoryImage = await File(imagePath).readAsBytes();
here the problem in your code is you are assigning file to a Uint8List. That's the error I guess

Is there anyway can I solve the images issues printing out on pos system via flutter?

I'm able to print out my images on receipt paper, but the problem I faced is that the image is line by line which is a problem.
Below is my code
final ByteData data = await rootBundle.load('assets/logo1.png');
final Uint8List buffer= data.buffer.asUint8List();
final image = decodeImage(buffer);
bytes += generator.image(image);
Here is the result of printing it out.
Use this function:
initSavetoPath() async {
//read and write
//image max 300px X 300px
final filename = 'logo_a.png';
var bytes = await rootBundle.load("assets/images/logo_a.png");
String dir = (await getApplicationDocumentsDirectory()).path;
writeToFile(bytes, '$dir/$filename');
setState(() {
pathImage = '$dir/$filename';
});
}
Check out this package: Click here!