簡體   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