java 生成条形码

这篇博客介绍了如何在Java中利用ZXing库生成和解码条形码。首先,需要下载ZXing的core-3.1.0.jar包,然后通过提供的代码示例可以将内容编码为条形码图片,同时也能将条形码图片中的内容解码出来。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java 生成二维码有很多开发的jar包如zxing,qrcode.q前者是谷歌开发的后者则是小日本开发的,
这里我们使用zxing的开发包来弄

1、先下载zxing开发包,这里用到的只是core那个jar包 (core-3.1.0.jar)
下载地址:http://download.youkuaiyun.com/download/u014733374/8212455
2、使用zxing开发还需要一个类,代码如下

public class ZxingEAN13EncoderHandler {
 //contents:条形码内容;width:条形码宽度;height:条形码高度;imgPath:生成的条形码的存放路径
 public String encode(String contents, int width, int height, String imgPath) {
        int codeWidth = 3 + // start guard
                (7 * 6) + // left bars
                5 + // middle guard
                (7 * 6) + // right bars
                3; // end guard
        codeWidth = Math.max(codeWidth, width);
        try {
            //在这里生成条形码的图片
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
                    BarcodeFormat.EAN_13, codeWidth, height, null);
            //在这里将生成的条形码的图片放在指定的路径下
            MatrixToImageWriter
                    .writeToFile(bitMatrix, "png", new File(imgPath));
            //在服务器上存放条形码的图片路径
            String findPicture = "http://m.aicailang.com:7001/upload/" + userId +".jpg";
            return findPicture ;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

   public static void main(String[] args) {
        String imgPath = "d:/2.png";
        // 益达无糖口香糖的条形码
        String contents = "6923450657713";
        int width = 105, height = 50;
        ZxingEAN13EncoderHandler handler = new ZxingEAN13EncoderHandler();
        //gettxm就是返回的条形码的编码图片
        String gettxm = handler.encode(contents, width, height, imgPath);

    }
 }

3、上面的将内容编码成条形码图片格式,下面是将编码以后的条形码图片中的内容解码出来

public class ZxingEAN13DecoderHandler {
    /**
     * @param imgPath
     * @return String
     */
    public String decode(String imgPath) {//将条形码的图片路径传进来
        BufferedImage image = null;
        Result result = null;
        try {
            image = ImageIO.read(new File(imgPath));
            if (image == null) {
                System.out.println("the decode image may be not exit.");
            }
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
            result = new MultiFormatReader().decode(bitmap, null);
            return result.getText();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        String imgPath = "d:/2.png";//存放条形码图片的路径
        ZxingEAN13DecoderHandler handler = new ZxingEAN13DecoderHandler();
        String decodeContent = handler.decode(imgPath);//将条形码图片的内容解析出来为一个字符串
        System.out.println("解码内容如下:");
        System.out.println(decodeContent);
   }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值