Convert landscape image to potrait (rotating the image) and save - Flutter - flutter

I have an image that is in landscape mode.
This is the image.
Now I need to convert it to portrait. i.e it should be like this.
And the image should be saved in the app directory. (saving part I know.just to complete the question)
How can I do that in flutter.

I use the lib image (https://pub.dev/packages/image) and you can call this code on a button action :
import 'package:image/image.dart' as ImageLib;
try {
File contrastFile = File(imagePath);
ImageLib.Image contrast =
ImageLib.decodeImage(contrastFile.readAsBytesSync());
contrast = ImageLib.copyRotate(contrast, -90);
contrastFile.writeAsBytesSync(ImageLib.encodeJpg(contrast));
setState(() {
imageCache.clear();
imageCache.clearLiveImages();
reload();
});
} catch (e) {
print(e);
}

Related

flutter download automatic image and save it in assets

in my app,I want to use SVG images from the asset file.
but I want to check them in-the app, if the image asset is different from my image network from the server, I download the image network and replace it in my image asset and again read it from the asset to show them.
how can I do that?
bool showSvg = false;
void compareSvg(){
if(value['svgimage']['name'] != Image.profileSvg){
// how download svg network and replace it to assetfile
}else{
showSvg = true; });}
and its my code example :
showSvg
?SvgPicture.asset(Image.profileSvg)
:SvgPicture.asset(Image.newProfileSvg),
You can not compare images (technically you can, but it very complicated task) and replace assets file. You need to cache you image after first downloading, and set your asset as placeholder before your image are downloaded. Look in to dock here.
Edited:
Okay. I don't get your answer about svg, but if true here solution
Add this pub for file caching to your project. And this code may help:
File? _downloaded;
void _fetchImage() async {
const key = "profile";
const url =
"http://upload.wikimedia.org/wikipedia/commons/0/02/SVG_logo.svg";
final file = await DefaultCacheManager().getSingleFile(url, key: key);
setState(() {
_downloaded = file;
});
}
Widget _svgImageFetcher() {
_fetchImage();
Widget networkSvg;
if (_downloaded == null) {
networkSvg = SvgPicture.asset("assets/images/profile.svg");
} else {
networkSvg = SvgPicture.file(_downloaded!);
}
return Column(children: [Expanded(child: networkSvg)]);
}
Just put _svgImageFetcher() Widget where you want to display image. And change default svg image and url.

Is there a way to detect available applications to pick image like gallery , amazon photos , google photos ... using flutter image picker

I want to create an image picker like android native that detect all available applications to pick image using flutter image picker.
To be more specific I want to display the available image applications.
Hey Don't know about image_picker, but file_picker works for me as below
Future getFromGallery() async {
FilePickerResult? result =
await FilePicker.platform.pickFiles(type: FileType.image);
if (result != null) {
setState(() {
pickedImage = File(result.files.single.path!);
});
}
}

How to display Cover Image of an epub file in Flutter

The dart-epub plugin mentions an example on how to get Cover Image:
// Book's cover image (null if there is no cover)
Image coverImage = epubBook.CoverImage;
How can I display this? I keep getting an error
The following _TypeError was thrown building:
type 'Image' is not a subtype of type 'ImageProvider<Object>'
Update: I figured it out
Import the image and epub package as
import 'package:image/image.dart' as image;
import 'package:epub/epub.dart' as epub;
Open the epub file
// Change the location to wherever your epub file is located
var targetFile = new File('location/epubFile.epub');
List<int> bytes = await targetFile.readAsBytes();
epub.EpubBook epubBook = await epub.EpubReader.readBook(bytes);
Save the Cover Image to some location (ideally same folder as the epub file so it easier to delete together)
// Save the Cover Image to some location
if (await File('location/epubFileCoverImage.png').exists()) {
print("File exists");
} else {
try {
File('location/epubFileCoverImage.png')
.writeAsBytesSync(image.encodePng(epubBook.CoverImage));
} catch (e) {
print("Error Saving Cover Image");
print(e);
coverError = true;
}
}
Use Exception handling as there may be errors loading the Cover Image for some epub files
Finally to display it in the Widget tree:
coverError ? Image.asset("assets/Error.png")
: Image.file(File('location/epubFileCoverImage.png')),

