I have a piece of java code which draws text on a buffer and saves it as monochrome BMP. I executed the program on Windows 7 and CentOS 6.3 .
Font used is arial. The image generated in Windows is crisp, characters are uniformly rendered. Why on Cent OS the characters are thin, looks like platform misses to render some pixels.
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class TimesB extends Canvas {
private Image img;
public TimesB() {
setBackground(Color.white);
}
public static void main(String s[]) throws IOException {
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
public void windowClosed(WindowEvent e) {System.exit(0);}
};
int width = 400, height = 300;
// TYPE_INT_ARGB specifies the image format: 8-bit RGBA packed
// into integer pixels
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
Graphics2D ig2 = bi.createGraphics();
ig2.setBackground(Color.WHITE);
Font font = new Font("Arial", Font.PLAIN, 18);
ig2.setFont(font);
FontRenderContext frc = ig2.getFontRenderContext();
System.out.println("Transform Type: " + frc.getTransformType());
System.out.println("Transform: " + frc.getTransform());
System.out.println("Anti- Aliasing: " + frc.getAntiAliasingHint());
String message = "Cantaloupemelone weissfleischig hell, mit Kernen!";
FontMetrics fontMetrics = ig2.getFontMetrics();
int stringWidth = fontMetrics.stringWidth(message);
int stringHeight = fontMetrics.getAscent();
// ig2.setPaint(Color.black);
ig2.drawString(message, 10,30);
message = "Imazalil, Theiabendazol und orthophenylphenol";
ig2.drawString(message, 10,60);
message = "gleichmabig feucht halten, Staunsse vermiden";
ig2.drawString(message, 10,90);
WritableRaster raster = bi.getRaster();
// Put the pixels on the raster. Note that only values 0 and 1 are used for the pixels.
// You could even use other values: in this type of image, even values are black and odd
// values are white.
for(int h=0;h<height;h++)
for(int w=0;w<width;w++){
int iVal = raster.getSample(w, h, 0);
if(iVal == 0) raster.setSample(w,h,0,1);
else
raster.setSample(w,h,0,0);
}
ImageIO.write(bi, "BMP", new File("D:\\project\\Java\\yourImageName_w.BMP"));
}
}
This is not only with java, even freetype library used with C++ produces the same output.
Is it because of the underlying graphics layer ? how this can be fixed ? How to make the Linux font rendering as good as windows ?
**I could not attach the output as I need 10 reputation !!