Java rotating an ImageBuffer fails

后端 未结 1 2031
感情败类
感情败类 2020-12-21 12:09

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

         


        
相关标签:
1条回答
  • 2020-12-21 12:38

    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;
    }
    
    0 讨论(0)
提交回复
热议问题