How to Convert Image to Uint8List in Flutter
I have Image it not from asset or file I want to convert Image to Uint8List
Something like this could work.
final data = await image.toByteData();
final result = data.buffer.asUint8List();
Related
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);
I want to implement twitter share in my flutter application.
I convert my widget to image by using RepaintBoundary and stored as ByteData as below.
Future<ByteData> convertWidgetToImage(GlobalKey key) async {
final RenderRepaintBoundary boundary =
key.currentContext!.findRenderObject() as RenderRepaintBoundary;
final image = await boundary.toImage();
final byteData = await image.toByteData(format: ui.ImageByteFormat.png);
return byteData!;
}
Then, I want to open twitter app with some default text and with this image data. (I can convert this bytedata to local file path url if needed as below)
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
Is there any way I can share this image to twitter (onTap then open twitter app with this image)?
This could be an option but I don't think I can share twitter image
https://pub.dev/packages/social_share
I want to download widgets screenshots as an image file in flutter Web. Is there any to do this?
By the way for converting the widget to Uint8List, I am using the screenshot plugin.
You can use the RenderRepaintBoundary.toImage() function to convert the widget into an image. Then you convert to byteData, then to Uint8list, then do a base64encode on that Uint8List and basically make the widget an anchor element which you can then download.
I've attached some sample code from a project that I was working on that lets the user generate a QR code and download it to the downloads folder on their PC to show what I'm talking about.
Make sure you've imported html at top of dart file:
import 'dart:html' as html;
Then, the code would look something like:
final key = GlobalKey();
final qrTextController = TextEditingController();
//this code "wraps" the qr widget into an image format
RenderRepaintBoundary boundary = key.currentContext!
.findRenderObject() as RenderRepaintBoundary;
//captures qr image
var image = await boundary.toImage();
String qrName = qrTextController.text;
//running on web
if(kIsWeb){
print('registering as a web device');
ByteData? byteData = await image.toByteData(format: ImageByteFormat.png);
Uint8List pngBytes = byteData!.buffer.asUint8List();
final _base64 = base64Encode(pngBytes);
final anchor =
html.AnchorElement(href: 'data:application/octet-stream;base64,$_base64')
..download = "image.png"
..target = 'blank';
html.document.body!.append(anchor);
anchor.click();
anchor.remove();
}
I am simply picking image from image_picker package like this
_imgFromCamera() async {
File image = await ImagePicker.pickImage(
source: ImageSource.camera, imageQuality: 50
);
final bytes = image.readAsBytesSync();
String base64Image = base64Encode(bytes);
print(base64Image);
setState(() {
_image = image;
});
}
You can see I have convert image in the string also. I need to know how can I show this string as an image? Because I save this string on other page now I need to show is an image
You can convert the base64 string into a file using
import `dart:covert`
Uint8List bytes = base64decode(_base64);
and then display it using the widget
Image.memory(bytes),
You can just write it in a single line but I split it for the sake of readability
Image.memory(base64Decode(base64String));
I have a form, in that form I have a signature pad. I used signature 3.2.0 package. That package consist a method toImage(). I want to store that image in the firebase storage. when I try below code.
fileImage = _controller.toImage() as File;
final FirebaseStorage storage = FirebaseStorage.instance;
final String picture = "${DateTime.now().millisecondsSinceEpoch.toString()}.jpg";
StorageUploadTask task = storage.ref().child(picture).putFile(fileImage);
task.onComplete.then((snapshot) async{
loadData.setSignatureURL = await snapshot.ref.getDownloadURL();
});
loadData.storeDetails();
I got an error type 'Image' is not a subtype of type 'File' in type cast.
How can I store signature as an image/
Casting can't be done on a whim, which is why you get this error.
The Image class provides a toByteData method that allows you to retrieve the raw image data as a ByteData object. You can then convert this to a Uint8List. This list then can be directly used for firebase storage with the putData method instead of putFile.
var image = await _controller.toImage();
ByteData data = await image.toByteData();
Uint8List listData = data.buffer.asUint8List();
final FirebaseStorage storage = FirebaseStorage.instance;
final String picture = "${DateTime.now().millisecondsSinceEpoch.toString()}.jpg";
StorageUploadTask task = storage.ref().child(picture).putData(listData);
...
If you need to encode this image to a specific type. You can use a version of the following code which encodes to JPG. It uses the image package which needs to be added as a dependency
import 'package:image/image.dart' as encoder;//This import needs to be added in the file this is being done
var image = await _controller.toImage();
//Store image dimensions for later
int height = image.height;
int width = image.width;
ByteData data = await image.toByteData();
Uint8List listData = data.buffer.asUint8List();
encoder.Image toEncodeImage = encoder.Image.fromBytes(width, height, listData);
encoder.JpegEncoder jpgEncoder = encoder.JpegEncoder();
List<int> encodedImage = jpgEncoder.encodeImage(toEncodeImage);
final FirebaseStorage storage = FirebaseStorage.instance;
final String picture = "${DateTime.now().millisecondsSinceEpoch.toString()}.jpg";
StorageUploadTask task = storage.ref().child(picture).putData(Uint8List.fromList(encodedImage));
...