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

查看:39
本文介绍了为什么 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");
        }
    }
}

如果您尝试使用例如 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. Gamma 值可能会在 Java 损坏后重置或丢失.

我实际上并没有尝试您的示例...您能否同时发布前后文件?如果实际上无法检查结果文件,就很难判断这些额外数据是否存在.

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 代替.在使用 Firefox 和各种程序的 Windows 上,它们在我们看来是相同的,因为这些程序在渲染时不使用颜色配置文件.但是,在您的 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天全站免登陆