我正在使用 .getRGB() 和 .setRGB() 来获取 BufferedImage 的一部分,如何复制透明度?
我使用以下代码来“裁剪图像”,但是它忽略透明度,因此从此方法获得的任何 BufferedImage 都是完全不透明的,并且似乎没有任何 .getARGB() 或 .setARGB() 方法。我该如何解决这个问题?
private static BufferedImage getCroppedImage(BufferedImage wholeImage, int xPos, int yPos, int width, int height)
{
GraphicsEnvironment graphEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
BufferedImage croppedImage = null;
try
{
GraphicsDevice screen = graphEnv.getDefaultScreenDevice();
GraphicsConfiguration gc = screen.getDefaultConfiguration();
croppedImage = gc.createCompatibleImage(width, height, Transparency.BITMASK);
}
catch (Exception e)
{
new errorWindow(e, "crop, in Images");
}
if (croppedImage == null)
{
croppedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
}
int[] pixels = new int[width * height];
wholeImage.getRGB(xPos, yPos, width, height, pixels, 0, width);
croppedImage.setRGB(0, 0, width, height, pixels, 0, width);
return croppedImage;
}
I am using the following code to "crop an image", however it ignores transparency, so any BufferedImages obtained from this method are completely opaque, and there don't appear to be any .getARGB() or .setARGB() methods. How do I work around this?
private static BufferedImage getCroppedImage(BufferedImage wholeImage, int xPos, int yPos, int width, int height)
{
GraphicsEnvironment graphEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
BufferedImage croppedImage = null;
try
{
GraphicsDevice screen = graphEnv.getDefaultScreenDevice();
GraphicsConfiguration gc = screen.getDefaultConfiguration();
croppedImage = gc.createCompatibleImage(width, height, Transparency.BITMASK);
}
catch (Exception e)
{
new errorWindow(e, "crop, in Images");
}
if (croppedImage == null)
{
croppedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
}
int[] pixels = new int[width * height];
wholeImage.getRGB(xPos, yPos, width, height, pixels, 0, width);
croppedImage.setRGB(0, 0, width, height, pixels, 0, width);
return croppedImage;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
Transparency.TRANSLUCENT
相反。这不会忽略 alpha 值。Use
Transparency.TRANSLUCENT
instead. This will not ignore alpha values.