如何提高Graphics2D文字质量?

时间:2012-11-24 12:48:17

标签: java rendering barcode graphics2d text-rendering

我有一个关于打印条形码附加信息的问题。我正在使用http://barbecue.sourceforge.net/创建我的条形码。

创建条形码后,我想添加一些其他信息。目前我用以下方式做到这一点!例如:

Graphics2D g2d5 = container4Barcode.createGraphics();
g2d5.setBackground(Color.WHITE);
g2d5.clearRect(0, 33, 200, 200);
g2d5.setColor(Color.BLACK);
g2d5.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d5.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
    RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
g2d5.setFont(new Font("Arial", Font.PLAIN, 8));
g2d5.drawString(barcode, 8, 40);
g2d5.drawString(generateRandomNumber(ekPreis), 57, 40);
String datumResult = datum;
g2d5.drawString(location, 98, 40);
g2d5.drawString(datum.substring(2), 114, 40);
g2d5.dispose();

输出结果如下: enter image description here

正如您所看到的,我的文本质量(条形码上方和下方)非常糟糕......如何提高文本质量,使文本更加流畅而不是像素化?!

(当我打印我的条形码时,条形码看起来非常像素化......)

任何提示?

更新

所以,我在这里添加了我最新结果的图片......当我打印出这些条形码时,它们看起来很糟糕!所以这就是我所做的代码:

Graphics2D g2d6 = container4Barcode.createGraphics();
g2d6.setColor(Color.black);
g2d6.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d6.setFont(new Font("Verdana", Font.BOLD, 7));
g2d6.drawString("FLORETT", 9, 20);
g2d6.drawString("50-521-60", 57, 20);
Graphics2D g2d4 = container4Barcode.createGraphics();
g2d4.setColor(Color.black);
g2d4.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
    RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 
g2d4.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, 
    RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2d4.setFont(new Font("Verdana", Font.BOLD, 11));
g2d4.drawString("SSYYS", 105, 19);
g2d4.dispose();

通过该代码,我获得了最好的结果!当然我玩“Metrics,AA_GASP,LCS_HRGB,不同的字体(Verdana在我看来是最好的)......”还有更多,但其中一些我无法使用,因为那时我的条形码变得模糊了!因此,我正在努力解决这个问题,即我无法从graphics2d中提高拉绳文字质量的质量!

所以,我想问一下是否有可能让“SSYYS”(字体大小11)和“FLORETT”(字体大小7)看起来更好! JAVA是否有可能在字体大小小于“12”的图像上绘制“平滑”文本?有解决方法吗?如图所示,字母“S和Y”看起来非常糟糕......

第二次更新:

到目前为止的一些示例代码...请确保存在以下文件夹:C:\ TestBarcodes \

希望我将代码减少到最低限度,你可以想象我的问题是什么......

package generator;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;

import net.sourceforge.barbecue.Barcode;
import net.sourceforge.barbecue.BarcodeException;
import net.sourceforge.barbecue.BarcodeFactory;
import net.sourceforge.barbecue.output.OutputException;

import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;

public class BarcodeGen {

  // sets the picWidth
  private static int picWidth = 149;
  // sets the picHeigth
  private static int picHeigth = 60;

  public static void main(String[] args) 
      throws BarcodeException, OutputException, COSVisitorException, IOException {
    generateBarcode("11138500");
  }

  public static void generateBarcode(String barcode) 
      throws IOException, COSVisitorException, BarcodeException, OutputException {

    Barcode barcode2 = BarcodeFactory.createCode39(barcode, false);
    int gw = barcode2.getWidth();
    // change this to suit if you want higher, default 50
    // barcode2.setBarWidth(50);
    // this sets DPI
    barcode2.setResolution(100);
    // barcode2.setFont(font);
    int gh = barcode2.getHeight();
    // change this if you want a coloured background
    // image = new BufferedImage(t, s, BufferedImage.TYPE_INT_RGB)
    BufferedImage image = new BufferedImage(gw, gh, BufferedImage.TYPE_INT_RGB);

    Graphics2D g2 = (Graphics2D) image.getGraphics();
    // default is black so draw a white box first
    // change type to INT_RGB if you want a coloured background
    g2.setColor(Color.white);
    g2.fillRect(0, 0, gw, gh);
    barcode2.draw(g2, 0, 0);

    // CREATE ADDITIONAL INFORMATION ON BARCODE

    BufferedImage container4Barcode = new BufferedImage(
        picWidth, picHeigth, image.getType());
    Graphics2D g2d = container4Barcode.createGraphics();

    g2d.setBackground(Color.WHITE);
    g2d.clearRect(0, 0, picWidth, picHeigth);
    g2d.setColor(Color.black);
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
        RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
    g2d.drawImage(image, 8, 21, 130, 18, null);
    g2d.dispose();

    Graphics2D g2d6 = container4Barcode.createGraphics();
    g2d6.setColor(Color.black);
    g2d6.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
        RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2d6.setFont(new Font("Verdana", Font.BOLD, 7));

    g2d6.drawString("FLORETT", 9, 20);
    g2d6.drawString("50-521-60", 57, 20);

    Graphics2D g2d4 = container4Barcode.createGraphics();
    g2d4.setColor(Color.black);
    g2d4.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
        RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2d4.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
        RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    g2d4.setFont(new Font("Verdana", Font.BOLD, 11));
    g2d4.drawString("SSYYS", 105, 19);
    g2d4.dispose();

    // PRINT PDF

    int ver = 782;

    PDDocument doc = new PDDocument();
    PDPage page = new PDPage(PDPage.PAGE_SIZE_A4);
    doc.addPage(page);

    PDXObjectImage image2 = new PDJpeg(doc, container4Barcode);
    PDPageContentStream content = new PDPageContentStream(doc, page);
    content.drawImage(image2, 5, ver);
    content.close();

    doc.save(new FileOutputStream("C:\\TestBarcodes\\barcode.pdf"));

    // opens the pdf file
    Process p = Runtime
        .getRuntime()
        .exec("rundll32 url.dll,FileProtocolHandler C:\\TestBarcodes\\barcode.pdf");
    try {
      p.waitFor();
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

enter image description here

1 个答案:

答案 0 :(得分:2)

如果有人想在这种情况下使用像素图像,而不是矢量,那么图像应该升级以获得更好的打印质量:

static final int PIXELS_PER_POINT = 4; // 4x

然后定义所有尺寸以点为单位,而不是以像素为单位:

// Image size in points
static final int IMAGE_WIDTH = 150;
static final int IMAGE_HEIGHT = 60;
// Font size in points
static final int FONT_SIZE = 11;

现在,在进行任何绘图时,请始终使用转换为像素的点:

static int toPixels(int value) {
    return value * PIXELS_PER_POINT;
}

BufferedImage draw() {
    BufferedImage image = 
        new BufferedImage(toPixels(IMAGE_WIDTH), toPixels(IMAGE_HEIGHT), TYPE_INT_ARGB);
    Graphics2D g = image.createGraphics();
    // <graphics init code goes here>

    Font font = new Font("Arial", Font.PLAIN, toPixels(FONT_SIZE));
    g.setFont(font);
    g.drawString("Grapes", toPixels(5), toPixels(40)); // coordinates are in points

    g.dispose()
    return image;
}

因此,通过这种方法,您可以使用“标准”维度进行操作。对于我来说,这种方法对于低复杂度和中等复杂度的绘图非常有效。

您可以更进一步,将PIXELS_PER_POINT转换为参数:使用1x为普通显示的网页上的图片,2x显示Retina,4x进行打印!