0

我正在使用以下代码来旋转给定的图像。

public int getImageWidth(BufferedImage img) {
        if (rotate == Rotate.UPSIDE_DOWN || rotate == Rotate.ABOUT_CENTER)
            return img.getWidth();
        else
            return img.getHeight();
    }





public int getImageHeight(BufferedImage img) {

        if (rotate == Rotate.UPSIDE_DOWN || rotate == Rotate.ABOUT_CENTER)
            return img.getHeight();
        else
            return img.getWidth();

    }

这是旋转枚举

public enum Rotate {
        DOWN, UP, UPSIDE_DOWN, ABOUT_CENTER;
    }

实际旋转方式

public BufferedImage rotateImage(BufferedImage source, int x, int y,
            float orientation) throws Exception {

        int newWidth = getImageWidth(source);
        int newHeight = getImageHeight(source);
        int cWidth = newWidth / 2;
        int cHeight = newHeight / 2;
        int imgType = source.getType() == 0 ? 5 : source.getType();

        BufferedImage result = new BufferedImage(getImageWidth(source),
                getImageHeight(source),imgType);
        Graphics2D g2 = result.createGraphics();

        if (rotate == Rotate.DOWN) {
            g2.translate(x + cHeight, y + cWidth);
            g2.rotate(Math.toRadians(90));
            g2.drawImage(source, 0, 0, newWidth, newHeight, null);
        } else if (rotate == Rotate.UP) {
            g2.translate(x + cHeight, y + cWidth);
            g2.rotate(Math.toRadians(-90));
            g2.drawImage(source, 0, 0, newWidth, newHeight, null);
        } else if (rotate == Rotate.UPSIDE_DOWN) {
            g2.translate(x + cWidth, y + cHeight);
            g2.rotate(Math.toRadians(180));
            g2.drawImage(source, 0, 0, newWidth, newHeight, null);
        } else if (rotate == Rotate.ABOUT_CENTER) {
            Rectangle r = new Rectangle(x, y, newWidth, newHeight);
            g2.setClip(r);
            AffineTransform original = g2.getTransform();
            AffineTransform at = new AffineTransform();
            at.concatenate(original);
            at.rotate(orientation, x + cWidth, y + cHeight);
            g2.setTransform(at);
            g2.drawImage(source, 0, 0, newWidth, newHeight, null);
            g2.setTransform(original);
        }

        g2.dispose();
        g2 = null;

        return result;

    }

客户端代码

// rotate derived & filtered image to 90 degree
        // using Affine transform
        setRotate(Rotate.UP);

        BufferedImage rSubImage = rotateImage(fSubImage, 0, 0, -90);

现在以下是源图像,

源图像

当我使用上面的代码旋转这个图像时,结果很奇怪

错误的旋转图像

我做错了什么 ?

旋转后质量也会丢失,请注意。

4

1 回答 1

0

我认为解决方案是使用 AffineTranform,将图像转换为中心。我修改了您的代码并对其进行了测试,对我来说它有效:

public BufferedImage rotateImage(BufferedImage source, int x, int y,
        float orientation) throws Exception {

    int newWidth = getImageWidth(source);
    int newHeight = getImageHeight(source);
    int imgType = source.getType() == 0 ? 5 : source.getType();

    BufferedImage result = new BufferedImage(getImageWidth(source),
            getImageHeight(source), imgType);

    if (rotate == Rotate.DOWN) {

        AffineTransform tranform = new AffineTransform();
        tranform.translate(newWidth / 2, newHeight / 2);
        tranform.rotate(Math.toRadians(90));
        tranform.translate(-source.getWidth()/2, -source.getHeight()/2);
        Graphics2D g2d = result.createGraphics();
        g2d.drawImage(source, tranform, null);

    } else if (rotate == Rotate.UP) {

        AffineTransform tranform = new AffineTransform();
        tranform.translate(newWidth / 2, newHeight / 2);
        tranform.rotate(Math.toRadians(-90));
        tranform.translate(-source.getWidth()/2, -source.getHeight()/2);
        Graphics2D g2d = result.createGraphics();
        g2d.drawImage(source, tranform, null);

    } else if (rotate == Rotate.UPSIDE_DOWN) {

        AffineTransform tranform = new AffineTransform();
        tranform.translate(newWidth / 2, newHeight / 2);
        tranform.rotate(Math.toRadians(180));
        tranform.translate(-source.getWidth()/2, -source.getHeight()/2);
        Graphics2D g2d = result.createGraphics();
        g2d.drawImage(source, tranform, null);


    } else if (rotate == Rotate.ABOUT_CENTER) {

        //......

    }



    return result;

}

希望能帮助到你!

于 2013-08-23T08:10:00.300 回答