gpt4 book ai didi

java - 图像尺寸变小

转载 作者:行者123 更新时间:2023-12-01 15:12:43 26 4
gpt4 key购买 nike

下面是一个小代码,它接受包含图像的文件的输入,然后将其倾斜一定角度。现在的问题是:与输入文件相比,输出文件的分辨率较低。在我的例子中,输入文件大小为 5.5 MB,输出文件大小为 1.1 MB。这是为什么?

/**
*
* @param angle Angle to be rotate clockwise. Ex: Math.PI/2, -Math.PI/4
*/
private static void TurnImageByAngle(File image, double angle)
{
BufferedImage original = null;
try {
original = ImageIO.read(image);
GraphicsConfiguration gc = getDefaultConfiguration();
BufferedImage rotated1 = tilt(original, angle, gc);
//write iamge
ImageIO.write(rotated1, getFileExtension(image.getName()), new File("temp"+" "+angle+"."+getFileExtension(image.getName())));
} catch (IOException ex) {
Logger.getLogger(RotateImage2.class.getName()).log(Level.SEVERE, null, ex);
}
}

public static GraphicsConfiguration getDefaultConfiguration() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
return gd.getDefaultConfiguration();
}

public static BufferedImage tilt(BufferedImage image, double angle, GraphicsConfiguration gc) {
double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
int w = image.getWidth(), h = image.getHeight();
int neww = (int)Math.floor(w*cos+h*sin), newh = (int)Math.floor(h*cos+w*sin);
int transparency = image.getColorModel().getTransparency();
BufferedImage result = gc.createCompatibleImage(neww, newh, transparency);
Graphics2D g = result.createGraphics();
g.translate((neww-w)/2, (newh-h)/2);
g.rotate(angle, w/2, h/2);
g.drawRenderedImage(image, null);
return result;
}

最佳答案

如果您查看代码,这并不奇怪(在不了解代码的作用的情况下进行复制和粘贴其缺点)。倾斜()方法付出了额外的努力(在其第三行)以使图像尺寸正确。

如果您考虑一下,您不能期望图像保持相同的大小。

关于java - 图像尺寸变小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12104928/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com