java生成矢量条形码二维码

产品说条形码模糊,然后就生成了矢量的条形码给前端用。后面才知道前端是可以自己直接生成的。
还是记录一下
遇到一个奇怪的问题,在demo工程里面请求返回的流页面能直接显示出图形。但是在项目工程里面却返回的是xml形式的字符串。这里搞了一阵。后面无意间发现。直接返回String形式的svg文件内容,前端居然可以显示。然后后台直接返回了Sting形式给前端直接用。样式也是调好的。加了width="100%‘’方便前端控制宽度。之前直接返回流前端用不了。

参考代码

    <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>de.erichseifert.vectorgraphics2d</groupId>
            <artifactId>VectorGraphics2D</artifactId>
            <version>0.9.3</version>
        </dependency>
        <dependency>
            <groupId>net.sf.barcode4j</groupId>
            <artifactId>barcode4j</artifactId>
            <version>2.1</version>
        </dependency>


    @RequestMapping("/bar")
    @ResponseBody
    public String getbar(HttpServletResponse response,String code) {
        String res = null;
        try {
             res = BarcodeTestTools.getcode3(response,code);
             log.info("返回的结果={}",res);
             res = res.replace("<svg","<svg width=\"100%\"");
        } catch (Exception e) {
            System.out.println(e);
        }
        System.out.println("返回结果==="+res);
        return res;
    }

  public static String getcode3(HttpServletResponse response,String code) throws BarcodeCanvasSetupException, TransformerException, IOException {
        ServletOutputStream out = response.getOutputStream();
        response.reset();
        Double moduleWidth = Double.valueOf(1.0D);
        double dpi = 150;
        Double width = 5d;
        Code128Bean bean = new Code128Bean();
        String barcode = code;
        Integer barcodeLength = Integer.valueOf(barcode.length());
        if (barcodeLength.intValue() < 8)
        {
            moduleWidth = Double.valueOf(2.0D * width.doubleValue());
        }
        else if (barcodeLength.intValue() < 16)
        {
            moduleWidth = width;
        }
        if (moduleWidth.doubleValue() < 1.0D)
        {
            moduleWidth = Double.valueOf(1.0D);
        }
        bean.setModuleWidth(UnitConv.in2mm(moduleWidth.doubleValue() / dpi));
//        bean.setModuleWidth(80);
//        bean.setHeight(60);
        bean.setBarHeight(30);// 高度
//        bean.setQuietZone(10); // 向右偏移
        bean.setMsgPosition(HumanReadablePlacement.HRP_NONE);
        // 设置两侧是否加空白
        bean.doQuietZone(false);
        SVGCanvasProvider canvas = new SVGCanvasProvider(true, 0);
        bean.generateBarcode(canvas, barcode);
        DocumentFragment frag = canvas.getDOMFragment();
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer trans = factory.newTransformer();
        Source src = new DOMSource(frag);
          File tempFile = File.createTempFile("bar",".svg");
        Result res = new StreamResult(tempFile);// 入参file则写到文件里面; 这里没必要这么搞,直接StringWriter写过去就可以,参考下面更新部分         response.getOutputStream()就直接写到返回流里面
        trans.transform(src, res);
        FileReader fileReader = new FileReader(tempFile);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        StringBuilder sb = new StringBuilder();
        String line =bufferedReader.readLine();
        while (line!=null){
            System.out.println(line);
            sb.append(line);
            line = bufferedReader.readLine();

        }
        bufferedReader.close();
        fileReader.close();
        tempFile.delete();
        return sb.toString();
    }


二维码

  @RequestMapping("/qr")
    @ResponseBody
    public String jxxqr(HttpServletResponse response,String code,int size) throws IOException, WriterException {
        String res = Test.getQr(response,code,size);
        System.out.println("qr====="+res);
        return res;
    }

    public static String getQr(HttpServletResponse response,String code,int size) throws WriterException, IOException {
        double point_x = 0;
        double point_y = 0;
        final int blockSize = 1;
        SVGGraphics2D funcOld = new SVGGraphics2D(point_x, point_y, 40 * blockSize, 40 * blockSize);
        ExportQrCode.fill2VectorLine(funcOld,GetBitMatrix(code, size,ErrorCorrectionLevel.M), blockSize);

        File tempFile = File.createTempFile("bar",".svg");
        PrintStream psFile = new PrintStream(tempFile);
        psFile.append(funcOld.toString());
        psFile.close();

        FileReader fileReader = new FileReader(tempFile);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        StringBuilder sb = new StringBuilder();
        String line =bufferedReader.readLine();
        while (line!=null){
            sb.append(line);
            line = bufferedReader.readLine();
        }
        bufferedReader.close();
        fileReader.close();
        tempFile.delete();
        return sb.toString();
    }

