1、maven中需要导入的依赖
<!-- ZXing Core 2.2 二维码 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>2.2</version>
</dependency>
2、jsp页面
<div class="col-md-6">
<div class="panel">
<div class="panel-heading">
<span id="my_permissions">推广二维码</span>
<span class="tools pull-right">
<a class="fa fa-chevron-down" href="javascript:;"></a>
<a class="fa fa-times" href="javascript:;"></a>
</span>
</div>
<div class="panel-body">
<div class="pro-codetext">
<div class="pro-codeimg"><img id="shopCode" src="${basePath}/agency/generate_qr_code.shtml" /></div>
</div>
</div>
</div>
</div>
3.controller中:
@Controller
@Scope(value="prototype")
@RequestMapping("agency")
public class AgencyController extends BaseController {
//注入service
@Resource
QRCodeService qrCodeService;
@RequestMapping(value = "generate_qr_code")
public void generate_qr_code(HttpServletRequest request, HttpServletResponse response) {
long id = TokenManager.getToken().getId();
String email = MathUtil.getMD5(TokenManager.getToken().getEmail()).substring(0, 4);
String recommended_code = id + email;
String shopURL = request.getRequestURL().substring(0, request.getRequestURL().indexOf("shiro.demo/"))
+ "shiro.demo/a/register.shtml?recommended_code=" + recommended_code;
System.out.println(shopURL);
// 二维码图片输出流
OutputStream out = null;
try {
response.setContentType("image/jpeg;charset=UTF-8");
BufferedImage image = qrCodeService.createQRCode(shopURL);
// 实例化输出流对象
out = response.getOutputStream();
// 画图
ImageIO.write(image, "png", out);
out.flush();
out.close();
} catch (Exception e) {
logger.error("生成二维码出现异常" + e.getMessage());
e.printStackTrace();
} finally {
try {
if (null != response.getOutputStream()) {
response.getOutputStream().close();
}
if (null != out) {
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
4、service接口
public interface QRCodeService {
public BufferedImage createQRCode(final String url) throws WriterException, IOException;
}
5、serviceImpl 实现类
/*
* QRCodeServiceImpl.java 1.0 2017年2月28日
*
* Copyright (c) 2011, 2014 Tianjin YiDianFu Network Technology Co. Ltd.
* All rights reserved.
*
* This software is the confidential and proprietary information of
* YiDianFu Network Technology Co. Ltd. ("Confidential Information").
* You shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement you
* entered into with YiDianFu.
*/
package com.sojson.agency.service.impl;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Service;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.sojson.agency.service.QRCodeService;
/**
*
*
* @author Administrator 2017年2月28日 下午1:07:55
* @version 1.0
*/
@Service
public class QRCodeServiceImpl implements QRCodeService {
/**
* @param url
* @return
* @throws WriterException
* @throws IOException
*
* @author Administrator 2017年2月28日 下午1:08:22
* @version 1.0
*/
@Override
public BufferedImage createQRCode(String url) throws WriterException, IOException {
BufferedImage image = null;
// 二维码图片输出流
OutputStream out = null;
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
Map hints = new HashMap();
// 设置编码方式
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
// 设置QR二维码的纠错级别(H为最高级别)具体级别信息
/* hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); */
BitMatrix bitMatrix = multiFormatWriter.encode(url, BarcodeFormat.QR_CODE, 400, 400, hints);
bitMatrix = updateBit(bitMatrix, 10);
image = MatrixToImageWriter.toBufferedImage(bitMatrix);
return image;
}
/**
* 自定义白边边框宽度
*
* @param matrix
* @param margin
* @return
*/
private static BitMatrix updateBit(final BitMatrix matrix, final int margin) {
int tempM = margin * 2;
// 获取二维码图案的属性
int[] rec = matrix.getEnclosingRectangle();
int resWidth = rec[2] + tempM;
int resHeight = rec[3] + tempM;
// 按照自定义边框生成新的BitMatrix
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
resMatrix.clear();
// 循环,将二维码图案绘制到新的bitMatrix中
for (int i = margin; i < resWidth - margin; i++) {
for (int j = margin; j < resHeight - margin; j++) {
if (matrix.get(i - margin + rec[0], j - margin + rec[1])) {
resMatrix.set(i, j);
}
}
}
return resMatrix;
}
}
springMvc+spring+mybatis(maven)框架下二维码生成
最新推荐文章于 2021-08-04 16:48:58 发布