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

    如果觉得对您有帮助请关注加粉丝,谢谢!

    package com.bbtree.project.weixin;

    import com.swetake.util.Qrcode;

    import javax.imageio.ImageIO;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.Base64;

    /**
    * Created by Administrator on 2017/1/6.
    */
    //微信二维码生成代码
    public class WeixinCode {
    public static void main(String[] args) {
    String data="柳阮真我爱你!!";
    /**
    * 生成二维码
    */
    encode(data,"F:/weixin/点开有惊喜.JPG");

    /**
    * 解释二维码
    */
    //decode(""C:\\Users\\Administrator\\Desktop\\桌面壁纸"");
    }

    /**
    * 对应的生成二维码的方法
    * @return
    */
    public static boolean encode(String srcValue,String qrcodePicfilePath){
    int MAX_DATA_LENGTH=200;
    byte[] d=srcValue.getBytes();
    int dataLength=d.length;
    int imageWidth=113;
    int imageHeight=imageWidth;
    BufferedImage bi = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_3BYTE_BGR);
    Graphics2D g=bi.createGraphics();
    g.setBackground(Color.WHITE);
    g.clearRect(0,0,imageWidth,imageHeight);
    g.setColor(Color.BLACK);
    if (dataLength>0&&dataLength<=MAX_DATA_LENGTH){
    Qrcode qrcode = new Qrcode();
    qrcode.setQrcodeErrorCorrect('M');
    qrcode.setQrcodeEncodeMode('B');
    qrcode.setQrcodeVersion(5);
    boolean[][] b = qrcode.calQrcode(d);
    int qrodeDataLen=b.length;
    for (int i=0;i<qrodeDataLen;i++){
    for (int j=0;j<qrodeDataLen;j++){
    if (b[j][i]){
    g.fillRect(j*3+2,i*3+2,3,3);
    }
    }
    }
    System.out.println("二维码成功生成!!");
    }else{
    System.out.println(dataLength+"大于"+MAX_DATA_LENGTH);
    return false;
    }
    g.dispose();
    bi.flush();
    File f = new File(qrcodePicfilePath);
    String suffix=f.getName().
    substring(f.getName().indexOf(".")+1,f.getName().length());
    System.out.println("二维码输出成功!!");
    try {
    ImageIO.write(bi,suffix,f);
    }catch (IOException ioe){
    System.out.println("二维码生成失败"+ioe.getMessage());
    return false;
    }
    return true;
    }
    }

     

  • 相关阅读:
    无缘无故出现IDEA无法引入包和类的问题?
    JDK8新特性——Optional
    【工具类】ExcelUtils
    【Mybatis-Plus学习笔记(二)】查询
    【Mybatis-Plus学习笔记(一)】入门
    【Mybatis学习笔记】目录
    【超详细全过程】DataGrip的安装与使用
    【超详细全过程】python环境配置+编辑器安装(python+pycharm)
    【Mybatis工具(五)】雪花算法
    【Mybatis工具(四)】UUID实现原理及其Java实现
  • 原文地址:https://www.cnblogs.com/chenxqNo01/p/6370641.html
Copyright © 2011-2022 走看看