为什么Java ImageIO会使JPEG颜色变平 [英] Why Java ImageIO flattens JPEG colors

查看:109
本文介绍了为什么Java ImageIO会使JPEG颜色变平的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我读取某些JPG文件时,颜色会变平。这是一个简单的示例,它读取jpg并将相同的图像写入另一个文件。

When I read certain JPG files, colors are flattened. Here is a simple example that reads a jpg and just writes the same image to another file.

import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

public class JPegReadTest {
    public static void main(String[] args) {
        if (args.length == 2) {
            try {
                BufferedImage src = ImageIO.read(new File(args[0]));
                ImageIO.write(src, "jpg", new File(args[1]));
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.err.println("Usage: java JPegReadTest src dest");
        }
    }
}

如果您尝试使用for示例 http://www.flickr.com/photos/visualpanic/233508614/sizes / l / ,目标图像的颜色与源文件不同。这是为什么?如何解决?

If you try this with for example http://www.flickr.com/photos/visualpanic/233508614/sizes/l/ , the colors of the destination image differ from the source file. Why is that? How to fix it?

还尝试将图像保存为png,但颜色也很淡(所以假设颜色信息没有正确读取)。

Also tried saving the image as png, but the colors are bland in it too (so assuming color info is not read properly).

推荐答案

这可能有几个原因。


  1. JPEG颜色数据通常存储为YCrCb而不是RGB,尽管转换应该几乎不可察觉。

  2. JPEG通常具有嵌入的颜色配置文件,但许多应用程序不理解这一点而只是忽略它(在这种情况下,您的输出文件可能缺少颜色配置文件。)

  3. 在Java破坏之后,Gamma值可能会被重置或丢失。

我实际上没有尝试你的例子......你能不能在文件前后发布?实际上没有能够检查结果文件,很难判断这些额外数据是否存在。

I didn't actually try your example... could you please post both before and after files? Without actually being able to examine the result file, it's hard to tell if this extra data is there or not.

编辑:是的,它是清除原始图像和转换后的图像具有不同的颜色配置文件Java剥离了原始的颜色配置文件,而是使用了通用的sRGB。它们在Windows上使用Firefox和各种程序看起来一样,因为这些程序在渲染器时不使用颜色配置文件。但是,在Mac上,Mac实际上支持这些颜色配置文件(在Mac上提供图形等方面的争论),因此它们呈现的方式不同。我没有Mac方便,但我怀疑如果你在任何平台上用Photoshop打开文件,你会看到差异。

Yeah, it's clear that your original and converted images have different color profiles. Java stripped out the original's color profile and used generic sRGB instead. They look the same to us on Windows with Firefox and assorted programs because these programs don't use the color profile when renderer. However, on your Mac, Mac actually supports these color profiles (cue debate over Macs for graphics, etc.) and so they render differently. I don't have a Mac handy, but I suspect that if you open the files in Photoshop on any platform, you'll see the difference.

这篇关于为什么Java ImageIO会使JPEG颜色变平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