0

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;
}
  • 1
    You need to make use of `RenderingHints`, for [example](https://stackoverflow.com/questions/13906687/how-can-i-smooth-my-jframe-shape/13906713#13906713) and [example](https://stackoverflow.com/questions/28427566/save-jpanel-as-image-hd-quality/28492187#28492187) – MadProgrammer Jul 18 '17 at 22:43
  • @MadProgrammer I'm looking through those two examples - is it an issue of DPI or is it just that I must set certain rendering hints so that text is not blurred? – Dan Mallory Jul 19 '17 at 16:19
  • In this immediate case, since you're only generating an image, I'd say it's just an configuration of the rendering hints. `Graphics` by default is configured at a very simple level, for speed (low quality, high speed), this gives you control over how to configure it for your specific needs. I'd just look at enabling the font anti aliasing properties and see what you get. If need be, you could start with a up scaled version (4x), render that then downscale it (by 4x) which would "fake" it, depending on the scaling algorithm, but it's probably easier to just set the rendering hints – MadProgrammer Jul 19 '17 at 22:10
  • @MadProgrammer thank you - this is very helpful ! And I figured it out aswell – Dan Mallory Jul 20 '17 at 13:18
  • @MadProgrammer I had to adjust things a bit for generalizing it since I won't be working with this code for much longer - I noticed that the issue thats coming back is even if I have antialiasing on, the text is still somewhat blurred. Here's what I'm running into - the barcode I'm generating is perfect quality (mostly because zxing is refined well enough) but the 'resolution' that the text is rendered at (anti-aliasing does help.. a bit) is lower than I would like. I've tried n possible combinations of rendering hints, any other ideas? http://imgur.com/a/Y2r4d – Dan Mallory Jul 24 '17 at 13:21
  • @MadProgrammer ah... vector vs bitmap imaging – Dan Mallory Jul 24 '17 at 15:01
  • I personally prefer vector based images, they are more flexible, but pixel images are generally easier to produce and you gain better control over the fine details – MadProgrammer Jul 24 '17 at 20:16
  • @MadProgrammer Yeah - I found a good library (VectorGraphics2D) someone wrote that helped well enough. Thank you ! – Dan Mallory Jul 25 '17 at 17:47

0 Answers0