Reading JPEGs: ImageIO.read() messes up color space

前端 未结 3 1610
悲&欢浪女
悲&欢浪女 2021-01-06 11:06

I\'m trying to read, rescale and save images within a Servlet. That\'s the relevant code:

BufferedImage image = ImageIO.read(file);

BufferedImage after = ne         


        
相关标签:
3条回答
  • 2021-01-06 11:50

    I have had the same problem, and found this page.

    I tried the suggestion above of creating a BufferedImage with the right type and using it as the after image instead of null in the filter call; and that did indeed resolve the problem.

    0 讨论(0)
  • 2021-01-06 11:56

    ImageIO.read ignores all embedded metadata, including an embedded color profile, which defines how RBG values map to physical devices such as screens or printers.

    You could read the metadata separately and pass it to ImageIO.write, but it's easier to just convert the image to the (default) sRGB color space and ignore the metadata.

    If you don't mind losing the metadata, replace

    after = scaleOp.filter(image, null);
    

    with

    after = scaleOp.filter(image, after);
    

    From the documentation of AffineTransformOp.filter:

    If the color models for the two images do not match, a color conversion into the destination color model is performed.

    0 讨论(0)
  • 2021-01-06 11:58

    You create after and then overwrite it with scaleOp.filter. Is this correct? So your after image may not be RGB even though you think it is? If you want after to be RGB then you may need to 'draw' image onto after before you do the transform.

    0 讨论(0)
提交回复
热议问题