2

我正在尝试旋转一个名为 pic 的 BufferImage 实例,当我尝试此操作时,它会调整大小、倾斜和裁剪图像,任何建议让它正常工作

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˚ 时它工作正常,所以我想知道问题是否出在图像的形状上?

4

1 回答 1

5

对于与 一起使用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;
}
于 2012-05-17T23:22:01.380 回答