Pick and resize image flow, how to implement it fluently - flutter

I need to provide a flow for the user to pick an image, resize it and store it.
I have the user profile picture with a InkWell and the following onTap method
var original = await ImagePicker().pickImage(source: ImageSource.gallery);
if (original == null) return;
File croppedFile = await ImageCropper().cropImage(
sourcePath: original.path,
aspectRatioPresets: [CropAspectRatioPreset.square],
androidUiSettings: AndroidUiSettings(
toolbarColor: Palette.primary,
lockAspectRatio: false),
iosUiSettings: IOSUiSettings(
minimumAspectRatio: 1.0,
));
if (croppedFile != null) upload(croppedFile);
This works fine. However the ImagePicker and the ImageResizer are two different plugins and two different flows. I need to await the first and then move to the second.
This creates a weird behaviour for the user. While moving from picker to resizer the screen goes back for few seconds to the main UserPage
How can I avoid that? How can I make these two plugins' flow fluently move one from each other without this small time-frame?
I guess this is due to the picker frame being removed from the stack and then the resizer one being added.

Related

Uploading image into Floating Action Button, set state not working

I have a floating action button that I want a user to click to take a picture with their camera and then have that image replace the camera icon on the floating action bar button.
Here is the code for my FAB, and including uploading the image to firestore storage.
floatingActionButton: FloatingActionButton.large(
heroTag: "add image",
backgroundColor: const Color(0xFF93C3B9),
child: (imageURL == ' ')
? const Icon(Icons.add_a_photo_outlined)
: Image.network(imageURL),
//open add gear page
onPressed: () async {
// todo: upload an image to Firebase Storage
//Take picture
ImagePicker imagePicker = ImagePicker();
XFile? file = await imagePicker.pickImage(source: ImageSource.camera);
if (file == null) return;
String uniqueFileName =
DateTime.now().millisecondsSinceEpoch.toString();
//Get reference to storage root
Reference referenceRoot = FirebaseStorage.instance.ref();
Reference referenceDirImages = referenceRoot.child('images/$userID');
Reference referenceImageToUpload =
referenceDirImages.child(uniqueFileName);
try {
//upload image
await referenceImageToUpload.putFile(File(file.path));
//get download URL
setState(() async {
imageURL = await referenceImageToUpload.getDownloadURL();
print(imageURL);
});
//upload path to fireStore database
} catch (error) {}
},
),
After the image uploads it's like the set state is not working to replace the icon with the image. The odd part is is I crtl-s and save in Visual Studio Code then the widgets seem to rebuild and then the image is visible there...
So after playing around with my code a bit I decided to edit the above code and take tha await function out of the setState() and make setState() not async anymore:
//get download URL
String tempUrl = await referenceImageToUpload.getDownloadURL();
setState(() {
print("--------- Set State -----------");
imageURL = tempUrl;
print("--------- Set State end -----------");
});
print("New image url $imageURL ------------");
not sure why it works, but this solves my issue.
By your description of the issue, I think you might be using StatelessWidget instead of StatefulWidget.
You see the button change when performing a hotreload because the value of imageURL is correctly changing internally, but you need a StatefulWidget to update the UI also.
Hope it helps!

Flutter, image_crop scale, resolution, size, quality issue

