Flutter how to convery image in base64 string and show image again - flutter

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

Related

how to convert blob url to base64 in flutter web?

I use image_picker: ^0.8.5+3 for upload image in web flutter.
Future _imgFromGallery() async {
final image = await picker.pickImage(
source: ImageSource.gallery, maxWidth: 800, maxHeight: 800);
}
But when I print image.path the uploaded photo, it shows me this address with 'blob' blob 'http://localhost:22808/8108f482-37c1-4b7f-9e9c-9e65d1810f5a'
I want to convert image to base64 and for this I use the following code. But this conversion is not done correctly and completely.Can you help me in this matter?
final bytes = html.File(image.path.codeUnits, image.path);
Uint8List b = Uint8List(bytes.toString().length);
String img64 = Base64Encoder().convert(b);
‍‍‍
I found the solution to my problem. I had to add the ‍‍image_picker_for_web: ^2.1.8 and convert the photo to Base64 with the code below:
var image = await picker.pickImage(
source: ImageSource.gallery, maxWidth: 800, maxHeight: 800);
var imageForWeb = await image.readAsBytes();
String base64Image = base64Encode(imageForWeb);

Convert Image to Uint8List in Flutter

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

Using Flutter Camera package, how do I convert a photo to a base64 string?

Using the Flutter Camera package (v0.9.4+5), how do I convert a captured photo into a base64 string?
I believe the following code will work, but welcome to any thoughts on how the code can be improved.
import 'dart:convert' as convert;
void capturePhoto() async {
// Note: `controller` being initialized as shown in readme
// https://pub.dev/packages/camera#example
XFile photo = await controller.takePicture();
List<int> photoAsBytes = await photo.readAsBytes();
String photoAsBase64 = convert.base64Encode(photoAsBytes);
}
Try this.
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
final bytes = Io.File(image.path).readAsBytesSync();
String img64 = base64Encode(bytes);
print (img64);

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!

How to resize image using multi_image_picker in Flutter?

I'm using multi_image_picker package to pick images and upload to server, but before uploading I want to resize images. I'm trying to accomplish it using dart.ui but having a problem:
//assets is List<Asset> from MultiImagePicker.pickImages
assets.forEach((asset) {
Future<ByteData> byteData = asset.getByteData();
byteData.then((d) async {
List<int> imageData = d.buffer.asUint8List();
String b64 =base64Encode(imageData);
print(b64); // prints [/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE...
//if i send b64 to server then decode it and save as img it's working well
//Resize
ui.instantiateImageCodec(imageData,targetHeight: 800, targetWidth: 600)
.then((codec) {
codec.getNextFrame().then((frameInfo) async {
ui.Image i = frameInfo.image;
ByteData bytes = await i.toByteData();
List<int> resizedImageData = bytes.buffer.asUint8List();
String rb64 = base64Encode(resizedImageData);
print(rb64); // prints too many backslashes:[k5KO/5qWk/+ZlZL/mpaT/5uXlP+alpP/mJSR/5iUkf+YlJH/mZSR/5uWk/+blpP/n5qX/6GcmP+gm5f/oZyY/6GcmP+fmpb/nZi..
//If i send rb64 to server then server cannot decode and save it.
});
});
});
});
This is the function I normally use to resize:
import 'dart:ui' as skia;
Future<skia.Image> resizeImage(String path, {int width, int height}) async {
Uint8List data = await File(path).readAsBytes();
final codec = await skia.instantiateImageCodec(data, targetWidth: width, targetHeight: height);
final frame = await codec.getNextFrame();
return frame.image;
}
As I mentioned in the comment, this is currently not working in Flutter Web but it's due to a bug that will be fixed soon, hopefully.