首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用zxing生成条形码

使用zxing生成条形码
EN

Stack Overflow用户
提问于 2012-09-27 17:33:51
回答 4查看 19.3K关注 0票数 2

我能够生成条形码使用zxing条形码库,我使用...

代码语言:javascript
复制
String text = "123456789101"; 

int width  = 300;
int height = 100; 
String imgFormat = "png";

BitMatrix bitMatrix = new UPCAWriter().encode(text, BarcodeFormat.UPC_A, width, height);
MatrixToImageWriter.writeToStream(bitMatrix, imgFormat, new FileOutputStream(new    File("C:\\code_.png")));
out.println("Success!");

我得到了我的输出,与平面条形码图像,但我想打印‘文本(字符串文本= "123456789101";)’也底部的图像,任何人知道请帮助我。

非常感谢。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-09-27 17:42:44

如果你的库没有实现这样的东西,我想它没有,否则这里没有问题,你可以自己在条形码图像上打印你的代码。有关如何做到这一点的简要描述,请查看this问题。

另一种选择是在图像下以明文形式输出代码-不确定它是否适合您。

更新:您也可以尝试使用Barcode4j库。我认为它可以做这样的事情。

票数 3
EN

Stack Overflow用户

发布于 2014-10-28 22:50:18

是一个旧线程,但如果有人还需要它的话...

代码语言:javascript
复制
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Hashtable;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

// ...
// vars width and height have image width and height (int)
// var barcodeMessage has the text under the barcode

// text
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB); // aux implementation
Graphics2D g2d = img.createGraphics();
Font font = new Font("Times", Font.PLAIN, 11);
g2d.setFont(font);
FontMetrics fm = g2d.getFontMetrics();
int textWidth = fm.stringWidth(barcodeMessage);
int textHeight = fm.getHeight();
g2d.dispose();
img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
g2d = img.createGraphics();
g2d.setColor(backgroundColor);
g2d.fillRect(0, 0, width, height);
g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
g2d.setFont(font);
fm = g2d.getFontMetrics();
g2d.setColor(textColor);
g2d.drawString(barcodeMessage, Math.round(Math.floor((width-textWidth)/2))-2, height-fm.getAscent());
g2d.dispose();

// barcode
Code128Writer code128Writer = new Code128Writer();
Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
BitMatrix bitMatrix = code128Writer.encode(barcodeMessage, BarcodeFormat.CODE_128, width, height-textHeight-(2*fm.getAscent()), hintMap);

// Make the BufferedImage that are to hold the Code128
int matrixWidth = bitMatrix.getWidth();
int matrixHeight = bitMatrix.getHeight();

Graphics2D graphics = (Graphics2D) img.getGraphics();
graphics.setColor(textColor);
for (int i = 0; i < matrixWidth; i++) {
    for (int j = 0; j < matrixHeight; j++) {
        if (bitMatrix.get(i, j)) {
            graphics.fillRect(i, j+fm.getAscent(), 1, 1);
        }
    }
}
graphics.dispose();

您可以使用宽度和高度变量、字体、条形码类型等。

票数 3
EN

Stack Overflow用户

发布于 2017-07-01 22:31:45

此方法允许使用java.awt绘制一个白色矩形,并在其上绘制图像底部的编码字符串。

代码语言:javascript
复制
/**
 * 
 * @param codeS String coded to a barcode
 * @param qrfile File with generated image when completed.
 * @param width in pixels
 * @param height in pixels
 * @param fontSize in pixels
 * @throws Exception 
 */
public void generateBarCode(String codeS,
        File qrfile,
        int width,
        int height,
        int fontSize) throws Exception {
    FileOutputStream qrCode = null;
    try {
        // Encode URL in QR format
        BitMatrix matrix;
        com.google.zxing.Writer writer = new Code128Writer();
        //writer = new QRCodeWriter(); in case of BarcodeFormat.QR_CODE
        try {
            matrix = writer.encode(codeS, BarcodeFormat.CODE_128, width, height);
        } catch (WriterException e) {
            //logger.error("Error generando el QR", e);
            throw new Exception("Error generando el QR");
        }
        // Create buffered image to draw to
        BufferedImage image = new BufferedImage(width,
                height, BufferedImage.TYPE_INT_RGB);
        // Iterate through the matrix and draw the pixels to the image
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int grayValue = (matrix.get(x, y) ? 0 : 1) & 0xff;
                image.setRGB(x, y, (grayValue == 0 ? 0 : 0xFFFFFF));
            }
        }
        Graphics graphics = image.getGraphics();
        graphics.drawImage(image, 0, 0, null);


        Font f = new Font("Arial", Font.PLAIN, fontSize);

        FontRenderContext frc = image.getGraphics().getFontMetrics().getFontRenderContext();
        Rectangle2D rect = f.getStringBounds(codeS, frc);
        graphics.setColor(Color.WHITE);

        //add 10 pixels to width to get 5 pixels of padding in left/right
        //add 6 pixels to height to get 3 pixels of padding in top/bottom
        graphics.fillRect(
                (int)Math.ceil((image.getWidth()/2)-((rect.getWidth()+10)/2)), 
                (int)Math.ceil(image.getHeight() - (rect.getHeight()+6)), 
                (int)Math.ceil(rect.getWidth()+10), 
                (int)Math.ceil(rect.getHeight()+6));
        // add the watermark text
        graphics.setFont(f);
        graphics.setColor(Color.BLACK);
        graphics.drawString(codeS, 
                (int)Math.ceil((image.getWidth()/2)-((rect.getWidth())/2)), 
                (int)Math.ceil(image.getHeight() - 6));
        graphics.dispose();

        qrCode = new FileOutputStream(qrfile);
        ImageIO.write(image, "png", qrCode);

    } catch (Exception ex) {
        throw new Exception("Error generando el QR");
    } finally {
        try {
            qrCode.close();
        } catch (Exception ex) {
            throw new Exception("Error generando el QR");
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12618311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档