使用 ImageWriter 保存为 JPEG 时将 Alpha 通道转换为白色

发布于 2024-12-09 20:11:34 字数 845 浏览 0 评论 0原文

我使用以下代码片段将 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 通道转换为白色,而不是默认情况下的黑色,但我找不到方法来做到这一点。将不胜感激任何帮助!

I'm converting a png image to jpeg with the following snippet of code:

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());

I'd like to convert the alpha channel to white, not black as it does by default, but I couldn't find a way to do that. Will appreciate any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百合的盛世恋 2024-12-16 20:11:34

我的解决方案很丑陋,而且可能很慢,但它是一个解决方案:)

    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 内容以供进一步参考。

My solution is ugly and probably slow, but it's a solution :)

    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 );
        }
    }

Of course, you can reduce the code by 60% if you just need to adjust the alpha channel. I kept all RGB stuff for further referece.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文