Adds an image cropper to force the use to use a square image when taking picture
This commit is contained in:
@ -7,73 +7,97 @@ import 'package:path_provider/path_provider.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:uuid/uuid.dart';
|
||||
import 'package:image/image.dart' as img;
|
||||
import 'package:image_cropper/image_cropper.dart' as img_cropper;
|
||||
import 'package:uuid/v5.dart';
|
||||
|
||||
class ImagesManager
|
||||
{
|
||||
static final ImagesManager instance = ImagesManager._internal();
|
||||
ImagesManager._internal();
|
||||
class ImagesManager {
|
||||
static final ImagesManager instance = ImagesManager._internal();
|
||||
ImagesManager._internal();
|
||||
|
||||
static final Logger log = Logger("ImagesManager");
|
||||
static final Logger log = Logger("ImagesManager");
|
||||
|
||||
static Uri? _imagesDirectory;
|
||||
static Uri? _imagesDirectory;
|
||||
|
||||
Future<Uri> get imageDirectory async
|
||||
{
|
||||
if(_imagesDirectory != null) return _imagesDirectory!;
|
||||
await _initImagesDirectory();
|
||||
return _imagesDirectory!;
|
||||
Future<Uri> get imageDirectory async {
|
||||
if (_imagesDirectory != null) return _imagesDirectory!;
|
||||
await _initImagesDirectory();
|
||||
return _imagesDirectory!;
|
||||
}
|
||||
|
||||
Future _initImagesDirectory() async {
|
||||
final directoryLoc = await getApplicationDocumentsDirectory();
|
||||
final directoryName = "images";
|
||||
final directoryPath = path.join(directoryLoc.path, directoryName);
|
||||
final directoryUri = Uri.directory(directoryPath);
|
||||
final directory = Directory.fromUri(directoryUri);
|
||||
|
||||
if (!await directory.exists()) {
|
||||
log.info("Image directory does not yet extists. Creating it.");
|
||||
}
|
||||
|
||||
Future _initImagesDirectory() async
|
||||
{
|
||||
final directoryLoc = await getApplicationDocumentsDirectory();
|
||||
final directoryName = "images";
|
||||
final directoryPath = path.join(directoryLoc.path, directoryName);
|
||||
final directoryUri = Uri.directory(directoryPath);
|
||||
final directory = Directory.fromUri(directoryUri);
|
||||
|
||||
if(!await directory.exists())
|
||||
{
|
||||
log.info("Image directory does not yet extists. Creating it.");
|
||||
}
|
||||
directory.create(recursive: false);
|
||||
|
||||
directory.create(recursive: false);
|
||||
|
||||
log.info("Image directory set up at '$directory'");
|
||||
|
||||
_imagesDirectory = directoryUri;
|
||||
log.info("Image directory set up at '$directory'");
|
||||
|
||||
_imagesDirectory = directoryUri;
|
||||
}
|
||||
|
||||
Future<String?> createImage(ImageSource source) async {
|
||||
// Get image from camera or not
|
||||
final XFile? ximage = await ImagePicker().pickImage(source: source);
|
||||
if (ximage == null) return null;
|
||||
|
||||
// Crop image
|
||||
final uuid = Uuid().v6();
|
||||
final imageDir = await imageDirectory;
|
||||
final finalPath = path.join(
|
||||
imageDir.path,
|
||||
uuid + path.extension(ximage.name),
|
||||
);
|
||||
final tempPath = path.join(
|
||||
imageDir.path,
|
||||
"${uuid}_tocrop${path.extension(ximage.name)}",
|
||||
);
|
||||
await ximage.saveTo(tempPath);
|
||||
|
||||
img_cropper.CroppedFile? cropped = await img_cropper.ImageCropper()
|
||||
.cropImage(
|
||||
sourcePath: tempPath,
|
||||
aspectRatio: img_cropper.CropAspectRatio(ratioX: 1.0, ratioY: 1.0),
|
||||
uiSettings: [
|
||||
img_cropper.AndroidUiSettings(
|
||||
toolbarTitle: "Crop image",
|
||||
toolbarColor: Colors.black,
|
||||
toolbarWidgetColor: Colors.white,
|
||||
),
|
||||
img_cropper.IOSUiSettings(title: "Crop image"),
|
||||
],
|
||||
);
|
||||
|
||||
if (cropped == null) return null;
|
||||
|
||||
await File(finalPath).writeAsBytes(await cropped.readAsBytes());
|
||||
await File(tempPath).delete();
|
||||
|
||||
return uuid;
|
||||
}
|
||||
|
||||
Future<Image?> loadImage(String imageUuid) async {
|
||||
final imageDir = await imageDirectory;
|
||||
if (!Uuid.isValidUUID(fromString: imageUuid)) {
|
||||
log.warning(
|
||||
"Tried to load an image with an invalid UUID : '$imageUuid'.",
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<String?> createImage(ImageSource source) async
|
||||
{
|
||||
// Get image from camera or not
|
||||
final XFile? ximage = await ImagePicker().pickImage(source: source);
|
||||
if(ximage == null) return null;
|
||||
|
||||
final uuid = Uuid().v6();
|
||||
final imageDir = await imageDirectory;
|
||||
ximage.saveTo(path.join(imageDir.path, uuid + path.extension(ximage.name)));
|
||||
return uuid;
|
||||
final imagePath = path.join(imageDir.path, "$imageUuid.jpg");
|
||||
final file = File(imagePath);
|
||||
if (!await file.exists()) {
|
||||
log.warning("Tried to load an image that does not extist: '$imagePath'.");
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<Image?> loadImage(String imageUuid) async
|
||||
{
|
||||
final imageDir = await imageDirectory;
|
||||
if(!Uuid.isValidUUID(fromString: imageUuid))
|
||||
{
|
||||
log.warning("Tried to load an image with an invalid UUID : '$imageUuid'.");
|
||||
return null;
|
||||
}
|
||||
|
||||
final imagePath = path.join(imageDir.path, "$imageUuid.jpg");
|
||||
final file = File(imagePath);
|
||||
if(!await file.exists())
|
||||
{
|
||||
log.warning("Tried to load an image that does not extist: '$imagePath'.");
|
||||
return null;
|
||||
}
|
||||
|
||||
return Image.file(file);
|
||||
}
|
||||
return Image.file(file);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user