java - 旋转后缓冲图像颜色变化
问题描述
当我在 Java Web 应用程序中旋转缓冲区图像时,它的颜色变为红色,并且这只发生在保存为 JPEG 格式时。如果格式是 PNG,它可以工作
代码..
public BufferedImage rotateImageByDegrees(BufferedImage img, double angle, int width, int height) {
double rads = Math.toRadians(angle);
double sin = Math.abs(Math.sin(rads)), cos = Math.abs(Math.cos(rads));
int w = img.getWidth();
int h = img.getHeight();
int newWidth = (int) Math.floor(w * cos + h * sin);
int newHeight = (int) Math.floor(h * cos + w * sin);
BufferedImage rotated = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = rotated.createGraphics();
AffineTransform at = new AffineTransform();
at.translate((newWidth - w) / 2, (newHeight - h) / 2);
int x = w / 2;
int y = h / 2;
at.rotate(rads, x, y);
g2d.setTransform(at);
g2d.drawImage(img, null, 0, 0);
g2d.setColor(Color.RED);
g2d.drawRect(0, 0, newWidth - 1, newHeight - 1);
g2d.dispose();
return rotated;
}
解决方案
推荐阅读
- oracle - PLS-00371 和 ORA-06550 在变量声明期间
- wordpress - 立即下载失败并使用 Wordpress 为 Google Cloud Console 提供此消息(失败 - 服务器问题)
- anylogic - 使用 AnyLogic 进入队列时,agent.goToPopulation 不起作用
- python - ValueError:在回归问题上训练 VGG16 模型以生成边界框坐标时,使用序列设置数组元素
- android - Android Studio (4.1.0 版) ✗ 未安装 Flutter 插件;这增加了 Flutter 特定的功能
- python-3.x - 显示每周交付量最多的国家/地区的问题
- azure-web-app-service - Azure App Service Web App Deploy from VScode“中央目录末尾预期的条目数不...”
- shell - 如何通过 shell 使用 openssl 库从 JWKS 中提取公钥
- list - 命令行/脚本列出 C:\users 中的所有用户配置文件
- android - 始终使用高 API 级别可以吗?