【问题标题】:How to crop the rotated image in xamarin Android?如何在 xamarin Android 中裁剪旋转的图像?
【发布时间】:2020-10-26 16:03:28
【问题描述】:

我的要求是在旋转后裁剪图像。

示例:https://github.com/SanthiyaArulsamy/Samples/tree/master/RotationSample

我的真实形象:

旋转后:

在这里,我将图像旋转了 45 度。现在我只想裁剪突出显示的(红色矩形)矩形区域。

代码:

        var cropWindowRect = new System.Drawing.RectangleF(100, 100, 500, 500);

        var points = new float[8]
        {
                cropWindowRect.Left, cropWindowRect.Top,
                cropWindowRect.Right, cropWindowRect.Top,
                cropWindowRect.Right, cropWindowRect.Bottom,
                cropWindowRect.Left, cropWindowRect.Bottom
        };
        this.imageView.SetImageBitmap(GetCroppedBitmap(imageBitmap, points));
        this.imageView.SetScaleType(ImageView.ScaleType.FitCenter);
        this.imageView.Rotation = 0;

  private static Rect GetRect(float[] points, int imagewidth, int imageheight)
    {
        int left = (int)Math.Round(Math.Max(0, Math.Min(Math.Min(Math.Min(points[0], points[2]), points[4]), points[6])));
        int top = (int)Math.Round(Math.Max(0, Math.Min(Math.Min(Math.Min(points[1], points[3]), points[5]), points[7])));
        int right = (int)Math.Round(Math.Min(imagewidth, Math.Max(Math.Max(Math.Max(points[0], points[2]), points[4]), points[6])));
        int bottom = (int)Math.Round(Math.Min(imageheight, Math.Max(Math.Max(Math.Max(points[1], points[3]), points[5]), points[7])));
        return new Rect(left, top, right, bottom);
    }
    private static Bitmap GetCroppedBitmap(Bitmap bitmap, float[] points)
    {
        Rect rect = GetRect(points, bitmap.Width, bitmap.Height);
        var height = rect.Height();
        var width = rect.Width();
        if (rect.Height() + rect.Top > bitmap.Height)
            height = bitmap.Height - rect.Top;
        if (rect.Width() + rect.Left > bitmap.Width)
            width = bitmap.Width - rect.Left;
        if (width > 0 && height > 0)
            return Bitmap.CreateBitmap(bitmap, rect.Left, rect.Top, width, height);
        else
            return bitmap;
    }

请建议我如何在旋转后找到图像坐标点。

【问题讨论】:

    标签: c# android xamarin.android image-rotation


    【解决方案1】:

    您可以裁剪屏幕视图并自定义它的框架。

    例如CustomShot方法如下:

    public Bitmap CustomShot(Activity activity, int x, int y, int w, int h)
    {
        // get the top view of windows
        View view = activity.Window.DecorView;
        view.BuildDrawingCache();
    
        // get the height of status bar
        Rect rect = new Rect();
        view.GetWindowVisibleDisplayFrame(rect);
        int statusBarHeights = rect.Top;
        Display display = activity.WindowManager.DefaultDisplay;
    
        // get the width and height of screen
        if (w == 0)
        {
            w = display.Width;
        }
           
        int heights = display.Height;
    
        // allow cache
        view.DrawingCacheEnabled = true;
    
        // custom the crop frame
        Bitmap bmp = Bitmap.CreateBitmap(view.GetDrawingCache(true), x,
                y, w, h);
    
        // clear cache
        view.DestroyDrawingCache();
    
        return bmp;
    }
    

    然后在Cropbutton_Click方法中使用如下:

    private void Cropbutton_Click(object sender, System.EventArgs e)
    {
        if(imageBitmap == null){
            imageBitmap = ViewToBitmap();
            this.imageView.SetImageBitmap(GetCroppedBitmap(imageBitmap, points));
        }else{
            this.imageView.SetImageBitmap(CustomShot( this, 0, 300, 0, 500));
            this.imageView.SetScaleType(ImageView.ScaleType.FitCenter);
            this.imageView.Rotation = 0;
        }
    }
    

    效果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-05
      • 1970-01-01
      • 2015-02-18
      • 1970-01-01
      相关资源
      最近更新 更多