Converting Image FlutterWebImagePicker Output to File

I'm using Flutter web for a webapp and having trouble converting an image from the image picker to a file in order to upload it to my server. I display the image in Image.file(xxx) but I get the error:
Error while trying to load an asset: FormatException: Illegal scheme
character (at character 6)
Image(image:%20MemoryImage(Uint8List%234267a,%20scale:%201),%20frameBuilder...
Here is the code I'm trying:
Future getImage(bool isCamera) async {
Image image;
if (isCamera) {
image = await FlutterWebImagePicker.getImage;
} else {
}
var bytes = await rootBundle.load('$image');
String tempPath = (await getTemporaryDirectory()).path;
File file = File('$tempPath/profile.png');
await file.writeAsBytes(
bytes.buffer.asUint8List(bytes.offsetInBytes, bytes.lengthInBytes));
setState(() {
currentSelfie = file;
_accDetails['customer_selfie'] = currentSelfie;
});
}
Thanks in advance
I've tested this package and was very happy with the result imagePickerWebit returns 3 different types it can be in the form of Image(widget for preview), byte, File(upload)
then you can use this to get the values
html.File _cloudFile;
var _fileBytes;
Image _imageWidget;
Future<void> getMultipleImageInfos() async {
var mediaData = await ImagePickerWeb.getImageInfo;
String mimeType = mime(Path.basename(mediaData.fileName));
html.File mediaFile =
new html.File(mediaData.data, mediaData.fileName, {'type': mimeType});
if (mediaFile != null) {
setState(() {
_cloudFile = mediaFile;
_fileBytes = mediaData.data;
_imageWidget = Image.memory(mediaData.data);
});
}
I havent used the plugin although your code has 2 issues. One is the if statement and the second one is using Rootbundle. If you are picking from the filesystem, my assumption isCamera would be false. You have not added any logic for the falsy condition.
if (isCamera) {// This would be true if the source was camera
image = await FlutterWebImagePicker.getImage;
} else {
}
Additionally,
var bytes = await rootBundle.load('$image');
From the flutter documentation, rootbundle contains the resources that were packaged with the application when it was built. Those are assets that you define in your pubspec. yaml. You are selecting an image at runtime hence its not bundled as an asset.
Since the package appears to return an image object, use the toByteData method on the image i.e
image = await FlutterWebImagePicker.getImage;
await image.toByteData();//This method has some parameters. Look into them

Flutter how to Convert filePath from content://media/external/images/media/5275 to /storage/emulated/0/DCIM/Camera/IMG_00124.jpg

I have URI LIKE below
content://media/external/images/media/5275
but I want convert it to Like format below:
/storage/emulated/0/DCIM/Camera/IMG_00124.jpg
Anyone who can help me!
Sreng Bona
Thanks!
var path = await FlutterAbsolutePath.getAbsolutePath("content://media/external/images/media/5275");
Add this flutter_absolute_path: ^1.0.6 to your file: pubspec.yaml
It will be working as Well.
Bona SR.
I had to search a lot, but I found a very useful solution to save the image in the cell phone's photo gallery and get the absolute path to use it the way you want.
For this example I used a result obtained from saving an image in the photo gallery, however it could also work to read other files, obviously making the changes that you see necessary, I hope it will help you
uri_to_file
image_gallery_saver
import 'dart:io';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:uri_to_file/uri_to_file.dart';
Future<File> saveImage(File image, String identifier) async {
try {
var result = await
ImageGallerySaver.saveImage(image.readAsBytesSync(),
quality: 60, name: identifier +
"-${DateTime.now().toIso8601String()}");
print(result);
File file = await toFile(Uri.parse(result['filePath']));
print(file);
return file;
} catch (e) {
print(e);
return new File('assets/img/default-img.jpg');
}
}
// example
saveImage(File("path/image.jpg"),"img-test");