I am developing an app using Flutter and I want to retrieve the exif orientation value of an image that I selected using the image_picker plugin.
When I run the code below and select an image that I have rotated beforehand, I get null for the orientation value and an empty Map for the exif data.
File file = await ImagePicker.pickImage(
source: ImageSource.gallery,
);
img.Image decodedImage = img.decodeImage(file.readAsBytesSync());
print("decodedImage.exif.orientation ${decodedImage.exif.orientation}"); // null
print("decodedImage.exif.data ${decodedImage.exif.data}"); // {}
I send the image that I used with this code to my mac book through google drive to see the orientation value and this is what I got:
I am using the newest available version of the image_picker plugin. How can I retrieve the exif orientation value of an image in Flutter?
I used this function:
needRotation(String path) async {
Map<String, IfdTag> data =
await readExifFromBytes(await new File(path).readAsBytes());
return data['EXIF ExifImageWidth'].values[0] >
data['EXIF ExifImageLength'].values[0];
}
using import 'package:exif/exif.dart';
Explain: given the path of an image it will return if need rotation (i.e. is in landscape mode)
I also hadthe null orientation problem that u mentioned and uses the width/height to figure it out my self...
It seems weired I know... hope someone comes with a better solution.
Related
In my app, I am picking a photo from gallery and save it's path with ImagePicker. Then, I am showing it with the path I saved. The problem is, ImagePicker is giving me a different path every time even if I choose the same picture again so I can not open the image with the path I saved, it's giving the error no such file. How I am getting the path is:
onPressed: () async {
final XFile? image =
await imagePicker.pickImage(source: ImageSource.gallery);
if (image != null) {
setState(() {
imagePath = image.path;
});
}
},
It gives me a path like this: "/private/var/mobile/Containers/Data/Application/..." so on. How can I get the actual path and open it?
ImagePicker will create a copy of the image to your app's cache directory, thus you need to move it into a more stable location.
It also has no knowledge of what was picked before, thus it will create a new image every time you pick it. If you need to have a stable file path based on the image, perhaps give a try to file_picker.
My code is simle;
final XFile image=await cameraController!.takePicture();
takenImage=await image.readAsBytes();
and when I am just using this two line, I can see the image with Image.memory(_vm.takenImage!, fit: BoxFit.fitWidth)
But i need to change picture rotation,
for this reason, I am using image package https://pub.dev/packages/image
so, why I am getting invalid image data after just these lines;
final XFile image=await cameraController!.takePicture();
takenImage=await image.readAsBytes();
img.Image? decodedImage=img.decodeImage(takenImage!);
takenImage=decodedImage!.getBytes();
If I delete the last two sentence it's working correctlye but if I add the last lines I am getting Exceltion: invalid image data, but why ? I mean .getBytes returning Uint8List, takenImage type is Uint8List? ...
if I can convert like my second code review, I can use the; img.copyRotate(originalImage!, 90).getBytes(); function, so please help me for understand.
For my problem related image type cause as you can see on question I wasn't encode my image with any type, so the solution and working code now can seen on below;
final XFile image=await cameraController!.takePicture();
img.Image? _capturedImage=img.decodeImage(await image.readAsBytes());
_capturedImage=img.copyRotate(_capturedImage!, 90);
takenImage=Uint8List.fromList(img.encodePng(_capturedImage));
I'm using the Flutter SDK Version 4.3.1.0.
I would like to display markers on the map and I found that this would be possible via:
final String positionMarkerPath = 'assets/markers/marker.png';
mapScene.addMapMarker(
MapMarker(
geoCoordinates,
MapImage.withFilePathAndWidthAndHeight(positionMarkerPath, 32, 32)));
When using this method I constantly get errors
E/CL_geoviz( 9632): [ERROR] CL_geoviz - Can't find image 'file:///assets/markers/marker.png' in asset repository.
E/CL_geoviz( 9632): [ERROR] CL_geoviz - Can't load image file 'file:///assets/markers/marker.png' to memory stream.
Now, the method MapImage.withFilePathAndWidthAndHeight is documented as follows in the SDK:
Creates a new map image from the provided path to the SVG Tiny image which is weird because I thought that Flutter doesn't even support SVG out of the box. Could that be an issue? Or what am I doing wrong here?
I tried using an SVG, Vector Drawable and a png, tried fully qualifying the path, nothing works
The documentation for MapImage.withFilePathAndWidthAndHeight says that it is only for SVG Tiny format. But it currently does not seem to work as the HERE SDK for Flutter is still in Beta state.
You can instead load and add PNG files as marker like this:
ByteData data = await rootBundle.load('assets/some_image.png');
MapImage image = MapImage.withPixelDataAndImageFormat(Uint8List.view(data.buffer), ImageFormat.png);
MapMarker marker = MapMarker(geoCoordinates, image);
hereMapController.mapScene.addMapMarker(marker);
The assets directory needs to be specified in the pubspec.yaml.
I'm previewing a camera with a CaptureElement in UWP. But why do i get the error: "No suitable transform was found to encode or decode" when using certain resolutions? It happens when using a webcam
I don't get the error when i use this method: mediacapture.VideoDeviceController.SetMediaStreamPropertiesAsync(...). but if i use this method, the captureelement doesn't get scaled to the resolution i want (capture element has it's strech attribute set to none because i don't want quality loss)
try
{
MediaCaptureInitializationSettings mediacapturesettings = new MediaCaptureInitializationSettings { VideoDeviceId = DeviceInfo.Id };
await mediacapture.InitializeAsync(mediacapturesettings);
}
catch (Exception exception)
{
var dialog = new MessageDialog(exception.Message);
await dialog.ShowAsync();
}
captureelement.Source = mediacapture;
captureelement.FlowDirection = Windows.UI.Xaml.FlowDirection.LeftToRight;
await mediacapture.StartPreviewAsync();
// await mediacapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, Resolution.EncodingProperties);
await mediacapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, Resolution.EncodingProperties,null);
VideoRotation currentrotation = Rotation;
mediacapture.SetPreviewRotation(currentrotation);
I don't get the error when i use this method: mediacapture.VideoDeviceController.SetMediaStreamPropertiesAsync(...). but if i use this method, the captureelement doesn't get scaled to the resolution i want .
It only happens the first time i callstartpreview().
I've consulted internally and got a response. This problem is probably graphic driver related. Your graphic driver may require your camera device preview to be in running state before you call MediaCapture.SetEncodingPropertiesAsync, so you got this error and only got it at the first time.
i want to know the difference between the two methods.
Difference:
MediaCapture.SetEncodingPropertiesAsync set the property on the camera sink. You can set resolutions and rotation here that the camera does not support and then an encoder will do work to convert to desired format.
MediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync set the properties on the source. You can only set the configurations that the camera supports.
So the size of preview window is controlled by the Xaml CaptureElement. If you are using VideoDeviceController.SetMediaStreamPropertiesAsync and want to resize the preview window, you can only set the Stretch="UniformToFill" to let the preview window fill the parent element.
I have been really cracking my head trying to write and read png files into a folder in Windows Phone 8. From few blogs sites and codeplex i found that the there is an extension to the WritableBitmap Class which provides few extra functionalities. ImageTools has PNG encoder and decoder. But I really cant find examples to use them.
What Im trying to achieve here is to create a folder called page and then in it a file called Ink File. I want to convert the bitmap to a PNG and store it there. The bitmap is created from the strokes drawn on a canvas. The class ImageTools provides a function called ToImage to convert the strokes from the canvas to image.
For storing
ExtendedImage myImage = InkCanvas.ToImage();
var encoder = new PngEncoder();
var dataFolder = await local.CreateFolderAsync("Page", CreationCollisionOption.OpenIfExists);
StorageFile Ink_File = await dataFolder.CreateFileAsync("InkFile", CreationCollisionOption.ReplaceExisting);
using (var stream = await Ink_File.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite))
{
using (var s = await Ink_File.OpenStreamForWriteAsync())
{
encoder.Encode(myImage, s);
await s.FlushAsync();
s.Close();
}
}
Is this a correct method? I receive some null exceptions for this. How do i find if the image is saved as png. How is this image saved? Is it encoded and saved in a file or is it saved as a png itsef. And how do we read this back?
I have checked out this, this , this and lot more like this.
I'm developing app for WP8
I have used the PNG Writer Library found in ToolStack and it works :)