-3

对于裁剪图像,我使用的是 joshholtz 的 CropImageView ( https://github.com/joshdholtz/CropImageView )。但是我在返回行之前的最后一行的裁剪函数处的 CreateBitmap 函数处收到 IllegalArgumentException 异常(说:x + 宽度必须是 <= bitmap.width())。

public Bitmap crop(Context context) throws IllegalArgumentException {
        // Weird padding cause image size
        int weirdSidePadding = this.getWeirdSideMargin();
        int weirdVerticalPadding = this.getWeirdVerticalMargin();

        FrameLayout.LayoutParams params = (LayoutParams) mDaBox.getLayoutParams();

        // Getting crop dimensions
        float d = context.getResources().getDisplayMetrics().density;
        int x = (int)((params.leftMargin - weirdSidePadding) * d);
        int y = (int)((params.topMargin - weirdVerticalPadding) * d);
        int width = (int)((this.getWidth() - params.leftMargin - params.rightMargin) * d);
        int height = (int)((this.getHeight() - params.topMargin - params.bottomMargin) * d);

        Bitmap crooopppppppppppppppeed = Bitmap.createBitmap(mBitmap, x, y, width, height);

        return crooopppppppppppppppeed;
}

实际上,我查看了可能相同的问题,但不幸的是,它们与我的情况不同,以帮助我。

你能帮我克服这个障碍吗?

4

1 回答 1

0

因此,在 createBitmap 中,您使用的函数从原始位图的子部分创建位图。要使其正常工作,x+width 不得大于原始位图的宽度(y+height 相同)并且 x 和 y 都必须 >=0。这不是这里的情况。

我想我明白了这个函数试图做什么,但它是错误的。它似乎混淆了裁剪和缩放的概念。如果要一次缩放和裁剪位图,则应使用 createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter) 和矩阵 m 中的比例因子。而且我不确定您是否想在这里进行缩放-我不知道为什么代码会查看密度,但是这样做可能是错误的。

于 2017-11-13T02:10:40.353 回答