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 bytes
Windows 7, 1,242 bytes
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?
I've tried setting explicit compression (via iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
), but I'm getting an UnsupportedOperationException when I try that.
Update:
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);
}
}