Flutter Unit8List Resize Photo - flutter

I want to change the size of the photos I take with flutter unit8list as http extension, for example, the size of the photo is 1280x800, but I want to adjust its size manually. Is this possible?
String imgurl = "http://.....jpg";
Uint8List bytes = (await NetworkAssetBundle(Uri.parse(imgurl),)
.load(imgurl))
.buffer
.asUint8List();

Related

upload and get image from firebase storage flutter web

I am currently developing a web filter application
In the application, when I enter product details, I need to choose an image of the product
But I face a problem, when I choose an image from the computer, I cannot get the image path, the name and the bytes only, and when I upload the bytes to the firebase, I cannot get the image because it is bytes. How can I calculate the path of the image and display it in a list?I am currently developing a flutter web application
In the application, when I enter product details, I need to choose an image for the product from my PC
But I face a problem, when I choose an image from the computer, I cannot get the image path, the (i get name and the bytes only), and when I upload the bytes to the firebase, I cannot get the image because it is bytes not as path. How can I get the path of the image and display it in a list products?
Uint8List? fileBytes;
String? fileName;
String? picUrl;
void chooseImage() async {
FilePickerResult? result = await FilePicker.platform.pickFiles();
if (result != null) {
fileBytes = result.files.first.bytes;
fileName = result.files.first.name;
}
}
uploadImageToFirebase() async {
UploadTask _uploadTask = (await FirebaseStorage.instance
.ref('uploads/$fileName')
.putData(fileBytes!)) as UploadTask;
picUrl = await (await _uploadTask).ref.getDownloadURL();
}

How can one convert an XFile image from any format to a JPG image for upload?

Right now I'm able to chose an image as XFile using MultiImage picker as follows:
final List<XFile>? images = await _picker.pickMultiImage(maxWidth: _maxWidth, maxHeight:_maxHeight);
which then gives me a list of images which I am able to send into my upload service for uploading to my server
for (var image in images)
{
await myUploadService(image); //image is an XFile
}
Now what I need to do is convert the images, no matter what format they are in, into a JPG image before the upload
So I have tried to convert the images, using the image Flutter package (https://github.com/brendan-duncan/image) as follows:
import 'package:image/image.dart' as ImageConvert;
for (var image in images)
{
//Convert all images to JPG
final newImage = ImageConvert.decodeImage(File(image.path).readAsBytesSync())!;
var newImg = ImageConvert.encodeJpg(newImage);
var newImg2 = Image.memory(Uint8List.fromList(newImg)) as XFile; // <--- the main problem
await myUploadService(newImg2);
}
But this isn't working, mainly at the point where I try to put the image back into an XFile format for upload with my upload service
So the main question is here is how can I convert the newly encoded JPG image back to XFile format?
Open to totally different strategies of dealing with this issue as well

How to Share Image from API(URL) in flutter?

Here I want to share image which I get through API. I tried different method for this functionality but I did not get any solution because every solutions have for only one image.
Exactly, I get multiple image from Url and I open any particular image in next page. So, I want to share that image which I opened in another page.
I tried for sharing image but I could not did this. Whenever I try to share that image, Image url share with sharing option on device but I want share image not URl of image How I can accomplish this?
You have to download the image like this:
http.Response response = await http.get(url);
then create an image on the device using the downloaded image like this (Read and Write Files):
final directory = await getTemporaryDirectory();
final path = directory.path;
final file = File('$path/image.png');
file.writeAsBytes(response.bodyBytes);
The getTemporaryDirectory() is in the plugin path_provider. Now, you have the image you'd like to share stored in the temporarily as "image.png" and you can use the share_plus plugin to share the image like this:
Share.shareFiles(['$path/image.png']);
Thanks to #abdulrazak, At first, you have to download the image in a temporary path & then you can share the image as you want. used Dio to download the image & share_plus for sharing
void _shareNetworkImage(String url) async {
Directory tempDir = await getTemporaryDirectory();
final path = '${tempDir.path}/test.jpeg';
await Dio().download(url, path);
Share.shareFiles([path]);
}
You can use the esys_flutter_share plugin.
Install it, and then get dependencies:
dependencies:
esys_flutter_share: ^1.0.2
Import the plugin to your code:
import 'package:esys_flutter_share/esys_flutter_share.dart';
Use the below code:
var request = await HttpClient().getUrl(Uri.parse('https://yourImageURL.jpg'));
var response = await request.close();
Uint8List bytes = await consolidateHttpClientResponseBytes(response);
await Share.file('ESYS AMLOG', 'amlog.jpg', bytes, 'image/jpg');

How to convert a PDF file to an image with Flutter?

How can I convert a PDF file into an image with Flutter?
I want to print the image to an ESC/POS printer using esc_pos_printer. This package won't accept PDFImage, it needs to be a Flutter Image.
I see plenty of PHP plugins that do this but nothing for Flutter.
edit: There is an answer to another question here which shows some code to decode an image from "pdf64" but I can't figure out exactly what "pdf64" is.
I created a PDF from html using flutter_html_to_pdflike this:
Directory appDocDir = await getApplicationDocumentsDirectory();
var targetPath = appDocDir.path;
var generatedPdfFile = await FlutterHtmlToPdf.convertFromHtmlContent(
htmlContent, targetPath, targetFileName);
generatedPdfFilePath = generatedPdfFile.path;
Now I need to know how to create a Flutter Image from that PDF or the bytecode to send raw to the printer.
You can use https://pub.dev/packages/printing:
await for (var page in Printing.raster(document)) {
final image = page.asImage();
...
}
This plugin can also convert your Html to Pdf with this:
final pdf = await Printing.convertHtml(
format: PdfPageFormat.a4,
html: '<html><body><p>Hello!</p></body></html>',
));

How I can convert an image from gallery or camera to binary string?

I used image_picker to choose an image from gallery or take a photo with camera and now I have to send it on request to backend as an array of bytes. My issue is that I don't know How I can encode the image. Can anyone tell me what I should use and how?
var image = await ImagePicker.pickImage(source: ImageSource.camera);
File imageFile=image ;
List<int> imageBytes = imageFile.readAsBytesSync();
String base64Image = base64UrlEncode(imageBytes);