我正在使用以下代码片段将png图像转换为jpeg:
ByteArrayOutputStream image1baos = new ByteArrayOutputStream();
image1 = resizeImage(cropImage(image1, rect1), 150);
ImageWriter writer = null;
Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpg");
if (iter.hasNext()) {
writer = (ImageWriter) iter.next();
}
ImageOutputStream ios = ImageIO.createImageOutputStream(image1baos);
writer.setOutput(ios);
// set the compression quality
ImageWriteParam iwparam = new MyImageWriteParam();
iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwparam.setCompressionQuality(0.2f);
// write image 1
writer.write(null, new IIOImage(image1, null, null), iwparam);
ios.flush();
// set image 1
c.getItem1().setImageData(image1baos.toByteArray());
我想将Alpha通道转换为白色,而不是像默认情况下那样将黑色转换为黑色,但我无法找到方法。将不胜感激任何帮助!
答案 0 :(得分:2)
我的解决方案很难看,可能很慢,但这是一个解决方案:)
BufferedImage img = <your image>
for( int i = 0; i < img.getWidth( ); i++ )
for( int j = 0; j < img.getHeight( ); j++ ) {
// get argb from pixel
int coli = img.getRGB( i, j );
int a = coli >> 24 & 0xFF;
int r = coli >> 16 & 0xFF;
int g = coli >> 8 & 0xFF;
int b = coli & 0xFF;
coli &= ~0xFFFFFFFF;
// do what you want with a, r, g and b, in your case :
a = 0xFF;
// save argb
coli |= a << 24;
coli |= r << 16;
coli |= g << 8;
coli |= b << 0;
img.setRGB( i, j, coli );
}
}
当然,如果您只需要调整Alpha通道,则可以将代码减少60%。我保留了所有RGB的东西以供进一步参考。