我有这个问题。我正在使用此代码来旋转图像,但旋转后的图像由于旋转而在其角上有黑色填充。 我怎样才能删除它?
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;
}
最佳答案
您需要创建一个透明图像:
BufferedImage buffer = gc.createCompatibleImage(高度, 宽度, Transparency.TRANSLUCENT);
其中“gc”是 Graphics2D 对象。当然,您也可以直接使用 new BufferedImage() 创建一个图像,但这将为您的特定图形上下文提供最有效使用的图像。
关于java - java中用图像填充黑色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16925848/