我正在尝试编写一个方法,该方法将位图和裁剪值作为参数并返回裁剪后的位图。
我的代码:
public Bitmap applyCrop(Bitmap bitmap, int leftCrop, int topCrop, int rightCrop, int bottomCrop) {
return Bitmap.createBitmap(bitmap, leftCrop, topCrop, bitmap.getWidth() - rightCrop, bitmap.getHeight() - bottomCrop);
}
使用此代码我收到以下IllegalArgumentException
:
java.lang.IllegalArgumentException: x + width must be <= bitmap.width()
我的代码有什么问题吗?
最佳答案
如果 Bitmap.createBitmap()
正在获取裁剪图像的大小而不是第二个角的坐标,您应该这样做:
public Bitmap applyCrop(Bitmap bitmap, int leftCrop, int topCrop, int rightCrop, int bottomCrop) {
int cropWidth = bitmap.getWidth() - rightCrop - leftCrop;
int cropHeight = bitmap.getHeight() - bottomCrop - topCrop;
return Bitmap.createBitmap(bitmap, leftCrop, topCrop, cropWidth, cropHeight);
}
关于java - 如何裁剪位图的四个边?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35623652/