页面
<!DOCTYPE html>
<html lang="en">
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
  <meta charset="utf-8">
  <meta name="description" content="particles.js is a lightweight JavaScript library for creating particles.">
  <meta name="author" content="Vincent Garreau" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <title>testsvg</title>
</head>

<style type="text/css">
    .svg-container {
        display: inline-block;
        position: relative;
        width: 100%;
        padding-bottom: 100%;
        vertical-align: middle;
        overflow: hidden;
    }
</style>
<body>

<div style="width: 100%;background-color: gray;height: 90px;">

</div>


<div style="width: 100%;height: 120px;">
    <div style="margin-left:10px;float: left;width: 45%;height:100px;background-color: #00FF00">
        <div class="svg-container" id="showbox">
        </div>
    </div>
    <div style="float: left;width: 45%;height:100px; background-color:green;margin-left: 5px; ">
        <div class="svg-container" id="showbox2" style="margin-top: -20px;">
         </div>
    </div>
</div>

<div style="height: 400px;width:100%;float: left;" data-test="写死的二维码">
    <svg version="1.2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0mm" y="0mm" width="40mm" height="40mm" viewBox="0 0 40 40"><style type="text/css"><![CDATA[text { font:12px Dialog; }]]></style><rect x="9" y="9" width="1" height="7" style="fill:rgb(0,0,0);stroke:none" /><rect x="9" y="17" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="9" y="21" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="9" y="23" width="1" height="7" style="fill:rgb(0,0,0);stroke:none" /><rect x="10" y="9" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="10" y="15" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="10" y="19" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="10" y="21" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="10" y="23" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="10" y="29" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="11" y="9" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="11" y="11" width="1" height="3" style="fill:rgb(0,0,0);stroke:none" /><rect x="11" y="15" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="11" y="17" width="1" height="4" style="fill:rgb(0,0,0);stroke:none" /><rect x="11" y="23" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="11" y="25" width="1" height="3" style="fill:rgb(0,0,0);stroke:none" /><rect x="11" y="29" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="12" y="9" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="12" y="11" width="1" height="3" style="fill:rgb(0,0,0);stroke:none" /><rect x="12" y="15" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="12" y="17" width="1" height="2" style="fill:rgb(0,0,0);stroke:none" /><rect x="12" y="20" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="12" y="23" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="12" y="25" width="1" height="3" style="fill:rgb(0,0,0);stroke:none" /><rect x="12" y="29" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="13" y="9" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="13" y="11" width="1" height="3" style="fill:rgb(0,0,0);stroke:none" /><rect x="13" y="15" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="13" y="17" width="1" height="2" style="fill:rgb(0,0,0);stroke:none" /><rect x="13" y="23" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="13" y="25" width="1" height="3" style="fill:rgb(0,0,0);stroke:none" /><rect x="13" y="29" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="14" y="9" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="14" y="15" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="14" y="17" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="14" y="19" width="1" height="3" style="fill:rgb(0,0,0);stroke:none" /><rect x="14" y="23" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="14" y="29" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="15" y="9" width="1" height="7" style="fill:rgb(0,0,0);stroke:none" /><rect x="15" y="17" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="15" y="19" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="15" y="21" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="15" y="23" width="1" height="7" style="fill:rgb(0,0,0);stroke:none" /><rect x="16" y="20" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="17" y="11" width="1" height="6" style="fill:rgb(0,0,0);stroke:none" /><rect x="17" y="18" width="1" height="3" style="fill:rgb(0,0,0);stroke:none" /><rect x="17" y="22" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="17" y="24" width="1" height="4" style="fill:rgb(0,0,0);stroke:none" /><rect x="17" y="29" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="18" y="10" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="18" y="13" width="1" height="2" style="fill:rgb(0,0,0);stroke:none" /><rect x="18" y="17" width="1" height="2" style="fill:rgb(0,0,0);stroke:none" /><rect x="18" y="20" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="18" y="25" width="1" height="5" style="fill:rgb(0,0,0);stroke:none" /><rect x="19" y="10" width="1" height="2" style="fill:rgb(0,0,0);stroke:none" /><rect x="19" y="13" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="19" y="15" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="19" y="18" width="1" height="2" style="fill:rgb(0,0,0);stroke:none" /><rect x="19" y="21" width="1" height="3" style="fill:rgb(0,0,0);stroke:none" /><rect x="19" y="25" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="19" y="27" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="20" y="9" width="1" height="2" style="fill:rgb(0,0,0);stroke:none" /><rect x="20" y="14" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="20" y="17" width="1" height="2" style="fill:rgb(0,0,0);stroke:none" /><rect x="20" y="20" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="20" y="23" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="20" y="25" width="1" height="2" style="fill:rgb(0,0,0);stroke:none" /><rect x="20" y="28" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="21" y="10" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="21" y="12" width="1" height="2" style="fill:rgb(0,0,0);stroke:none" /><rect x="21" y="15" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="21" y="18" width="1" height="5" style="fill:rgb(0,0,0);stroke:none" /><rect x="21" y="26" width="1" height="4" style="fill:rgb(0,0,0);stroke:none" /><rect x="22" y="18" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="22" y="20" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="22" y="23" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="22" y="25" width="1" height="2" style="fill:rgb(0,0,0);stroke:none" /><rect x="22" y="28" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="23" y="9" width="1" height="7" style="fill:rgb(0,0,0);stroke:none" /><rect x="23" y="17" width="1" height="4" style="fill:rgb(0,0,0);stroke:none" /><rect x="23" y="26" width="1" height="3" style="fill:rgb(0,0,0);stroke:none" /><rect x="24" y="9" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="24" y="15" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="24" y="17" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="24" y="19" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="24" y="21" width="1" height="2" style="fill:rgb(0,0,0);stroke:none" /><rect x="24" y="24" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="24" y="27" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="24" y="29" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="25" y="9" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="25" y="11" width="1" height="3" style="fill:rgb(0,0,0);stroke:none" /><rect x="25" y="15" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="25" y="17" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="25" y="23" width="1" height="3" style="fill:rgb(0,0,0);stroke:none" /><rect x="26" y="9" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="26" y="11" width="1" height="3" style="fill:rgb(0,0,0);stroke:none" /><rect x="26" y="15" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="26" y="17" width="1" height="3" style="fill:rgb(0,0,0);stroke:none" /><rect x="26" y="26" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="26" y="28" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="27" y="9" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="27" y="11" width="1" height="3" style="fill:rgb(0,0,0);stroke:none" /><rect x="27" y="15" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="27" y="17" width="1" height="2" style="fill:rgb(0,0,0);stroke:none" /><rect x="27" y="20" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="27" y="22" width="1" height="4" style="fill:rgb(0,0,0);stroke:none" /><rect x="27" y="29" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="28" y="9" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="28" y="15" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="28" y="18" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="28" y="20" width="1" height="2" style="fill:rgb(0,0,0);stroke:none" /><rect x="28" y="24" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="28" y="28" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /><rect x="29" y="9" width="1" height="7" style="fill:rgb(0,0,0);stroke:none" /><rect x="29" y="18" width="1" height="3" style="fill:rgb(0,0,0);stroke:none" /><rect x="29" y="23" width="1" height="1" style="fill:rgb(0,0,0);stroke:none" /></svg>
