Java的旋转的ImageBuffer的失败(Java rotating an ImageBuffe

2019-06-24 12:53发布

我想,当我尝试这一点,调整大小和倾斜和裁剪图像旋转BufferImage名为PIC的一个实例,任何建议得到它正常工作

public void rotate(double rads){
    AffineTransform tx = new AffineTransform();
    tx.rotate(rads,pic.getWidth()/2,pic.getHeight()/2);
    AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
    pic = op.filter(pic, null);
}

当我把它旋转90˚正常工作,所以我想知道如果这个问题是,它是图像的形状?

Answer 1:

对于使用AffineTransform ,你可以使用这样的正方形图片:

private BufferedImage getImage(String name) {
    BufferedImage image;
    try {
        image = ImageIO.read(new File(name));
    } catch (IOException ioe) {
        return errorImage;
    }
    int w = image.getWidth();
    int h = image.getHeight();
    int max = Math.max(w, h);
    max = (int) Math.sqrt(2 * max * max);
    BufferedImage square = new BufferedImage(
            max, max, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = square.createGraphics();
    g2d.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.drawImage(image, (max - w) / 2, (max - h) / 2, null);
    g2d.dispose();
    return square;
}


文章来源: Java rotating an ImageBuffer fails