简体   繁体   中英

Black Padding with images in java?

I have this problem. I am using this code to rotate an image but the rotated image has black padding in its corners due to rotation. How could I remove it?

public static BufferedImage rotate(BufferedImage img, int angle) {
        rotate_checked = false;
        int w = img.getWidth();
        int h = img.getHeight();
        BufferedImage dimg =new BufferedImage(w, h,  BufferedImage.TYPE_BYTE_GRAY);
        Graphics2D g = dimg.createGraphics();
        g.rotate(Math.toRadians(angle), w/2, h/2);
        g.drawImage(img, null, 0, 0);
       return dimg;
} 

You need to create a transparent image:

BufferedImage buffer = gc.createCompatibleImage(height, width, Transparency.TRANSLUCENT);

where 'gc' is a Graphics2D object. You can also create one directly with new BufferedImage() of course, but this will give you the most efficient-to-use image for your particular graphics context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM