I want to convert image captured with camera to base64 but it seems am converting the image path and not the image itself, please help me.
File file;
final picker = ImagePicker();
void _choose() async {
final pickedFile = await picker.getImage(source: ImageSource.camera);
file = File(pickedFile.path);
if (file != null) {
setState(() {
base64Encode(file.readAsBytesSync());
print(base64Encode(Image.file(file));
});
}
}
Wrong:
print(base64Encode(Image.file(file));
Right:
print(base64Encode(file.readAsBytesSync()));
Related
Following a tutorial for adding and saving images and getting errors for the below code, under ImagePicker, ImageSource and SelectedImage.
Future getImage() async {
var image = await ImagePicker.PickImage(source: ImageSource.camera);
setState(() {
selectedimage = image;
});
}
You need to create an instance first, then you will be able to use pickImage method.
Future getImage() async {
XFile? image = await ImagePicker().pickImage(source: ImageSource.camera);
if (image != null) {
setState(() {
selectedimage = image;
});
}
}
Fine more about image_picker
You have to be careful whenever playing with the setState() functions. This combined with how you pick the image and how you store it in the 'var' in your case could cause further trouble. To generally ease things up for further use I recommend creating a utils class, in which you would have an image picker method, just like this one:
import 'package:image_picker/image_picker.dart';
pickImage(ImageSource source) async {
final ImagePicker _imagePicker = ImagePicker();
XFile? _file = await _imagePicker.pickImage(source: source);
if (_file != null) {
return await _file.readAsBytes();
}
print('No Image Selected');
}
Afterwards, if you would like to call that in any other instance you would need something like this, though this is for a url image(I had it in hand), such as:
void selectImage() async {
Uint8List? im = await pickImage(ImageSource.gallery);
final ByteData imageData = await NetworkAssetBundle(Uri.parse(
"url for template image"))
.load("");
final Uint8List bytes = imageData.buffer.asUint8List();
// if null - use the template image
im ??= bytes;
// update state
setState(() {
_image = im!;
});
}
Hope it helped in a way.
I am trying to save an image to my local mobile device when I use this code from the pub.dev example page of the camera package. However I am not finding the file saved on the device. Can anyone please help me with how to save the image taken to a given path
void onTakePictureButtonPressed() {
takePicture().then((XFile? file) {
if (mounted) {
setState(() {
imageFile = file;
videoController?.dispose();
videoController = null;
});
if (file != null) {
showInSnackBar('Picture saved to ${file.path}');
}
}
});
}
Using await ImagePicker.pickImage(...), you are already on the right track because the function returns a File.
The File class has a copy method, which you can use to copy the file (which is already saved on disk by either the camera or by lying in gallery) and put it into your application documents directory:
// using your method of getting an image
final File image = await ImagePicker.pickImage(source: imageSource);
// getting a directory path for saving
final String path = await getApplicationDocumentsDirectory().path;
// copy the file to a new path
final File newImage = await image.copy('$path/image1.png');
setState(() {
_image = newImage;
});
You should also note that you can get the path of the image file from ImagePicker using image.path, which will also contain the file ending that you might want to extract and you can save your image path by using newImage.path.
You can use path provider and save the images like this
https://pub.dev/packages/path_provider
final XFile? image = await ImagePicker().pickImage(source: ImageSource.gallery);
if (image == null) return;
final String newFile = await getApplicationDocumentsDirectory().path;
final var fileName = basename(file.path);
final File localImage = await image.saveTo('$newFile/$fileName');
I use pickedFile to save an video from my device. And I have some problems. After selecting an video from the gallery, it changes its name to something else (image_picker6849334646212907222). How to get the original name from the video?
Future<File> captureAndSaveVideo() async {
File _image;
final picker = ImagePicker();
final pickedFile = await picker.getVideo(source: ImageSource.gallery);
setState(() {
_image = File(pickedFile.path);
});
var videoName = pickedFile.path.split('/').last;
if (_image == null) return null;
//final fileName = path.basename(_image.path);
try {
final directory = await getExternalStorageDirectory();
print('File path is :${videoName}');
if (directory != null)
return _image.copy('${directory.path}/${videoName}');
;
} catch (e) {
return null;
}
}
use this package path
import 'package:path/path.dart' as path;
var videoName = path.basename( pickedFile.path);
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
I am trying to get a image using camera in flutter using image picker but i am getting an error
lib/Screen/AllDocuments.dart:15:3: Error: 'File' isn't a type.
File _image;
I have also tried to cast but still it is not working
File _image;
final picker = ImagePicker();
Future getImage() async {
final pickedFile = await picker.getImage(source: ImageSource.camera);
setState(() {
_image = File(pickedFile);
});
}
How to fix this error
You have to call the .path method on the pickedFile.
See this documentation here => Image Picker
Use this :
File _image;
final picker = ImagePicker();
Future getImage() async {
final pickedFile = await picker.getImage(source: ImageSource.camera);
setState(() {
_image = File(pickedFile.path);
});
}