</div>
<!-- scripts -->
<script th:src="@{/js/particles.js}"></script>
<script th:src="@{/js/app.js}"></script>
<script th:src="@{/js/jquery-3.4.1.min.js}"></script>
<script th:src="@{/js/layui/layui.js}"></script>
<script>

    $(function(){
        var ctxPath  = [[@{/}]];
        var ctxPath=[[${#httpServletRequest.getContextPath()}]]/;

        $.get(ctxPath +"/ban/qr?code=123456&size=40", function(result2){
            console.log(result2);
            $("#showbox2").html(result2);
        });
        $.get(ctxPath +"/ban/bar3?code=123456789", function(result){
            console.log(result);
            $("#showbox").html(result);
        });
    });

 </script>
</body>
</html>

2021-07-16 14:21:17 更新
优惠券换新版了,用矢量二维码和条形码。把之前的代码整理了一下。矢量的优点是随意放大不失真。这就很棒了!

实验发现,使用上面生成的二维码会出现白线,类似打印机坏了,一个像素没打印一样。
在这里插入图片描述
类似这种 谷歌找了一下,换了种方式后没有这个问题了 记录一下 生成的二维码很干净

另外值得注意的是 response.getOutputStream()不需要手动关闭。在servlet运行完毕时自动flush和close

 package com.mb.sleep.sleep.utils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitArray;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.krysalis.barcode4j.HumanReadablePlacement;
import org.krysalis.barcode4j.impl.code128.Code128Bean;
import org.krysalis.barcode4j.output.BarcodeCanvasSetupException;
import org.krysalis.barcode4j.output.svg.SVGCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv;
import org.w3c.dom.DocumentFragment;

import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class BarQrCodeService {

    /**
     * 条形码
     *
     * @param code
     * @return
     * @throws BarcodeCanvasSetupException
     * @throws TransformerException
     * @throws IOException
     */
    public static String getBarCodeSVGStr(String code) throws Exception {
        Double moduleWidth = Double.valueOf(1.0D);
        //分辨率
        double dpi = 300;
        Double width = 6d;
        Code128Bean bean = new Code128Bean();
        String barcode = code;
        Integer barcodeLength = Integer.valueOf(barcode.length());
        if (barcodeLength.intValue() < 8) {
            moduleWidth = Double.valueOf(2.0D * width.doubleValue());
        } else if (barcodeLength.intValue() < 16) {
            moduleWidth = width;
        }
        if (moduleWidth.doubleValue() < 1.0D) {
            moduleWidth = Double.valueOf(1.0D);
        }
        bean.setModuleWidth(UnitConv.in2mm(moduleWidth.doubleValue() / dpi));
        bean.setBarHeight(30);// 高度
        bean.setQuietZone(60); // 向右偏移
        bean.setMsgPosition(HumanReadablePlacement.HRP_NONE);
        // 设置两侧是否加空白
        bean.doQuietZone(false);
        SVGCanvasProvider canvas = new SVGCanvasProvider(true, 0);
        bean.generateBarcode(canvas, barcode);
        DocumentFragment frag = canvas.getDOMFragment();
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer trans = factory.newTransformer();
        Source src = new DOMSource(frag);
        StringWriter outw = new StringWriter();
        trans.transform(src, new StreamResult(outw));
        return outw.toString();
    }

    /**
     * 条形码写流
     * @param response
     * @param code
     * @throws BarcodeCanvasSetupException
     * @throws TransformerException
     * @throws IOException
     */
    public static void getBarCodeSVGStrOut(HttpServletResponse response, String code) throws Exception {
        Double moduleWidth = Double.valueOf(1.0D);
        //分辨率
        double dpi = 300;
        Double width = 6d;
        Code128Bean bean = new Code128Bean();
        String barcode = code;
        Integer barcodeLength = Integer.valueOf(barcode.length());
        if (barcodeLength.intValue() < 8) {
            moduleWidth = Double.valueOf(2.0D * width.doubleValue());
        } else if (barcodeLength.intValue() < 16) {
            moduleWidth = width;
        }
        if (moduleWidth.doubleValue() < 1.0D) {
            moduleWidth = Double.valueOf(1.0D);
        }
        bean.setModuleWidth(UnitConv.in2mm(moduleWidth.doubleValue() / dpi));
        bean.setBarHeight(30);// 高度
        bean.setQuietZone(60); // 向右偏移
        bean.setMsgPosition(HumanReadablePlacement.HRP_NONE);
        // 设置两侧是否加空白
        bean.doQuietZone(false);
        SVGCanvasProvider canvas = new SVGCanvasProvider(true, 0);
        bean.generateBarcode(canvas, barcode);
        DocumentFragment frag = canvas.getDOMFragment();
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer trans = factory.newTransformer();
        Source src = new DOMSource(frag);
        trans.transform(src, new StreamResult(response.getOutputStream()));
    }

    /**
     * 二维码
     *
     * @param code
     * @param size
     * @return
     * @throws WriterException
     * @throws IOException
     */
    public static String getqrSVGStr(String code, int size) throws Exception {
        return createQRStr(code,size);
    }

    /**
     * 二维码 写流
     *
     * @param response
     * @param code
     * @param size
     * @return
     * @throws WriterException
     * @throws IOException
     */
    public static void getqrSVGStrOut(HttpServletResponse response, String code, int size) throws Exception {
        response.getOutputStream().write(createQRStr(code,size).getBytes(StandardCharsets.UTF_8));
    }

    /**
     * 生成svg字符串
     * @param code
     * @param size
     * @return
     * @throws Exception
     */
    private static String createQRStr(String code, int size) throws Exception {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        hints.put(EncodeHintType.CHARACTER_SET, StandardCharsets.UTF_8.name());
        BitMatrix bitMatrix = qrCodeWriter.encode(code, BarcodeFormat.QR_CODE, size, size, hints);
        StringBuilder sbPath = new StringBuilder();
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        BitArray row = new BitArray(width);
        for (int y = 0; y < height; ++y) {
            row = bitMatrix.getRow(y, row);
            for (int x = 0; x < width; ++x) {
                if (row.get(x)) {
                    sbPath.append(" M" + x + "," + y + "h1v1h-1z");
                }
            }
        }
        StringBuilder sb = new StringBuilder();
        sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        sb.append("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 ").append(width).append(" ").append(height).append("\" stroke=\"none\">\n");
        sb.append("<style type=\"text/css\">\n");
        sb.append(".black {fill:#000000;}\n");
        sb.append("</style>\n");
        sb.append("<path class=\"black\"  d=\"").append(sbPath).append("\"/>\n");
        sb.append("</svg>\n");
        return sb.toString();
    }

}

需要的依赖 

<!--zxing二维码生成 -->
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.3.1</version>
</dependency>
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.2.1</version>
</dependency>
<dependency>
    <groupId>de.erichseifert.vectorgraphics2d</groupId>
    <artifactId>VectorGraphics2D</artifactId>
    <version>0.9.3</version>
</dependency>
<dependency>
    <groupId>net.sf.barcode4j</groupId>
    <artifactId>barcode4j</artifactId>
    <version>2.1</version>
</dependency>
<dependency>
    <groupId>de.erichseifert.vectorgraphics2d</groupId>
    <artifactId>VectorGraphics2D</artifactId>
    <version>0.9.3</version>
</dependency>


  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
英文版的工具名是“Barcode Toolbox”,然后点击“Hide Barcode”。如下图: 之后在工作区会出现如下图的“Barcode Toolbox”工具面板: 通过条形码测量工具可对已创建的条形码进行校验,其校验功能也可以校验由其它条形码工具所生成条形码。在工具箱上选择条形码测量工具,横向划过已生成条形码,在条形码调板的代码字段内即可产生该条形码的构成数值。测量前一定要确保条码是群组后的,测量时测量工具一定要把条码拉完。 在条码工具面板右方还有一个下拉菜单: 下拉菜单内容如下: 下拉菜单的具体含意是: -添加结束区域指示符 -添加上下端鉴别条 -添加左右端鉴别条 -使用OCRB作为缺省字体未完待续 -判读字符居中对齐 -判读字符的高度缩放百分比 PS:一般情况下这些选项保持默认便可,无需更改。 条码制作过程:以 EAN 13 为例 在code栏里输入正确的13位条码号(最后的校验位可不必输入,会自动生成,若你发现自动生成的未位与资料来符,请及时核对你的资料的正确性),输入时你可以不用分隔符隔开,输入完后会自动隔开的。 在code witdth里输入合适的条宽缩小量。条宽缩小量因不同的印刷而有所不同。胶印一般为0.02。凹印一般为0.03。柔印可以为0.05.还有一点要注意:这里的设置要在条码放缩前进行。数值越大出来的条码线条越宽。 在Magnification里输入你想设置的条码放缩的比率。由于印刷的精度,规定条码的放缩比为0.8-2.0之间。最好在0.9-1.2之间。数值越大出来的条码整体越宽。 在bar heigh里输入你想要的条码高度。标准规定EAN-13条码的高度为25mm。所以一般都输入25。你可以将条码截断。一般通过蒙板实现。若要变短,最好小于原条码的1/4,最大不可大于原条码的1/3。数值越大出来的条码整体越高。 参数设置好后点击AI工具栏的条码画笔工具(如下图)在工作区点击便会出现条码。 自我经验:条码数字正常应该是 OCR-B 10 PITCH BT 字体,如若不是请打散条码群组后更改。至于条码图形的高低宽窄可自由等比例调整以适合你的设计要求。最后一定试打一张条码,用条码扫描仪扫描测试成不成功(打开WORD软件,扫描仪对准条码扫出的数字核对一下资料便可

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值