首页 > 代码库 > Atitit  图像处理底色变红的解决

Atitit  图像处理底色变红的解决

 

 

 

 

 

Atitit  图像处理底色变红的解决

 

1.1. 原因  ImageIO  bug alpha通道应该在保存jpg的时候排除1

1.2. 解决,自己移除alpha通道即可1

2. Image saved in JavaFX as jpg is pink toned1

 

 

1.1. 原因  ImageIO  bug alpha通道应该在保存jpg的时候排除

It looks like you are encountering existing bugs in the ImageIO or JavaFX Image processing libraries.

 

SwingFXUtils.fromFXImage

1.2. 解决,自己移除alpha通道即可

 

public static BufferedImage Remove_alpha_channel(BufferedImage copy2) {

// Get buffered image:

BufferedImage image = copy2;

 

// Remove alpha-channel from buffered image:

BufferedImage imageRGB =

  new BufferedImage(

    image.getWidth(),

    image.getHeight(),

    BufferedImage.OPAQUE);

 

Graphics2D graphics = imageRGB.createGraphics();

 

graphics.drawImage(

  image,

  0,

  0,

  null);

return imageRGB;

}

 

2. Image saved in JavaFX as jpg is pink toned

// Remove alpha-channel from buffered image:BufferedImage imageRGB =

  new BufferedImage(

    image.getWidth(),

    image.getHeight(),

    BufferedImage.OPAQUE);

 

个人猜测,因为PNG图片包含了一个Alpha(透明)通道,一共四个通道,jpg不支持Alpha通道,所以导致了这个现象,细节要看算法,研究不深,就不分析了,下面给出解决方法:

Java

技术分享

0

1

2

3

4

5

6

7

8

9

10

 

 

java.awt.Image i = Toolkit.getDefaultToolkit().createImage(图像);

PixelGrabber pg = new PixelGrabber( i, 0, 0, -1, -1,  true);

pg.grabPixels();

int width = pg.getWidth(), height = pg.getHeight();

 

DataBuffer buffer = new DataBufferInt(( int[])pg.getPixels(), pg.getWidth() * pg.getHeight());

WritableRaster raster = Raster.createPackedRaster( buffer, width, height, width, RGB_MASKS , null );

BufferedImage img = new BufferedImage( RGB_OPAQUE,raster, false, null);

 


使用Toolkit打开图像,createImage有几个重载函数请按需选择,最后输出BufferedImage就是正常色彩。

参考:http://stackoverflow.com/questions/43

小卒 最近在做头像上传功能,发现对于带有alpha通道的图片,有的会变红,有的会变黑,有时压缩不平滑。发现是有几处程序写错了。

 

. As widely discussed, the problem is in alpha-channel that needs to be excluded from the source image, targeted for .jpg save. I also rearranged my code to make it shorter. The workaround is:

 

save - Image saved in JavaFX as jpg is pink toned - Stack Overflow.html

作者:: 绰号:老哇的爪子 ( 全名::Attilax Akbar Al Rapanui 阿提拉克斯 阿克巴 阿尔 拉帕努伊 ) 

汉字名:艾提拉(艾龙)   EMAIL:1466519819@qq.com

转载请注明来源: http://www.cnblogs.com/attilax/

Atiend

 

 

Atitit  图像处理底色变红的解决