How to convert CameraController's XFile to Image type in Flutter?
Solution 1
This is my found solution:
import 'dart:io';
import 'package:image/image.dart' as img;
...
CameraController _controller;
...
final xFile = await _controller.takePicture();
final path = xFile.path;
final bytes = await File(path).readAsBytes();
final img.Image image = img.decodeImage(bytes);
Solution 2
You can get the path from XFlie by using .path property and then show image using
Container(
child: Image.file(File(XFile.path)),
);
Mohsen Emami
It’s 10 years that I've been working in software development industry with the concentration on developing Native and Cross-Platform applications using Android (Java/Kotlin) and also Flutter framework (Dart). I'm an ever-learning process-focused mobile engineer that always enjoyed working with new technologies and being challenged with obstacles to overcome. I've master's degree in Computer Engineering from University of Tehran and have always tried to apply my academic knowledge in my professional work.
Updated on December 27, 2022Comments
-
Mohsen Emami over 1 year
Flutter's
CameraController
has atakePicture()
method for taking picture from the camera which gives type ofFuture<XFile>
, so I need to convert it toImage
type frompackage:image/image.dart
package to manually crop it with another method.How to convert it?
-
Sevastyan Savanyuk over 2 yearsNo need to create a
File
:final bytes = await file.readAsBytes(); return decodeImage(bytes);
-
Kohls almost 2 yearsOP asked for Image type, not presenting Image path in Widget
-
data_pikachu almost 2 yearsThe argument type 'Future<Uint8List>' can't be assigned to the parameter type 'List<int>' - I get this error using this method