zoukankan      html  css  js  c++  java
  • java生成二维码图片

    1、POM文件引入

            <dependency>
                <groupId>com.google.zxing</groupId>
                <artifactId>core</artifactId>
                <version>3.2.0</version>
            </dependency>
            <dependency>
                <groupId>com.google.zxing</groupId>
                <artifactId>javase</artifactId>
                <version>3.2.0</version>
            </dependency>

    2、java代码编写,servlet或spring mvc 的Controller

    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 org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import javax.servlet.ServletOutputStream;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.net.URLEncoder;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * Created by user1 on 2015/8/12.
     */
    @Controller
    @RequestMapping(value = "/qrcode", produces = "application/json; charset=UTF-8")
    public class QRCodeController {
    
        private static Logger logger = LoggerFactory.getLogger(QRCodeController.class);
    
    
        /**
         * 生成验证码图片
         * @param response
         * @throws java.io.IOException
         */
        @RequestMapping(value = "/qrcode", produces = "image/png")
        public void createQRCode(
                @RequestParam(value = "type")String type,
                HttpServletRequest request, HttpServletResponse response) throws IOException {
    
            //生成需要分享的二维码,也可以根据此参数生成其他的二维码,只要增加更多else if
            if ("test".equalsIgnoreCase(type)){
    
                //生成二维码图片的内容
                String qrCodeContent = "http://www.coolpeng.cn";
    
                //生成二维码图片
                encodeQRCode(response, qrCodeContent);
    
                logger.debug("qrCodeContent  {}", qrCodeContent);
            }
    
        }
    
    
    
    
    
        /**
         * 生成二维码图片并作为输出流输出到页面上
         * @param resp
         * @param content
         * @throws IOException
         */
        private void encodeQRCode(HttpServletResponse resp,String content) throws IOException {
    
            ServletOutputStream out = resp.getOutputStream();
    
            Map<EncodeHintType, Object> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    
            try {
    
                BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300, hints);
    
                //里面自己会把流close的
                MatrixToImageWriter.writeToStream(bitMatrix, "png", out);
    
            } catch (WriterException | IOException e ) {
                logger.error("",e);
            }
        }
    
    
    }
  • 相关阅读:
    Python 调用multiprocessing模块下面的Process类方法(实现服务器、客户端并发)-TCP协议
    Python开启进程的2中方式
    基于UDP的交互的实例
    Python socket粘包问题(最终解决办法)
    Python socket粘包问题(初级解决办法)
    Python socket套字节
    OSI七层模型
    异常处理
    Python封装与隐藏
    螺旋队列问题
  • 原文地址:https://www.cnblogs.com/lhp2012/p/5144151.html
Copyright © 2011-2022 走看看