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

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

问题描述

我正在使用javax.imageio从Windows和Linux上的文本字符串生成图像,我发现图像的质量差异很大(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编码期间引起的,而是在将文本呈现到Image中时已经引起的.如果没有文本抗锯齿,则可以将图像压缩为较小的尺寸,因为它包含的颜色差异较小,因此也可以解释文件大小的差异.

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和Windows)上生成不同的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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