Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
547 views
in Technique[技术] by (71.8m points)

swing - Java rotating an ImageBuffer fails

I am trying to rotate an instance of a BufferImage named pic when I try this it resizes and skews and crops the image, any advice to get it to work properly

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);
}

When I have it rotate 90? it works fine so I'm wondering if the problem is that it is the shape of the image?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For use with AffineTransform, you can square an image using something like this:

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...