zoukankan      html  css  js  c++  java
  • java生成二维码

    import com.google.zxing.BarcodeFormat;

    import com.google.zxing.EncodeHintType;
    import com.google.zxing.MultiFormatWriter;
    import com.google.zxing.common.BitMatrix;
    import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

    public byte[] getBarcode(String code, Integer width, Integer height)
    throws IOException
    {
    ImageControl ic = new ImageControl();

    BufferedImage buf = ic.getBarcode(code, width, height);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageIO.write(buf, "jpg", os);
    byte[] b = os.toByteArray();
    return b;
    }

    public BufferedImage getBarcode(String str, Integer width, Integer height)
    {
    BufferedImage image = null;
    try
    {
    Hashtable<EncodeHintType, String> hints = new Hashtable();
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    int black = -16777216;
    int white = -1;
    BitMatrix bitMatrix = new MultiFormatWriter().encode(str, BarcodeFormat.CODE_128, width.intValue(), height.intValue(), hints);
    image = new BufferedImage(width.intValue(), height.intValue(), 12);
    for (int x = 0; x < width.intValue(); x++) {
    for (int y = 0; y < height.intValue(); y++) {
    image.setRGB(x, y, bitMatrix.get(x, y) ? black : white);
    }
    }
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    return image;
    }

     相关jar包 core-2-3-0.jar

  • 相关阅读:
    leetcode: power of three 三种解法
    继续写java和socket
    node中的事件发射器
    谈一谈Crsf和XSS攻击
    谈一谈那些框架们
    【Mysql数据库】学习笔记
    【数据库】DML-增删改查-SQL实现
    【Jsp,Servlet初学总结】 含 cookie和session的使用
    struct和typedef struct彻底明白了
    Android 学习之路
  • 原文地址:https://www.cnblogs.com/wanghongwei123/p/7098335.html
Copyright © 2011-2022 走看看