Flutter convert ByteData to File - flutter

I'm using this template/package to draw in Flutter https://pub.dev/packages/scribble/example.
Now I want to convert the drawn image to a File. So that I can use it in my tflite model. To predict the image the runModelOnImage (tflite package) function requires a path to the image.
The used scribble package, provides the saveImage function to return the drawn image as byteData:
// saveImage
Future<void> _saveImage(BuildContext context) async {
final image = await notifier.renderImage();
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text("Your Image"),
content: Image.memory(image.buffer.asUint8List()),
),
);
}
How can I transform the byteData image to a File so I can use it in my model?

ByteData is an abstraction for:
A fixed-length, random-access sequence of bytes that also provides random and unaligned access to the fixed-width integers and floating point numbers represented by those bytes.
As Gunter mentioned in the comments, you can use File.writeAsBytes. It does require a bit of API work to get from ByteData to a List, however.
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
Future<void> writeToFile(ByteData data, String path) {
final buffer = data.buffer;
return new File(path).writeAsBytes(
buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));
}
You can read more about this on here

Related

captureFromWidget returns black image in screenshot library

I'm trying to capture QrImage using screenshot package specifically .captureFromWidget method, I'm not building the widget QrImage, because I don't want to show it just print it out using the sumni_printer package.
I tried to show the QR before printing when it was an image, and it showed flawsly as it is.
here's the code:
Future<Uint8List> getQRBytes(String data) async {
QrImage qr = QrImage(
data: data,
size: 100,
);
ScreenshotController screenshotController = ScreenshotController();
Screenshot(
controller: screenshotController,
child: qr,
);
Uint8List bytes = await screenshotController.captureFromWidget(qr);
return bytes;
}
and the results when printed in sumni printer:
this is how I import the Uint8List when printing:
Uint8List qr = await getQRBytes(data);
await SunmiPrinter.printImage(qr);
here's how it looks in the app
It turns out it's the sunmi_printer package...
there's a new package, sunmi_printer_plus
it solved my issue and it's an amazing package

Flutter: save a generated PDF from preview page with a custom function

I'm using the pdf package (link to pub.dev page) to create documents inside my app.
How can I save the document from the preview page?
I know about the built-in print and share functions, but I need something else.
I need a function that does the following:
save the document to the device
upload it to Firebase Storage
delete the local file
I know how to do all of these steps, but I'm unable to access the Uint8List bytes of the pdf document that is being previewed... I've tried to add a PdfPreviewAction, but I still can't access the bytes of the document from it
all you have to do is call the save method in the Document() object
import "package:pdf/widgets.dart" as w;
final pdf = w.Document();
Future<Uint8List> getPDFData() async {
return await pdf.save();
}
you can do all the operations using the getPDFData() method
for example:
return PdfPreview(
build: (format) => getPdfData(),
actions: [
PdfPreviewAction(
icon: Icon(Icons.print),
onPressed: (context, fn, format) {
//if you call fn(format), you'll get Future<UInt8List>
}),
],
);

How to download image from URL in app + web in flutter?

Is there any package or method available to save/download image from URL in flutter for web as well app.
You can use Firebase storage or s3-Cognito to achieve this. First, you need to upload the image to the Database using file picker or image picker and use the getDownloadUrl() method to get the imageUrl
You don't need a package.
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'dart:typed_data';
TextButton(
onPressed: () async {
String imageUrl='https://......example';
Uint8List response = await http.get(Uri.parse(imageUrl)).then(
(value) => value.bodyBytes);
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: Image.memory(response),
);
},
);
},
child: Text(
'Show Image from memory',
));
This code alone works without any packages. Now that you have it in memory, you can store it and do other things with it. Convert it between base64 and Unit8List.

How to convert image provider to image in flutter inorder use in canvas? [duplicate]

I basically want to show an image that I have in the assets folder onto the canvas.
import 'package:flutter/material.dart' as ui;
...
ui.Image img = ui.Image.asset("images/some_image.png");
ui.paintImage(canvas: canvas, image: img);
And have got the following error message when I tried to assign img to paintImage's image.
The argument type 'Image (C:\ABC\flutter\packages\flutter\lib\src\widgets\image.dart)' can't be assigned to the parameter type 'Image (C:\ABC\flutter\bin\cache\pkg\sky_engine\lib\ui\painting.dart)'.
I don't really know what could go wrong, I have seen other codes that had a similar approach with ui.Image.
Please advise.
There are two classes called Image in flutter, in different packages.
There's the Widget, which behaves like a widget and can be instantiated from an asset, from memory or from the network through its named constructors.
There's also the ui package Image which is used when painting at a lower level, for example in a CustomPainter. Since this version is in the ui package it's normally imported with the ui prefix like this:
import 'dart:ui' as ui;
Don't import material as ui! That will lead to a lot of confusion.
To make a widget, use the Image.asset constructor, passing the asset name.
To make a ui.Image from an asset use this snippet:
Future<ui.Image> load(String asset) async {
ByteData data = await rootBundle.load(asset);
ui.Codec codec = await ui.instantiateImageCodec(data.buffer.asUint8List());
ui.FrameInfo fi = await codec.getNextFrame();
return fi.image;
}

Drawing a photo from the camera on a canvas?

I want to take a photo with the image_picker library that stores the file on the phone sd card.
I want to then load this image so that i can draw it with a CustomPaint widget by drawing the image on the canvas.
I have tried to load the image with both FileImage and Image.file
but this doesn't work because the 'Image' in the CustomPaint extends NativeFieldWrapperClass2 and isn't an Image widget
Is there anyone who knows how to load a photo and draw it on a canvas?
You need the ui package version of Image (as distinct from the widget called Image)
Import the ui package, and create a method to return the ui.Image:
import 'dart:ui' as ui;
Future<ui.Image> load(String filename) async {
var file = File(filename);
var bytes = await file.readAsBytes();
var codec = await ui.instantiateImageCodec(bytes);
var fi = await codec.getNextFrame();
return fi.image;
}
Use ui.Image to draw. You may need a transform to shrink it.