繁体   English   中英

在图像视图中裁剪后,如何获取从图库中选取的图像的宽度和高度

[英]How can I get the width and height of an Image picked from gallery after cropping in it Image view

我试图获取图像中图像集的确切高度宽度 ,使用裁剪将图像/图像放置在Image视图中。

那么,如何获得其精确选择的宽度和高度?

下面是我的代码

  1. 从图库中选择一个图像,然后进行裁剪。
private void SelectImageFromGallery(object sender, EventArgs e)
                {
                    new ImageCropper
                    {
                        CropShape = ImageCropper.CropShapeType.Rectangle,
                        Success = imageFile =>
                        {
                            Device.BeginInvokeOnMainThread(() =>
                        {
                            profile_img.Source = ImageSource.FromFile(imageFile);
                            var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

                            var pathFile = profile_img.Source
                                .ToString().Replace("/data/user/0", "/data/data");
                            File.Copy(imageFile, Path.Combine(folderPath, Path.GetFileName(pathFile)));

                        });
                    }
                }.Show(this);

            }
  1. 准备上传图片( 但我需要它的宽度和高度

     // Get the folder path of the cropped Image var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); // Convert it into bytes. var readAllBytes = File.ReadAllBytes(Path.Combine(folderPath, getPathName)); // ByteArrayContent. var baContent = new ByteArrayContent(readAllBytes); 

根据上面的代码,有什么方法可以获取图像的宽度高度

在Android环境中,一种方法是使用BitmapFactory类。

var options = new Android.Graphics.BitmapFactory.Options { InJustDecodeBounds = true };
Android.Graphics.BitmapFactory.DecodeFile(FULL_FILE_PATH_HERE, options);
Android.Util.Log.Info("MyApp", $"Width ={options.OutWidth}");
Android.Util.Log.Info("MyApp", $"Height={options.OutHeight}");

您可以尝试在此处使用代码来获取照片的宽度和高度:

在您的SelectImageFromGallery

private async void SelectImageFromGallery(object sender, EventArgs e)
        {
            new ImageCropper
            {
                CropShape = ImageCropper.CropShapeType.Rectangle,
                Success = imageFile =>
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {

                        ...

                        Size s = GetImageSize(imageFile);
                        Console.WriteLine(s);
                    });
                }
            }.Show(this);

        }

GetImageSize方法:

 public static Size GetImageSize(string fileName)
        {

            if (Device.RuntimePlatform == Device.iOS)
            {
                UIImage image = UIImage.FromFile(fileName);
                return new Size((double)image.Size.Width, (double)image.Size.Height);
            }
            else if (Device.RuntimePlatform == Device.Android)
            {
                var options = new BitmapFactory.Options
                {
                    InJustDecodeBounds = true
                };
                fileName = fileName.Replace('-', '_').Replace(".png", "");
                var resId = Android.App.Application.Context.Resources.GetIdentifier(
                    fileName, "drawable", Android.App.Application.Context.PackageName);
                BitmapFactory.DecodeResource(
                    Android.App.Application.Context.Resources, resId, options);
                return new Size((double)options.OutWidth, (double)options.OutHeight);
            }

            return Size.Zero;
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM