如果觉得对您有帮助请关注加粉丝,谢谢!
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;
}
}