java生成二维码,并且给二维码添加logo

之前有写过类似的,实现方式大致上都是一样的

package com.bus.wx.action.code;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import com.bus.plugin.wx.action.WxAction;
import com.bus.wx.util.Limits;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.lys.sys.log.Log;

/**
* 条形码、二维码
* @author hwj
*
*/
@Scope(value = "prototype")
@Controller("Bar_Qr_Code_Action")
@RequestMapping(value="plug/wx/wwz/{bcflag}/barqrcode")
public class Bar_Qr_Code_Action extends WxAction{

\tprivate static final int LogoPart = 4;
\tprivate static final int BLACK = 0xFF000000;//用于设置图案的颜色
private static final int WHITE = 0xFFFFFFFF; //用于背景色
String format = "png";



public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, (matrix.get(x, y) ? BLACK : WHITE));
// image.setRGB(x, y, (matrix.get(x, y) ? Color.YELLOW.getRGB() : Color.CYAN.getRGB()));
}
}
return image;
}

\t public BufferedImage LogoMatrix(BufferedImage matrixImage) throws IOException{
\t /**
\t * 读取二维码图片,并构建绘图对象
\t */
\t Graphics2D g2 = matrixImage.createGraphics();
\t
\t int matrixWidth = matrixImage.getWidth();
\t int matrixHeigh = matrixImage.getHeight();
\t
\t /**
\t * 读取Logo图片
\t */
\t String path=request.getSession().getServletContext().getRealPath("/images/logo/dzjkklog.png");
\t BufferedImage logo = ImageIO.read(new File(path));
\t
\t //开始绘制图片
\t g2.drawImage(logo,matrixWidth/5*2,matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5, null);//绘制
\t BasicStroke stroke = new BasicStroke(5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
\t g2.setStroke(stroke);// 设置笔画对象
\t //指定弧度的圆角矩形
\t RoundRectangle2D.Float round = new RoundRectangle2D.Float(matrixWidth/5*2, matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5,20,20);
\t g2.setColor(Color.white);
\t g2.draw(round);// 绘制圆弧矩形
\t
\t //设置logo 有一道灰色边框
\t BasicStroke stroke2 = new BasicStroke(1,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
\t g2.setStroke(stroke2);// 设置笔画对象
\t RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(matrixWidth/5*2+2, matrixHeigh/5*2+2, matrixWidth/5-4, matrixHeigh/5-4,20,20);
\t g2.setColor(new Color(128,128,128));
\t g2.draw(round2);// 绘制圆弧矩形
\t
\t g2.dispose();
\t matrixImage.flush() ;
\t return matrixImage ;
\t }
\t
\t
\t/**
\t * 生成二维码:直接将生成的二维码传输到前台页面
\t * @param bcflag
\t */
\t@RequestMapping(value="createQrCodes",method = RequestMethod.GET)
\tpublic void createQrCodes(@PathVariable String bcflag,String dastid){
\t\t
\t\tString url="扫二维码出现的内容";
\t\tif(url!=null&&!"".equals(url)){
\t\t\tServletOutputStream stream=null;
\t\t\ttry {
\t\t\t\tint width=430;
\t\t\t\tint height=430;
\t\t\t\tstream=response.getOutputStream();
\t\t\t\tQRCodeWriter writer=new QRCodeWriter();
\t\t\t\t Hashtable<encodehinttype> hints = new Hashtable<encodehinttype>();
\t\t\t
\t\t\t hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
\t\t\t hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
\t\t\t hints.put(EncodeHintType.MARGIN, 1);//设置二维码边的空度,非负数
\t\t\t BitMatrix bitMatrix = new MultiFormatWriter().encode(url,//要编码的内容
\t\t\t BarcodeFormat.QR_CODE,
\t\t\t width, //条形码的宽度
\t\t\t height, //条形码的高度
\t\t\t hints);
\t\t\t\t//BitMatrix m=writer.encode(url, BarcodeFormat.QR_CODE, height,width);
\t\t\t\t BufferedImage image = toBufferedImage(bitMatrix);
\t\t\t image = LogoMatrix(image);
\t\t\t if (!ImageIO.write(image, format, stream)) {
\t\t\t throw new IOException("Could not write an image of format " + format);
\t\t\t }
\t\t\t\t} catch (Exception e) {
\t\t\t\t\tLog.in.info(e.getMessage());
\t\t\t\t}finally{
\t\t\t\t\tif(stream!=null){
\t\t\t\t\t\ttry {
\t\t\t\t\t\t\tstream.flush();
\t\t\t\t\t\t\tstream.close();
\t\t\t\t\t\t\t} catch (IOException e) {
\t\t\t\t\t\t\t\tLog.in.info(e.getMessage());
\t\t\t\t\t\t\t}
\t\t\t\t\t}
\t\t\t\t}
\t\t }
\t}
}/<encodehinttype>/<encodehinttype>


原文:https://blog.csdn.net/qq_34707456/article/details/103462624


分享到:


相關文章: