Printing Image with Bluetooth Printer in Flutter - flutter

I want to print the screenshot image of first page in Flutter. So far I have come up to the part where I need to parse that image into function that would print it, here is the code:
FIRST PAGE
_screenshot() {
screenshotController.capture().then((File image) {
//Capture Done
setState(() {
_screenshot = image;
});
print('Successful Screenshot => $_screenshot');
Navigator.push(
context, MaterialPageRoute(builder: (context) => Print(_screenshot)));}
So this is the page that screenshots that whole page and pushes that screenshot to the second screen. Here is the second screen:
SECOND PAGE
class Print extends StatefulWidget {
final File screenshot;
Print(this.screenshot);
#override
_PrintState createState() => _PrintState();
}
Here I initialize that image from the first page, and then I created the function for printing that should print the ticket, but I am getting an error:
Future<Ticket> _ticket(PaperSize paper) async {
final ticket = Ticket(paper);
//this is a working option, but I don't want to load from assets, but load screenshot file from first page
//final ByteData data = await rootBundle.load(assets/store.png');
final File img = await rootBundle.load(widget.screenshot); //here parse the image from first page **ERROR HERE**
final Uint8List bytes = data.buffer.asUint8List();
final Image image = decodeImage(bytes);
ticket.image(image);
return ticket;
}
I am getting an error that Byte data cannot be assigned to variable type File which I understand, but how would I go around making this work? I am using the esc_pos_bluetooth: ^0.2.8 library for bluetooth printing.

You don't need to load data from rootBundle instead, you can use readAsBytes() class method from File class which returns Uint8List.
Uint8List bytes = await widget.screenshot.readAsBytes();
Your final code should look like.
Future<Ticket> _ticket(PaperSize paper) async {
final ticket = Ticket(paper);
final Uint8List bytes = await widget.screenshot.readAsBytes();
final Image image = decodeImage(bytes);
ticket.image(image);
return ticket;
}

Related

In Flutter, how to extract frame by frame from an animated gif?

I'm trying to extract frames from an animated gif using Flutter. By researching the Flutter API, I thought the following code should work but it only gives me the first frame although it gives me the correct frames count.
static Future<List<ui.Image>> loadAnimatedGif(String assetPath) async {
List<ui.Image> gifImage = [];
ByteData data = await rootBundle.load(assetPath);
var codec = await ui.instantiateImageCodec(data.buffer.asUint8List());
int frmCnt = codec.frameCount;
print("frmcnt is $frmCnt"); //it gives correct frames count
for (int i = 0; i < frmCnt; i++) {
var frame = await codec.getNextFrame();
gifImage.add(frame.Image);
}
return gifImage;
}
I made a simple app out of this and it works using the same code but added a converter in it.
What I notice at first because nothing
Using the ui.Image loads the frames but it delays for about 10secs before showing all of them using a breakpoint to debug it.
Converting ui.Image to Uint8List takes time as much as the frames but the format shows all frames as soon the extraction is done.
Here is the dart pad to quickly run and test it.
Here is the code if you will like to scan through it, I am loading the gif from Network.
class GifFramesDisplay extends StatefulWidget {
const GifFramesDisplay({super.key});
#override
State<GifFramesDisplay> createState() => _GifFramesDisplayState();
}
class _GifFramesDisplayState extends State<GifFramesDisplay> {
List<Uint8List> _frames = [];
#override
void initState() {
super.initState();
loadGif();
}
Future<void> loadGif() async {
// Load the gif image from the network url and store the bytes in a ByteData object
final url = Uri.parse(
'https://raw.githubusercontent.com/Aman-Malhotra/animate_icons/main/demo/animate_icons.gif');
final ByteData data = await NetworkAssetBundle(url).load(url.path);
// Using the _extractGifFrames function to extract the frames
_extractGifFrames(data).then((List<Uint8List> frames) {
// Set the state to update the UI
setState(() {
_frames = frames;
});
});
}
// Function to extract gif image frames
Future<List<Uint8List>> _extractGifFrames(ByteData data) async {
// Create a list to store the frames
final List<Uint8List> frames = <Uint8List>[];
// Create a codec to decode the gif
final ui.Codec codec =
await ui.instantiateImageCodec(data.buffer.asUint8List());
// Count the number of frames in the gif
final int frameCount = codec.frameCount;
print('Total frameCount: $frameCount');
// Loop through the frames and add them to the list
for (int i = 0; i < frameCount; i++) {
// Get the next frame
final ui.FrameInfo fi = await codec.getNextFrame();
// Add the frame to the list
final frame = await loadImage(fi.image);
// Add the frame to the list if it is not null
if (frame != null) {
frames.add(frame);
}
}
return frames;
}
Future<Uint8List?> loadImage(ui.Image image) async {
final ByteData? byteData =
await image.toByteData(format: ui.ImageByteFormat.png);
return byteData?.buffer.asUint8List();
}
#override
Widget build(BuildContext context) {
return Center(
child: _frames.isEmpty
? const CircularProgressIndicator()
: ListView.builder(
itemCount: _frames.length,
itemBuilder: (BuildContext context, int index) {
return Image.memory(_frames[index]);
},
),
);
}
}
Click to watch a preview of the loaded gif frames of the code above

flutter get pdf files string data to variable

I want to take a pdf files string data in a fast way and the package which I use is giving some problems.
How can I get correctly and fast way a pdf file?
Thanks for your help.
this is the way ı using
PDFDoc? _pdfDoc;
String _text = "";
_getPdfFile() {
setState(() async {
_pdfDoc = await PDFDoc.fromPath(filePath!);
data = await _pdfDoc!.text;
});
}
this is the source of code. this code will take string data from the pdf page 3.
Instead, if you want all the data in pdf you can look at this link.https://help.syncfusion.com/flutter/pdf/working-with-text-extraction
final String data;
final PdfDocument document =
PdfDocument(inputBytes: myFile.readAsBytesSync());
setState(() {
data = PdfTextExtractor(document).extractText(startPageIndex: 3);
});
document.dispose();

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!

flutter image_web_picker Object? instead of Image returned

I copied this from the official image web picker files but I get the message that getImage return an Object? instead of an Image. Where is the problem? And I importet the package if you think that is the problem.
class _LoadPhotoState extends State<LoadPhoto> {
late final Image _pickedImage;
Future<void> _pickImage() async {
Image fromPicker = await ImagePickerWeb.getImage(outputType: ImageType.widget);
setState(() {
_pickedImage = fromPicker;
});
}
}
It's not your fault, the README is wrong: the API reference of that package says the getImage method returns a Future<Object?>. However, the solution is easy:
Image fromPicker = (await ImagePickerWeb.getImage(outputType: ImageType.widget)) as Image;

share_plus does not share the text flutter

I am trying to implement sharing widget as a screenshot via share_plus.dart plug in. It works, however it does not attach the string text to the widget. How do I fix that?
Future<void> shareReview() async {
final image = await controller.capture();
saveAndShare(image);
}
void saveAndShare(Uint8List bytes) async {
final directory = await getApplicationDocumentsDirectory();
final image = File('${directory.path}/review.png');
image.writeAsBytesSync(bytes);
final text = 'blahblahblahbalh';
await Share.shareFiles([image.path], text: text);
}