Invalid image on Creating thumbnails from video with flutter - flutter

Trying to generate an Thumbnail image from video , the file is created but , errors as Invalid image on load .Using this package video_thumbnail
Creating thumbnail ,
Future<File> genThumbnail(url) async {
//WidgetsFlutterBinding.ensureInitialized();
Uint8List bytes;
final Completer<ThumbnailResult> completer = Completer();
bytes = await VideoThumbnail.thumbnailData(
video: url,
imageFormat: ImageFormat.JPEG,
maxHeight: 250,
maxWidth: 300,
timeMs: 0,
quality: 0);
int _imageDataSize = bytes.length;
print("image size: $_imageDataSize");
//final _image = Image.memory(bytes);
//var _file =File.fromRawPath(bytes);
Directory tempDir = await getTemporaryDirectory();
var uint8list = bytes;
var buffer = uint8list.buffer;
ByteData byteData = ByteData.view(buffer);
File file = await File('${tempDir.path}/img/THUMBNAIL${DateTime.now().toIso8601String()}.JPEG').writeAsBytes(
buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));
return file;
}
Saving to firestore
await genThumbnail(fileurl).then((_thumbFIle) async{
String Thumbfileurl = await uploadFile(_thumbFIle, 'thumbnailOf${filenamewithoutExtension}.JPEG', 'videothumbnail');
await sendFileToFirestoreChat(fileType, fileurl, filenamewithoutExtension,Thumbfileurl);
return fileurl;
});
The Saved Image ,
https://firebasestorage.googleapis.com/v0/b/proj-inhouse.appspot.com/o/videos%2Fvideothumbnails%2FthumbnailOfVID-20210301-WA0006.JPEG?alt=media&token=fa4f23c1-601f-486b-97d1-c63e221166af

Posting this as a Community Wiki as it's based on #pskink comments.
To resolve, add the writeAsBytes(bytes) instead of writeAsBytes(buffer.asUint8List()). There is no need for any buffer.

Related

What is targetPath in flutter_image_compress?

How to fix this error
I am trying to reduce the size of an image file and here i use flutter_image_compress
Future<File> compressFile(File file) async {
var result = await FlutterImageCompress.compressAndGetFile(
file.absolute.path,
file.absolute.path,
quality: 66,
);
return result!;
}
compressAndGetFile takes a String path and a String targetPath. They must be different or you'll have an error.
try this :
final file = await ImagePicker().pickImage(source: source);
var fileFromImage = File(file.path);
var basename = path.basenameWithoutExtension(fileFromImage.path);
var pathString = fileFromImage.path.split(path.basename(fileFromImage.path))[0];
var pathStringWithExtension = "$pathString${basename}_image.jpg";
Future<File> compressFile(File file) async {
var result = await FlutterImageCompress.compressAndGetFile(
file.absolute.path,
pathStringWithExtension,
quality: 66,
);
return result!;
}
Also make sure you added flutter_image_compress to your pubspec.yaml

Save Image From ImagePicker Locally as a Memory(cache)

I want to save an Image from ImagePicker as a Memory but error Occured . Can you Please help me with this function and if another function needed to load image please Mentioned it below.
Uint8List? memoryImage;
Future getImage() async {
final picker = ImagePicker();
final image = await picker.getImage(source: ImageSource.camera);
if (image == null) return;
final Directory directory = await getApplicationDocumentsDirectory();
final path=directory.path;
final Filename=basename(image.path);
File file = File('$directory/$Filename.jpg');
final bytes = await file.readAsBytes();
final byte1= file.writeAsBytes(bytes);
setState(() {
memoryImage = byte1 as Uint8List?;
});
}
With this line you can write image bytes as a file.
File imageFile = await File(fileSavePath).writeAsBytes(imageBytes);
To access the Uint8List from the file you need to use
Uint8List memoryImage = File(imagePath).readAsBytesSync();
Or
Uint8List memoryImage = await File(imagePath).readAsBytes();
here the problem in your code is you are assigning file to a Uint8List. That's the error I guess

How can I set a unique UUID for my PickedFile?

I'm trying to give an image uploaded from camera a unique id before uploading to Firestore.
I've been using Reed Barger's https://www.udemy.com/course/build-a-social-network-with-flutter-and-firebase/ tutorial but some of the code he used has been deprecated. In this case, image compression.
His code to compress and set unique image id was
compressImage() async {
final tempDir = await getTemporaryDirectory();
final path = tempDir.path;
Im.Image imageFile = Im.decodeImage(file.readAsBytesSync());
final compressedImageFile = File('$path/img_$postId.jpg')
..writeAsBytesSync(Im.encodeJpg(imageFile, quality: 85));
setState(() {
file = compressedImageFile;
});
}
The libraries he used include
import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';
import 'package:image/image.dart' as Im;
import 'package:uuid/uuid.dart';
His variables were
File file;
String postId = Uuid().v4();
Since then, the image_picker library allows me to compress the image by seting the imageQuality so there's no need for the image.dart package. How can I modify my code (below) to set a unique image id in the format Reed used?
PickedFile file;
String postId = Uuid().v4();
handleTakePhoto() async {
Navigator.pop(context);
file = (await ImagePicker().getImage(
source: ImageSource.camera,
maxHeight: 675.0,
maxWidth: 960,
imageQuality: 85,
));
final tempDir = await getTemporaryDirectory();
final path = tempDir.path;
final compressedImageFile = File('$path/img_$postId.jpg');
setState(() {
this.file = file;
file = compressedImageFile;
});
}
As it is, it returns an error that compressedFileImage can't be assigned to type PickedFile.
I've tried adding the cast as PickedFile but that doesn't work.
That's because you're creating a File instead of a PickedFile:
final compressedImageFile = File('$path/img_$postId.jpg');
Just change it to:
final compressedImageFile = PickedFile('$path/img_$postId.jpg');
This has worked so far. I've created a variable File storedImage; Then changed
setState(() {
this.file = file;
file = compressedImageFile;
}
to
setState(() {
this.file = file;
storedImage = File(file.path);
storedImage = compressedImageFile;
print(compressedImageFile); //For debugging
}
The print statement gives me the new uuid and name.

upload Image to firebase storage and NOT File

There are lot of references of uploading a dart:io 'File' but I want to upload a material.dart Image to firebase.
My Image comes from a processed Thumbnail of a video and not just picked by a Image picker.
This is how I generate a thumbnail,
Future<ThumbnailResult> genThumbnail(url) async {
//WidgetsFlutterBinding.ensureInitialized();
Uint8List bytes;
final Completer<ThumbnailResult> completer = Completer();
bytes = await VideoThumbnail.thumbnailData(
video: url,
imageFormat: ImageFormat.JPEG,
maxHeight: 250,
maxWidth: 300,
timeMs: 0,
quality: 0);
int _imageDataSize = bytes.length;
print("image size: $_imageDataSize");
final _image = Image.memory(bytes);
//final _file =File.fromRawPath(bytes);
_image.image
.resolve(ImageConfiguration())
.addListener(ImageStreamListener((ImageInfo info, bool _) {
completer.complete(ThumbnailResult(
image: _image,
dataSize: _imageDataSize,
height: info.image.height,
width: info.image.width,
));
}));
return completer.future;
}
and this is how File is uploaded to firebase
String Thumbfileurl = await uploadFile(thumbResult.image, fileName, fileType);
And inside uploadFile()
final StorageUploadTask uploadTask = storageReference.putFile(file);
So as you see , A File is needed , but i have an Image , is there a way to Convert Image to File or is there any workaround to achieve this .

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.