为什么 Java ImageIO 在不同的机器上生成不同的图像(linux vs Windows)? [英] Why does Java ImageIO generate different images on different machines (linux vs Windows)?

查看:27
本文介绍了为什么 Java ImageIO 在不同的机器上生成不同的图像(linux vs Windows)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 和 Linux 上使用 javax.imageio 从文本字符串生成图像,我发现图像质量非常不同(Linux = 质量差,物理尺寸小,但尺寸相同).

I'm using javax.imageio to generate images from text strings on Windows and Linux, and I find the images are very different in quality (Linux = poor quality, small physical size, although same dimentions).

Linux (Ubunutu),443 字节

Windows 7,1,242 字节

我使用相同的字体文件(来自 Windows,上传到 linux),并使用此代码生成图像.知道如何提高 linux 生成的图像的质量吗?为什么生成的图像首先不同?

I'm using the same font file (From Windows, uploaded to linux), and using this code to generate the images. Any idea how to improve the quality of the linux-generated images? Why are the generated images different in the first place?

我试过设置 显式压缩(通过 iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);),但是当我尝试这样做时,我得到了 UnsupportedOperationException.

I've tried setting explicit compression (via iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);), but I'm getting an UnsupportedOperationException when I try that.

更新:

这是一个 SSCCE.我已经更新了我的例子并删除了字体,结果是一致的.如果您在两个系统上都设置了字体,它们也会发生.

Here is an SSCCE. I've updated my example and removed the font, the results are consistent. They also happen if you do set the font on both systems.

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Example {
    /**
     * <p>Create an image from text. <p/>
     * <p/>
     * http://stackoverflow.com/a/4437998/11236
     */
    public static void createFromText(String text, Path outputFile, int width, int height, Color color, int fontSize) {
        JLabel label = new JLabel(text, SwingConstants.CENTER);
        label.setSize(width, height);
        label.setForeground(color);

        BufferedImage image = new BufferedImage(
                label.getWidth(), label.getHeight(),
                BufferedImage.TYPE_INT_ARGB);

        Graphics g = null;
        try {
            // paint the html to an image
            g = image.getGraphics();
            g.setColor(Color.BLACK);
            label.paint(g);
        } finally {
            if (g != null) {
                g.dispose();
            }
        }

        // get the byte array of the image (as jpeg)
        try {
            ImageIO.write(image, "png", outputFile.toFile());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        Path output = Paths.get("/tmp/foo.png");
        createFromText("Custom Text", output, 200, 40, Color.blue, 30);
    }
}

推荐答案

只是猜测,但您的图像看起来像这样也可能来自将文本渲染到图像时不同的抗锯齿行为.这意味着差异不是在图像的压缩或 png 编码期间引起的,而是在将文本渲染到图像中时引起的.如果没有文字抗锯齿,图片可以被压缩到更小的尺寸,因为它包含的不同颜色较少,这样也可以解释文件大小的差异.

Just a guess, but your images look like this could also come from different antialiasing behaviour when rendering the text to an image. Meaning that the difference is not caused during the compression or png encoding of the image, but already when rendering the text into the Image. If there is no text antialiasing, the image can be compressed to a smaller size because it contains less different colours, so that could also explain the difference in file size.

尝试使用明确指定的抗锯齿设置而不是依赖于系统默认值进行试验,看看这是否有所不同.您可以通过将 RenderingHints 添加到 Graphics2D 对象来做到这一点,例如呈现提示 text_antialising:

Try to experiment with explicitely specifiying antialising settings instead of relying on the system defaults, and see if that makes a difference. You can do this by adding RenderingHints to the Graphics2D object, e.g. the rendering hint text_antialising:

http://docs.oracle.com/javase/6/docs/api/java/awt/RenderingHints.html#KEY_TEXT_ANTIALIASING

另见此处有关字体渲染行为的讨论:

Also see here for a discussion on font rendering behaviour:

Java 字体渲染

这篇关于为什么 Java ImageIO 在不同的机器上生成不同的图像(linux vs Windows)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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