java - 当我缩放图像时,我会在它上面得到一个红色层。并非所有图像都发生这种情况
问题描述
图像尺寸要求:
private static final int WIDTH = 640;
private static final int HEIGHT = 480;
缩放的主要方法:
public byte[] getScaledCustomImageByteArray(byte[] imageBytes, int width, int height) throws IOException {
if (imageBytes == null) {
return null; //NOSONAR
}
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(imageBytes)) {
BufferedImage before = ImageIO.read(byteArrayInputStream);
if (before == null) {
throw new ImageProcessingException("ByteArray is not an image!");
}
BufferedImage after = getScaledInstance(before, width, height);
ImageIO.write(after, "png", byteArrayOutputStream);
byteArrayOutputStream.flush();
return byteArrayOutputStream.toByteArray();
}
}
这种方法可以正常工作,它可以保持比例。
private Dimension getScaledDimension(Dimension imgSize, Dimension boundary) {
int originalWidth = imgSize.width;
int originalHeight = imgSize.height;
int boundWidth = boundary.width;
int boundHeight = boundary.height;
int newWidth = originalWidth;
int newHeight = originalHeight;
if (originalWidth > boundWidth) {
newWidth = boundWidth;
newHeight = (newWidth * originalHeight) / originalWidth;
}
if (newHeight > boundHeight) {
newHeight = boundHeight;
newWidth = (newHeight * originalWidth) / originalHeight;
}
return new Dimension(newWidth, newHeight);
}
这是我在调整图像大小时无法正确理解的问题。
private BufferedImage getScaledInstance(BufferedImage image, int maxWidth, int maxHeight) {
int width = image.getWidth();
int height = image.getHeight();
// if the image is already scaled, no further scaling is needed
if (width <= maxWidth && height <= maxHeight) {
return image;
}
int type = (image.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB :
BufferedImage.TYPE_INT_ARGB;
BufferedImage result = image;
BufferedImage scratchImage = null;
Graphics2D graphics2D = null;
int prevWidth = result.getWidth();
int prevHeight = result.getHeight();
Dimension targetDimensions = getScaledDimension(new Dimension(image.getWidth(),
image.getHeight()), new Dimension(maxWidth, maxHeight));
int targetWidth = (int) targetDimensions.getWidth();
int targetHeight = (int) targetDimensions.getHeight();
do {
if (width > targetWidth) {
width /= 2;
if (width < targetWidth) {
width = targetWidth;
}
}
if (height > targetHeight) {
height /= 2;
if (height < targetHeight) {
height = targetHeight;
}
}
if (scratchImage == null) {
scratchImage = new BufferedImage(width, height, type);
graphics2D = scratchImage.createGraphics();
graphics2D.setComposite(AlphaComposite.Clear);
graphics2D.fillRect(0, 0, width, height);
}
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.setComposite(AlphaComposite.Src);
graphics2D.drawImage(result, 0, 0, width, height, 0, 0, prevWidth, prevHeight, null);
prevWidth = width;
prevHeight = height;
result = scratchImage;
} while (width != targetWidth || height != targetHeight);
graphics2D.dispose();
if (targetWidth != result.getWidth() || targetHeight != result.getHeight()) {
scratchImage = new BufferedImage(targetWidth, targetHeight, type);
graphics2D = scratchImage.createGraphics();
graphics2D.setComposite(AlphaComposite.Clear);
graphics2D.fillRect(0, 0, targetWidth, targetHeight);
graphics2D.setComposite(AlphaComposite.Src);
graphics2D.drawImage(result, 0, 0, null);
graphics2D.dispose();
result = scratchImage;
}
return result;
}
Google width 1856pixels && hegiht 886pixel 我有这个问题
但是当图像宽度为 750 像素 && 高度为 497 像素时,一切看起来都不错
遗留代码。我看不出问题出在哪里。
解决方案
推荐阅读
- r - 按条件填充数据框列中的空值
- python - 将包含多个 numpy 数组的列表转换为单个 numpy 数组
- apache-kafka - Kafka 升级路径从 0.10.0.1 到 2.5.0
- python - 从 acf 和 pacf python 确定 p,q
- python - 使用python进行复杂的字符串过滤
- react-native - 尝试注册两个同名的视图RNCWebView
- javascript - 通过在嵌套的 json 结构中复制 javascript 中特定键的值来附加键值对
- c# - C# Task.Run(() =>) 和 Task.Run(async() => await) 的区别
- apache - 如何将 .htaccess 转换为 web.config 文件
- c# - VS2017 C# 导入 WinForms 产生命名空间错误