下面是一个小代码,它输入包含图像的文件,然后将其倾斜一个角度。现在的问题是:与输入文件相比,输出文件的分辨率较低。在我的例子中,输入文件大小为 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;
}