9

I have a question concerning printing additional information on barcodes. I am using http://barbecue.sourceforge.net/ to create my barcodes.

After I created my barcodes I want to add some additional information. At the moment i do this with the following way! For example:

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();

The output is in a pdf the following: enter image description here

As you can see is the quality of my text (above and under the barcode) is really bad ... How can I increase the quality of the text to make the text more smoother and not that pixelated?!

(When I print my barcodes, the barcodes look very pixelated ...)

Any tips?

UPDATE:

So, I added here the a picture of my latest outcome ... When I print out these barcodes they look horrible! So here is the code what I did:

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();

With that Code I get the best results! Of course I played with "Metrics, AA_GASP, LCS_HRGB, different fonts (Verdana is the best in my opinion) ..." and a lot more, but some of them I couldn't use, because then my barcode got blurred! So actioally I am forcing the problem that I am unable to improve the quality of my text-quality of the drawstring from graphics2d!

So, I want to ask if there is a possibility to let the "SSYYS" (Font Size 11) and the "FLORETT" (Font Size 7) look much nicer! Is there a possibility in JAVA to draw "smooth" text on an image with a font size less than "12" ? Is there a workaround to to that ? As you can see in the picture the letters "S and Y" look very awful...

2nd Update:

Some Example code so far... Please be sure that the following folder exists: C:\TestBarcodes\

Hope I reduced my code to the minimum that you can imagine what my problem is...

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

4

2 回答 2

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打印!

于 2013-10-08T12:00:28.610 回答
1

要修复 Graphics2d 中文本或形状中的锯齿状边缘,您需要设置RenderingHint

Graphics2D g2d = bufferedImage.createGraphics();

    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

解释

典型的抗锯齿算法通过根据估计的形状的部分像素覆盖率将形状边界的像素的现有颜色与请求的填充油漆混合来工作。

文本抗锯齿提示键。TEXT_ANTIALIASING 提示可以控制文本抗锯齿算法的使用,而与用于形状渲染的选择无关。通常,应用程序可能只想对文本使用抗锯齿,而不对其他形状使用。此外,用于减少文本混叠伪影的算法通常比为一般渲染开发的算法更复杂,因此此提示键提供了额外的值,可以控制某些特定于文本的算法的选择。如果保留在 DEFAULT 状态,此提示通常将遵循常规 KEY_ANTIALIASING 提示键的值。

于 2021-09-21T15:12:59.873 回答