直接上代码
下载jar包地址: http://pan.baidu.com/s/1eSxq7ke
package com.util;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import com.swetake.util.Qrcode;
public class CreateTwoBarImage {
public void creatTxm(String param) throws Exception {
Qrcode qrcode = new Qrcode();
qrcode.setQrcodeErrorCorrect('M');
qrcode.setQrcodeEncodeMode('B');
qrcode.setQrcodeVersion(7);
byte[] bstr = param.getBytes("UTF-8");//返回用指定名字命名的字节数组值
BufferedImage bi = new BufferedImage(139, 139,
BufferedImage.TYPE_INT_RGB);//实例化指定参数的BufferedImage
Graphics2D g = bi.createGraphics();//返回一个呈现指定 BufferedImage 的 Graphics2D 对象
g.setBackground(Color.WHITE); // 设置该Graphics2D 对象的背景颜色
g.clearRect(0, 0, 139, 139); //擦除指定的矩形,并且用一个透明的颜色填充它
g.setColor(Color.BLACK); // 条码颜色
if (bstr.length > 0 && bstr.length < 123) {
boolean[][] b = qrcode.calQrcode(bstr); //通过calQrcode函数将byte数组转换成boolean数组 ,然后依据编码后的boolean数组绘图
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b.length; j++) {
if (b[j][i]) {
g.fillRect(j * 3 + 2, i * 3 + 2, 3, 3); //填充指定的矩形
}
}
}
}
g.dispose(); //处理图形上下文,并释放系统资源
bi.flush();//将生成的BufferedImage序列化到磁盘
String FilePath = "D:/" + param + ".jpg";//生成的二维码要存放的文件路径
File f = new File(FilePath);
ImageIO.write(bi, "jpg", f);//将生成的二维码以图片的形式写入相应的文件
}
public static void main(String args[]) {
try {
new CreateTwoBarImage().creatTxm("lmb");
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.item.controller;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.swetake.util.Qrcode;
/**
*
* @author: Yanmh
* @Title: PrintTwoBarController.java
* @Description: 生成2维码
*
* @data: 2017 2017年11月27日 上午11:05:38
*/
@Controller
@RequestMapping("printcon")
public class PrintTwoBarController {
@RequestMapping(value="/img.it")
public void print(HttpServletRequest request,
HttpServletResponse response) throws IOException{
String code = request.getParameter("code");
BufferedImage img = getImg(code);
ImageIO.write(img, "jpg", response.getOutputStream());
}
public BufferedImage getImg(String code) throws UnsupportedEncodingException{
Qrcode testQrcode = new Qrcode();
testQrcode.setQrcodeErrorCorrect('M');
testQrcode.setQrcodeEncodeMode('B');
testQrcode.setQrcodeVersion(7);
byte[] d = code.getBytes("UTF-8");
BufferedImage image = new BufferedImage(98, 98,
BufferedImage.TYPE_BYTE_BINARY);
Graphics2D g = image.createGraphics();
g.setBackground(Color.WHITE);
g.clearRect(0, 0, 98, 98);
g.setColor(Color.BLACK);
if (d.length > 0 && d.length < 120) {
boolean[][] s = testQrcode.calQrcode(d);
for (int i = 0; i < s.length; i++) {
for (int j = 0; j < s.length; j++) {
if (s[j][i]) {
g.fillRect(j * 2 + 3, i * 2 + 3, 2, 2);
}
}
}
}
g.dispose();
image.flush();
return image;
}
}
前台:
<img src="<%=path %>/printcon/img.it?code=http://www.baidu.com" width="100" height="100"/>
效果