这个问题可能是因为输入图像的高度或宽度为0而导致的。因此,需要添加一些逻辑来检查输入图像的宽度和高度,确保它们大于0。以下是一个示例方法:
public static Bitmap scaleBitmap(Bitmap bitmap, int newWidth, int newHeight) {
if(bitmap == null) {
return null;
}
int width = bitmap.getWidth();
int height = bitmap.getHeight();
if(width == 0 || height == 0) {
return null;
}
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false);
}