<dependency>
java
<groupId>QRCode</groupId>
web
<artifactId>QRCode</artifactId>
ajax
<version>3.0</version>
spring
</dependency>
app
public class CreateQRCode {
maven
/**
ide
* 建立二維碼
工具
* @param qrData 生成二維碼中要存儲的信息
this
* @param path 二維碼圖片存儲路徑 eg:"D:/qrcode.png"
spa
* @throws Exception
*/
public static boolean creatQrcode(String qrData, String path) {
try {
Qrcode qrcode = new Qrcode();
qrcode.setQrcodeErrorCorrect('M');//糾錯等級(分爲L、M、H三個等級)
qrcode.setQrcodeEncodeMode('B');//N表明數字,A表明a-Z,B表明其它字符
qrcode.setQrcodeVersion(7);//版本
//設置一下二維碼的像素
int width = 67 + 12 * (7 - 1);
int height = 67 + 12 * (7 - 1);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//繪圖
Graphics2D gs = bufferedImage.createGraphics();
gs.setBackground(Color.WHITE);
gs.setColor(Color.BLACK);
gs.clearRect(0, 0, width, height);//清除下畫板內容
//設置下偏移量,若是不加偏移量,有時會致使出錯。
int pixoff = 2;
byte[] d = qrData.getBytes("utf-8");
if (d.length > 0 && d.length < 120) {
boolean[][] s = qrcode.calQrcode(d);
for (int i = 0; i < s.length; i++) {
for (int j = 0; j < s.length; j++) {
if (s[j][i]) {
gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
}
}
}
}
gs.dispose();
bufferedImage.flush();
ImageIO.write(bufferedImage, "png", new File(path));
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 解析二維碼(QRCode)
*
* @param imgPath 圖片路徑
* @return
*/
public static String decoderQRCode(String imgPath) {
//QRCode 二維碼圖片的文件
File imageFile = new File(imgPath);
BufferedImage bufImg = null;
String content = null;
try {
bufImg = ImageIO.read(imageFile);
QRCodeDecoder decoder = new QRCodeDecoder();
content = new String(decoder.decode(new TwoDimensionCodeImage(bufImg)), "utf-8");
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
} catch (DecodingFailedException dfe) {
System.out.println("Error: " + dfe.getMessage());
dfe.printStackTrace();
}
return content;
}
}
class TwoDimensionCodeImage implements QRCodeImage {
//BufferedImage做用將一幅圖片加載到內存中
BufferedImage bufImg;
public TwoDimensionCodeImage(BufferedImage bufImg) {
this.bufImg = bufImg;
}
@Override
public int getWidth() {
return bufImg.getWidth();//返回像素寬度
}
@Override
public int getHeight() {
return bufImg.getHeight();//返回像素高度
}
@Override
public int getPixel(int i, int i1) {
return bufImg.getRGB(i, i1);//獲得長寬值,即像素值,i,i1表明像素值
}
}
package com.st.project.controller;
import com.st.project.common.AjaxResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import static com.st.project.common.CreateQRCode.creatQrcode;
import static com.st.project.common.CreateQRCode.decoderQRCode;
/**
* 建立二維碼
*/
@Controller
@RequestMapping("/qrcode")
public class QrcodeController {
@Value("${portals.upload.image.path}")
private String qrcodePath; //二維碼存儲路徑
/**
* 建立二維碼
* @return
*/
@ResponseBody
@PostMapping("/add.dd")
public AjaxResult addQrcode(HttpServletRequest request){
AjaxResult ajaxResult = new AjaxResult();
ajaxResult.setState(false);
String qrData=request.getParameter("qrData");
String qrSuffix=request.getParameter("qrSuffix");
String qrcode=System.currentTimeMillis()+"."+qrSuffix;
String path=qrcodePath+qrcode;
boolean getQrcode=creatQrcode(qrData,path);
if(getQrcode==true){
ajaxResult.setState(true);
ajaxResult.setData(qrcode);
}
return ajaxResult;
}
/**
* 解析二維碼
* @return
*/
@ResponseBody
@PostMapping("/decoder.dd")
public AjaxResult decoderQrcode(HttpServletRequest request){
AjaxResult ajaxResult = new AjaxResult();
ajaxResult.setState(false);
String qrcode=request.getParameter("qrcode");
String qrData=decoderQRCode(qrcodePath+qrcode);
if(qrData!=null && !"".equals(qrData)){
ajaxResult.setState(true);
ajaxResult.setData(qrData);
}
return ajaxResult;
}
}
此時已生成一張名爲qrcode.png的二維碼圖片: