首页 > 代码库 > 带圆角LOGO的QrCode二维码实时生成
带圆角LOGO的QrCode二维码实时生成
最近工作中经常要用到QrCode二维码,研究了一下,写了个带圆角LOGO的JAVA实现,QrCode之所以能在中间放个LOGO图标,是因为编码时的信息冗余。实现的具体代码如下:
方法接口:
import java.io.File; import java.io.OutputStream; public interface QRCodeService { public void generateToStream(String code, OutputStream stream); public void generateToStream(String code, OutputStream stream, int width); public void generateToStream(String code, OutputStream stream, int width, int frontColor); public void generateToStream(String code, OutputStream stream, int width, int frontColor, File logo); }接口实现类:
import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; @Service("qrCodeService") public class QRCodeServiceImpl implements QRCodeService { private static final Logger LOGGER = LoggerFactory.getLogger(QRCodeServiceImpl.class); // 二维码的宽 private static int WIDTH = 250; // 中间图片的宽 private static int IMGWIDTH = 60; // 圆角半径 private static int RADIUS = 10; // 留白填充宽度 private static int MARGIN = 4; private static int FRONTCOLOR = 0x00000000;//0x00808080; /** * 功能描述:生成普通二维码到输出流 */ @Override public void generateToStream(String code, OutputStream stream) { this.generateToStream(code, stream, WIDTH, FRONTCOLOR, null); } @Override public void generateToStream(String code, OutputStream stream, int width) { this.generateToStream(code, stream, width, FRONTCOLOR, null); } @Override public void generateToStream(String code, OutputStream stream, int width, int frontColor) { this.generateToStream(code, stream, width, frontColor, null); } @Override public void generateToStream(String code, OutputStream stream, int width, int frontColor, File logo) { Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 修正容量高 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 边框留白 hints.put(EncodeHintType.MARGIN, 1); BitMatrix matrix = null; try { matrix = new MultiFormatWriter().encode(code, BarcodeFormat.QR_CODE, width, width, hints); } catch (WriterException e) { LOGGER.error("", e); } proc(matrix, stream, frontColor, logo); } public void proc(BitMatrix matrix, OutputStream stream, int frontColor, File logo) { int width = matrix.getWidth(); // 处理后图片的数据 int pixels[] = new int[width * width]; // 中间图片数组数据 int src[][] = null; boolean hashlogo = false; if(logo != null){ src = http://www.mamicode.com/getPic(logo);>控制器类:@Controller @RequestMapping("") public class QrCodeController { private static final Logger LOGGER = LoggerFactory.getLogger(QrCodeController.class); @Resource(name = "qrCodeService") QRCodeService qrCodeService; @RequestMapping("qrcode") public void generateQrCode(HttpServletRequest request,HttpServletResponse response){ try { String code = request.getParameter("url"); if(StringUtils.isBlank(code)){ LOGGER.error("url is null"); return; } String width = request.getParameter("width"); String color = request.getParameter("color"); String logo = request.getParameter("logo"); OutputStream os = response.getOutputStream(); if (StringUtils.isNotBlank(width) && StringUtils.isNotBlank(color) && StringUtils.isNotBlank(logo)){ int my_width = Integer.valueOf(width); int my_color = Integer.valueOf(color); //int my_logo = Integer.valueOf(logo); LOGGER.info("hash logo"); File logofile = new File("logo.jpg"); qrCodeService.generateToStream(code, os, my_width, my_color, logofile); } else if (StringUtils.isNotBlank(width) && StringUtils.isNotBlank(color)){ int my_width = Integer.valueOf(width); int my_color = Integer.valueOf(color); qrCodeService.generateToStream(code, os, my_width, my_color); } else if(StringUtils.isNotBlank(width)) { int my_width = Integer.valueOf(width); qrCodeService.generateToStream(code, os, my_width); } else { qrCodeService.generateToStream(code, os); } os.flush(); os.close(); LOGGER.info("generate qrcode succeed"); } catch (IOException e) { LOGGER.error("generate qrcode error : ", e); } } }生成效果:
带圆角LOGO的QrCode二维码实时生成
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。