Flutter Image format into byte fomat - flutter

How to get image in byte format using flutter on_click of button and also how to stored image in gallery on any devices please help out on this.
onPressed: () {
isVideo = false;
_onImageButtonPressed(ImageSource.camera);
}

This should get the bytes:
onImageButtonPressed(Future<File> futureFile) {
var file = await futureFile;
var bytes = file.readAsBytesSync();
}

Related

How can I update a photo from flutter, if the path of a photo is from the database?

I have a problem and it is that the method http.MultipartFile.fromPath() is giving me an error of Unhandled Exception: FileSystemException: Cannot retrieve length of file, path = '' (OS Error: No such file or directory, errno = 2) when I don't want to update the image I use the image picker (I don't use the image picker to open the camera because I don't want to update the photo), but when I want to use the image picker (I want to update the photo) it works correctly.
When I use the image picker I use the path I get when I open the camera and take the picture normally but when I don't use the camera the path is the one I get from the database and assign it to the image picker file.
So the problem appears when I don't want to update the photo.
Example of a new camera path, just used by the camera and a path that I get from the database, previously saved::
I get this route from the camera
File: '/data/user/0/com.example.rati_app/cache/3b4075c7-03df-471b-b8c0-7b88561ab29f32288.jpg'
i get this route from the data base,
the problem arises when i want to send this route
fzn/3b4075c7-03df-471b-b8c0-7b88561ab29f32288.jpg
How could I solve that problem?
Help would be greatly appreciated.
code and image::
Future UpdateImageUser(RoutePictureProfile) async {
var Url = Uri.parse("http://.....");
var request = await http.MultipartRequest('POST', Url);
var pic = await http.MultipartFile.fromPath('User_image',RoutePictureProfile);
request.files.add(pic);
var response = await request.send();
}
String fotousuarioo; fotousuarioo is the variable that contains the path of the image from the database and that I bring it using future builder
File image;
IconButton(
icon: Icon(Icons.check, color: Colors.black),
onPressed: () {
if(image == null){
image = File(fotousuarioo);}
if(KeyFormValidaciones.currentState.validate() && image != null ){
update2perfilmodel.setPhotoCrud = image.path;
Updateeprofileservices.UpdateImageUser(editar2perfilmodel.Photocrud);}),
//////////////////////
Future OpenCamara33() async {
final pickedFile = await ImagePicker().pickImage(source: ImageSource.camera);
setState(() {
if (pickedFile != null) {
image = File(pickedFile.path);// direccien de la foto
}else{
}});}
enter image description here

Get text content from an epub file flutter

I am trying to extract text content from an epub file but unable to do so. I have tried converting it to bytes and then read it but it's not returning a proper text. I can't find any library that helps me do so.
I only need the text file from the epub file so that I can read it with text to speech.
I did it using the epubx package
Here is the complete code
File _epubFile = File(file.path);
final contents = await _epubFile.readAsBytes();
EpubBookRef epub = await EpubReader.openBook(contents.toList());
var cont = await EpubReader.readTextContentFiles(epub.Content!.Html!);
List<String> htmlList = [];
for (var value in cont.values) {
htmlList.add(value.Content!);
}
var doc = parse(htmlList.join());
final String parsedString = parse(doc.body!.text).documentElement!.text;
return parsedString;

How do i open srt file on flutter?

I am trying to load subtitle to a video using the flutter video player package it works good for short files but it stopped as the file get bigger
I trayed subtitle_wrapper package but it has many bugs
Future<ClosedCaptionFile> getSubtitle(String url) async {
final data = NetworkAssetBundle(Uri(path: url));
final newdata = await data.load(url);
String fileContents = getStringFromBytes(newdata);
return captionFile = SubRipCaptionFile(fileContents);
}
this is getStringFromBytes function
getStringFromBytes(ByteData data) { final buffer = data.buffer;
var list = buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
return utf8.decode(list); }
it wasn't the size after all I tested on some srt files that have a blank space for some duration and the flutter converter do a check on every element if the length is<3 it break on get out of the loop

Dart: How to Encode Decode animated webp files?

I am trying to decode animated webp image file and encode it's frames to animated webp image file in Dart (cli)
Dart Code:
import 'dart:io';
import 'package:image/image.dart';
void main() {
final image = 'asset/test.webp';
File bytes = File(image);
List<int> list = bytes.readAsBytesSync();
var anim = decodeWebPAnimation(list)!;
var obj = WebPEncoder();
for (var frame in anim) {
obj.addFrame(frame, duration: 1);
}
var finished = obj.finish();
print(finished); // [] empty, no frames get added
// final encodedAnim = obj.encodeAnimation();
File('asset/output.webp').writeAsBytesSync(finished!,
flush: true); // pass something like encodeWebp(input)
}
pubspec.yaml
dependencies:
image: ^3.1.3
input file: https://raw.githubusercontent.com/WhatsApp/stickers/main/Android/app/src/main/assets/2/07_OK.webp
Output:
There is zero frame after calling finish(), so the output image file is invalid. i.e. file size: 0 byte
Ref of Dart APIs:
WebpEncoder:
https://pub.dev/documentation/image/latest/image/WebPEncoder-class.html
DecodeWebpAnimation: https://pub.dev/documentation/image/latest/image/decodeWebPAnimation.html
What went wrong? How to fix this?
Thank You!

Converting Image FlutterWebImagePicker Output to File

I'm using Flutter web for a webapp and having trouble converting an image from the image picker to a file in order to upload it to my server. I display the image in Image.file(xxx) but I get the error:
Error while trying to load an asset: FormatException: Illegal scheme
character (at character 6)
Image(image:%20MemoryImage(Uint8List%234267a,%20scale:%201),%20frameBuilder...
Here is the code I'm trying:
Future getImage(bool isCamera) async {
Image image;
if (isCamera) {
image = await FlutterWebImagePicker.getImage;
} else {
}
var bytes = await rootBundle.load('$image');
String tempPath = (await getTemporaryDirectory()).path;
File file = File('$tempPath/profile.png');
await file.writeAsBytes(
bytes.buffer.asUint8List(bytes.offsetInBytes, bytes.lengthInBytes));
setState(() {
currentSelfie = file;
_accDetails['customer_selfie'] = currentSelfie;
});
}
Thanks in advance
I've tested this package and was very happy with the result imagePickerWebit returns 3 different types it can be in the form of Image(widget for preview), byte, File(upload)
then you can use this to get the values
html.File _cloudFile;
var _fileBytes;
Image _imageWidget;
Future<void> getMultipleImageInfos() async {
var mediaData = await ImagePickerWeb.getImageInfo;
String mimeType = mime(Path.basename(mediaData.fileName));
html.File mediaFile =
new html.File(mediaData.data, mediaData.fileName, {'type': mimeType});
if (mediaFile != null) {
setState(() {
_cloudFile = mediaFile;
_fileBytes = mediaData.data;
_imageWidget = Image.memory(mediaData.data);
});
}
I havent used the plugin although your code has 2 issues. One is the if statement and the second one is using Rootbundle. If you are picking from the filesystem, my assumption isCamera would be false. You have not added any logic for the falsy condition.
if (isCamera) {// This would be true if the source was camera
image = await FlutterWebImagePicker.getImage;
} else {
}
Additionally,
var bytes = await rootBundle.load('$image');
From the flutter documentation, rootbundle contains the resources that were packaged with the application when it was built. Those are assets that you define in your pubspec. yaml. You are selecting an image at runtime hence its not bundled as an asset.
Since the package appears to return an image object, use the toByteData method on the image i.e
image = await FlutterWebImagePicker.getImage;
await image.toByteData();//This method has some parameters. Look into them