[英]How do I create an image mask from a PNG with transparency in Java?
I need to create a Java class to create an image mask from a PNG image with transparency.我需要创建一个 Java 类来从具有透明度的 PNG 图像创建图像蒙版。 I would prefer to do this as much as possible with out-of-the box image processing libraries as possible.
我更愿意使用开箱即用的图像处理库尽可能多地做到这一点。
Original raster needs to be a byte-interleaved raster, with four bytes per pixel.原始光栅需要是字节交错光栅,每个像素有四个字节。 No safety checks are in place in this example.
本示例中没有进行安全检查。
BufferedImage orig = ImageIO.read(new File("temp.png"));
DataBuffer dataBuffer = orig.getRaster().getDataBuffer();
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
int[] nBits = {8};
int[] bOffs = {0};
ColorModel colorModel = new ComponentColorModel(cs, nBits, false, false,
Transparency.TRANSLUCENT,
DataBuffer.TYPE_BYTE);
WritableRaster raster = Raster.createInterleavedRaster(dataBuffer,
orig.getWidth(), orig.getHeight(),
orig.getWidth() * 4, 4,
bOffs, null);
BufferedImage mask = new BufferedImage(colorModel, raster, false, null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.