【发布时间】:2018-10-25 14:38:04
【问题描述】:
我正在尝试使用 Alpha 通道保存 PNG 图像,但在进行了一些像素操作并保存后,Alpha 通道在每个像素上又回到了 255。这是我的代码:
首先是像素操作:
public BufferedImage apply(BufferedImage image) {
int pixel;
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
pixel = image.getRGB(x, y);
if (threshold < getAverageRGBfromPixel(pixel)) {
image.setRGB(x, y, new Color(0f, 0f, 0f, 0f).getRGB());
}
}
}
return image;
}
注意:应该透明的像素是黑色的,所以我明确地击中了它们。
这是保存的代码。
@Test
public void testGrayscaleFilter() {
ThresholdFilter thresholdFilter = new ThresholdFilter();
testImage = thresholdFilter.apply(testImage);
File outputFile = new File(TEST_DIR + "/testGrayPicture" + ".png");
try {
// retrieve image
ImageIO.write(testImage, "png", outputFile);
} catch (IOException e) {
}
谁能告诉我我做错了什么?
【问题讨论】: