So I'm trying to create labels for a generalized purpose by drawing to a Graphics2D object in a BufferedImage and then using ImageIO.write() to write this to a file. Unfortunately I'm encountering an issue where I can't figure out how to make the quality of the Font not 'blurred' (i can't think of the word in English, the edges are a bit cut or jagged, sort of indicative of low render resolution I guess). Is there a way to set it so that the Font renders at a certain DPI/Quality/Resolution?
This is what I get when I render the image. Notice the barcode I generated separately is not 'blurred' per se but the Font is.
/**
*
* @param data Array of Strings passed into label, where data[0] is the string to be barcoded
* @param titles Array of Strings passed into label parallel to data array, containing appropriate titles
* @throws IOException If
* @throws WriterException
*/
public static File createSeedBag(String[] titles,String[] data, Font[] fonts) throws IOException, WriterException{
/**
* TODO: Array of Fonts to customize Font
* Size customization
*/
BufferedImage theLabel;
BufferedImage theBarcode;
Graphics2D theGraphics;
File imageFile,
theFile;
FontMetrics titleMetrics,
theMetrics;
int width = 300;
int height = 450;
theLabel = new BufferedImage(width,height, BufferedImage.TYPE_BYTE_GRAY);
imageFile = LabelPrinter.writeBarCode(data[0], BarcodeSpecs.LARGE);
theGraphics = theLabel.createGraphics();
theGraphics.setPaint(Color.WHITE);
theGraphics.fillRect(0, 0, theLabel.getWidth(), theLabel.getHeight());
theGraphics.setPaint(Color.BLACK);
theGraphics.drawRect(0, 0, theLabel.getWidth()-1, theLabel.getHeight()-1);
/**
* Font objects
*/
fonts[0] = new Font("Bodoni 72", Font.BOLD, 40);
theGraphics.setFont(fonts[0]);
/**
* 1) Read Image File and set to bottom of label
*
* 2)
*/
theBarcode = ImageIO.read(imageFile);
theGraphics.drawImage(theBarcode, 1, height - 61, null);
titleMetrics = theGraphics.getFontMetrics(fonts[0]);
theGraphics.drawString(data[data.length-1], (width/2) - ((titleMetrics.stringWidth(data[data.length-1])) / 2), 50);
fonts[1]= new Font("Bodoni 72",0,20);
theMetrics = theGraphics.getFontMetrics(fonts[1]);
int stringHeight = theMetrics.getAscent();
theGraphics.setFont(fonts[1]);
for(int i = 1; i< data.length; i++) {
theGraphics.drawString(titles[i] + ": " + data[i], 5, 50 + (stringHeight * (i+1)));
}
theFile = new File("currentLabel.png");
ImageIO.write(theLabel, "png", theFile);
imageFile.deleteOnExit();
return theFile;
}