将Graphics2D写入BufferedImage - 字体分辨率较差

时间:2017-07-18 15:58:52

标签: java graphics2d javax.imageio

因此,我尝试通过绘制BufferedImage中的Graphics2D对象,然后使用ImageIO.write()将其写入文件来创建用于通用目的的标签。不幸的是,我遇到了一个问题,我无法弄清楚如何使字体的质量不被模糊和#39; (我无法想到英文中的单词,边缘有点切割或锯齿状,有点表示我认为渲染分辨率低)。有没有办法设置它,以便字体呈现在某个DPI /质量/分辨率?

This is what I get when I render the image。请注意,我单独生成的条形码不是“模糊”的。本身但字体是。

/**
 * 
 * @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;
}

0 个答案:

没有答案