I`m using this package https://pub.dev/packages/image_crop
I want to upload a 4000x4000 quality picture to the server after converting it to 1080.
Firstly, I do the below
final pickedFile = await ImagePicker()
.pickImage(source: ImageSource.gallery, imageQuality: 100);
if (pickedFile == null) {
return;
}
final file = File(pickedFile.path);
final sample = await ImageCrop.sampleImage(
file: file, preferredWidth: 4000, preferredHeight: 4000
);
and then secondly I do the below
final sample = await ImageCrop.sampleImage(
file: file_tmp!,
preferredSize: (4000 / scale).round(),
);
and then lastly I do the below
final full = class_image.copyResize(image,
height: 1080, width: 1080);
I don't know if I'm getting it right now. Every time I change the value of preferredSize, the size of the final image file seems to change, and in the case of image_crop, the first scale is automatically set to 0.5, which causes deterioration in image quality.
First of all, I want to change the scale to 1.0, and I want it to come out without enlarging the screen. (This seems to cause quality degradation)
And please check if my logic is okay
If anyone knows this package well, please reply.

Why won't my image show up after picking from image_picker in Flutter?

I have been using image_picker: ^0.8.4+9 for the past couple of months with no problems. Today whenever I try to pick an image the gallery opens and I am able to pick an image, however once thats done the image doesn't appear on the screen. No errors and I update my github with it everyday and checked to see if something was changed but nothing was. I've tried using other versions only to have the same problem. I understand that this is an issue with M1 Macs sometimes, however why would it suddenly stop working using the same version? I have seen other questions similar like this. I have the correct permissions and there hasn't been a thing changed since it was last working. This could be a problem I cannot fix due to it being an iOS issue or something else but I wanted to see if anyone else is having this issue. Can't test on a physical device since I am on iOS beta but still it was working on the sim so that shouldn't matter.
Here is the basic function of getting am image and storing it:
File? pickedImage;
Future pickImage() async {
final _imagePicker = ImagePicker();
final pickedImage =
await _imagePicker.pickImage(source: ImageSource.gallery);
final imageTemp = File(pickedImage!.path);
setState(() => this.pickedImage = imageTemp);
}
Here is the button to initiate the gallery and pick the image:
ElevatedButton(
onPressed: () async {
final _imagePicker = ImagePicker();
final pickedImage = await _imagePicker.pickImage(
source: ImageSource.gallery);
final imageTemp = File(pickedImage!.path);
setState(() => this.pickedImage = imageTemp);
},
child: const Text('Add Image')),
const SizedBox(height: 20),
// The image should show here if not null else the 'No image' text
pickedImage != null
? Image.file(pickedImage!, height: 200, width: 200)
: Text('No Image'),
EDIT: As Vandad linked in the comment below, it seems this is a newer issue. Here is the discussion. github.com/flutter/flutter/issues/98569

Flutter Resize ImagePicker Image before uploading to firebase

I am building a demo wallpaper app using flutter where users can upload images to firebase. When loading those images I first want to load a small version of the image and only once the user clicks on the image, load the full version.
In order to achieve this I thought I would simply upload 2 versions in the background once a user picks the image. Now I am struggling with how to achieve this.
Here is how the user picks the image using ImagePicker into a file var.
Future pickImage() async {
var tempImage = await ImagePicker.pickImage(source: ImageSource.gallery, maxHeight: 2000);
print(tempImage.runtimeType);
setState(() {
inspirationimage = tempImage;
});
String result = await uploadImage();
}
As you can see the tempimage is the full size version. I would now have sth like this:
var smallImage = tempImage.resize(height: 200);
Obviously this does not work as tempImage is of type file. Any ideas how this is usually solved?
Thanks
Since you are using Firebase, you can use the Firebase "Resize Image" extension. If you want to do it on client side however, take a look at this answer.
Why don't you let your user crop the image by using this
image_cropper 1.3.1 plugin?
link : https://pub.dev/packages/image_cropper
Example
// File image = await ImagePicker.pickImage(source: source);
final _picker = ImagePicker();
PickedFile image = await _picker.getImage(source: source);
if (image != null) {
// Remove crop attribute if we don't want to resize the image
File cropped = await ImageCropper.cropImage(
sourcePath: image.path,
aspectRatio: CropAspectRatio(ratioX: 1, ratioY: 1),
compressQuality: 100, // 100 means no compression
maxWidth: 700,
maxHeight: 700,
compressFormat: ImageCompressFormat.jpg,
androidUiSettings: AndroidUiSettings(
toolbarColor: primaryColor,
toolbarTitle: "RPS Cropper",
statusBarColor: primaryColor,
backgroundColor: Colors.white,
//toolbarWidgetColor: primaryColor,
activeControlsWidgetColor: primaryColor,
//dimmedLayerColor: primaryColor,
cropFrameColor: primaryColor,
cropGridColor: primaryColor,
),
);

How to avoid app crash when picking a image with flutter and image_picker package? [duplicate]

This question already has answers here:
How to solve the image picker crash app error?
(6 answers)
Closed 3 years ago.
I'm building a application for taking photos and send them into firestore, i'm using image_picker :0.5.0+8 when I'm trying to pick a image from camera or from gallery the app crash. it open gallery very well or the camera very well but if a choose a picture the app crash.
this is my code:
File image_state; AppState state;
bool isLoading; bool isShowSticker; String _imageUrl;
Future getImage() async {
var tempImage = await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
image_state = tempImage;
state = AppState.picked;
});
File croppedFile = await ImageCropper.cropImage(
sourcePath: image_state.path,
ratioX: 1.0,
ratioY: 1.0,
toolbarTitle: 'Cropper',
toolbarColor: Colors.blue,
);
if (croppedFile != null) {
image_state = croppedFile;
setState(() {
state = AppState.cropped;
});
} }
is there some one who work with this librairy? can you show me an example that work please?
issue
There are example of usages inside the plugin's repo.
It may be related to AndroidX compability. You have to avoid AndroidX or migrate your project to AndroidX.
There were a breaking change after version 0.5.0