0

以下は、画像を含むファイルの入力を受け取り、それを角度で傾ける小さなコードです。問題は、出力ファイルの解像度が入力ファイルに比べて低いことです。私の場合、入力ファイルのサイズは 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;
}
4

2 に答える 2

1

Thats no surprise if you look at the code (Copy&Paste without understanding what the Code does has its drawbacks). The tilt()-Method makes extra effort (in its 3rd line) to make the image properly sized.

If you think about it, you cant expect the image to stay the same size.

于 2012-08-24T10:25:45.573 に答える
0

結果の画像が元の画像と同じカラー モデルを持たない可能性があります。

gc.createCompatibleImage(...)

GraphicsConfiguration が関連付けられているデバイスと互換性のあるカラー モデルを持つ BufferedImage を作成しています。これにより、画像のサイズが小さくなる可能性があります。

ImageIO も、元の圧縮アルゴリズムとは異なる圧縮アルゴリズムを適用している可能性があります。

于 2012-08-24T07:37:51.050 に答える