java - 如何裁剪位图的四个边?

标签 java android bitmap android-bitmap

我正在尝试编写一个方法,该方法将位图和裁剪值作为参数并返回裁剪后的位图。

我的代码:

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/

相关文章:

android - 布局动画 : how to not animate entering views?

c# - 将字节分配给 RGB 颜色

java - 'main event loop' 在旧版 Java 应用程序中看起来如何 [JCIP 9.1]

java - JNI EXCEPTION_ACCESS_VIOLATION (0xc0000005) msvcr100.dll+0x1ed7 仅适用于 Windows

java - 将 war 部署到 Tomcat 时生成数据库

未授予 Android 权限

java - Apache Commons 文件上传 - 上传后文件损坏

java - AlarmManager 从不在 AlarmReceiver/BroadcastReceiver 中调用 onReceive

android - 将位图传递给另一个 Activity 以 RunTimeException 结束

android - 从 Android Canvas 获取像素值