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

  • 相关阅读:
    Quartz任务调度系统,克隆表达式
    Java故障分析基础
    SpringData JPA整理
    Mockito教程
    SpringMVC单元测试-MockMvc
    SpringBoot Actuator & SpringBoot Admin
    Swagger
    Spring @AliasFor
    java命令查询属性信息
    windows版本免安装redis, nginx, zookeeper
  • 原文地址:https://www.cnblogs.com/wanghongwei123/p/7098335.html
Copyright © 2011-2022 走看看