[英]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); BufferedImage buffer = gc.createCompatibleImage(height,width,Transparency.TRANSLUCENT);
where 'gc' is a Graphics2D object. 其中“ gc”是Graphics2D对象。 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.
当然,您也可以直接使用新的BufferedImage()创建一个图像,但这将为您的特定图形上下文提供最有效使用的图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.