I'm using the package image_picker to accept an image from the user. This results in a PickedFile and byte array. Since the image is then uploaded I'm wondering how to guess the mime type from the byte array.
PickedFile image =
await _picker.getImage(source: ImageSource.gallery, imageQuality: 50);
Uint8List data = await image.readAsBytes();
package:mime has a lookupMimeType function that can infer the MIME type either from a file extension or from magic bytes in the content. As of writing, it should support detecting content for:
application/pdf
application/postscript
image/gif
image/jpeg
image/png
image/tiff
video/mp4
model/gltf-binary
(See https://github.com/dart-lang/mime/blob/master/lib/src/magic_number.dart for the current list of types it supports out of the box.)
Example:
final data = await image.readAsBytes();
final mime = lookupMimeType('', headerBytes: data);
Related
trying to create pdf file using pdf package then convert it to an image and then convert the image to base64 to save it in a QR code. this should be a simple task for URL images and gallery images. But in my case, the image is generated in the flutter from pdf doc as a memory image. my code is
await for (var page in Printing.raster(await doc.save(), pages: [0, 1], dpi: 72)) {final reciptimage = page.toImage();}
tried the below code but it doesn't work
final bytes = await Io.File(reciptimage).readAsBytes();
I couldn't find any way to generate base64 from the memory image reciptimage.
Any help?
may this eample can help you.
RenderRepaintBoundary boundary = _globalKey.currentContext.findRenderObject();
ui.Image image = await boundary.toImage(pixelRatio: 3.0);
ByteData byteData =
await image.toByteData(format: ui.ImageByteFormat.png);
Uint8List pngBytes = byteData.buffer.asUint8List();
String bs64 = base64Encode(pngBytes);
I tried to compress web image using the below code (with image_compression_flutter package), however it takes about 1 minute to compress a 6 mb image.
Future<Uint8List?> compressImage(Uint8List imgBytes,
{required String path, int quality = 70}) async {
final input = ImageFile(
rawBytes: imgBytes,
filePath: path,
);
Configuration config = Configuration(
outputType: ImageOutputType.jpg,
// can only be true for Android and iOS while using ImageOutputType.jpg or ImageOutputType.pngĂ
useJpgPngNativeCompressor: false,
// set quality between 0-100
quality: quality,
);
final param = ImageFileConfiguration(input: input, config: config);
final output = await compressor.compress(param);
return output.rawBytes;
}
Is it because of web limitations it takes such a long time to compress?
Is there any workaround regarding this issue
Eventually, I found a solution for compressing web image. Image picker now has XFile type which can be directly compressed in web. It took about 2 seconds to compress a 12mb image to 50kb
final XFile? pickedImageFile = await ImagePicker().pickImage(
source: isCamera ? ImageSource.camera : ImageSource.gallery,
imageQuality: imageQuality,
maxWidth: maxWidth,
preferredCameraDevice: CameraDevice.rear);
While uploading to a server there is no need to convert XFile to any type. Just need to read bytes from pickedImageFile
await pickedImageFile.readAsBytes();
I've tried this way, and use it in FileImage but haven't found a rally point
Uint8List bytes = base64.decode(imageBase64);
File file = File.fromRawPath(bytes);
imageBase64 is the image url that has been encode.
The point of this thing is, i want to use it in FileImage or another widget image file but i don't want this image is saved to device and upload this file to the attachments file Rest API
can anyone help me.
If what you want to do once you have the bytes is to show the image, then you can use Image.memory.
var imageWidget = Image.memory(bytes);
OTOH If you're trying to upload the image, you don't need to convert it into file. You can use bytes directly assuming you can't upload it using base64 enconding. Package http
import 'package:http/http.dart';
...
//create multipart using filepath, string or bytes
MultipartFile multipartFile = MultipartFile.fromBytes(
'file',
byteData,
filename: fileName,
);
//add multipart to request
request.files.add(multipartFile);
var response = await request.send();
If you have an UintList8 you can use MemoryImage(bytes).
Uint8List bytes = base64.decode(imageBase64);
Image(image: MemoryImage(bytes));
I am using MultipartFile to send my image file to server. I know that when I use ImagePicker
File ImageFile;
_openCamera() async {
var picture = await ImagePicker.pickImage(source: ImageSource.camera);
this.setState(() {
imageFile = picture;
});
}
then use MultipartFile like this
request.files.add(http.MultipartFile.fromBytes('img', ImageFile.readAsBytesSync(), filename: 'photo.jpg'));
But my problem is I want my image is from my assets like from here Image.asset('images/photo1.png');. I have error
A value of type 'Image' can't be assigned to a variable of type 'File'.
Try changing the type of the variable, or casting the right-hand type to 'File'.
So, my question is how I can send my image using MultipartFile method?
First, obtain the asset as list of bytes:
var bytes = (await rootBundle.load('images/photo1.png')).buffer.asUint8List();
Then use that in the MultipartFile named constructor:
var mpFile = http.MultipartFile.fromBytes('img', bytes, filename: 'photo.jpg');
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);