生成二维码图片

@SuppressWarnings("unchecked")
    @ResponseBody
    @RequestMapping("/downLoadQrcode.do")
    public void downLoadQrcode(HttpServletRequest request,
            HttpServletResponse resp, HttpSession session) throws WriterException, IOException  {
        String activityId=request.getParameter("activityId");
        // 生成二维码图片
        int width = 1000;
        int height = 1000;
        String format = "jpg";
        @SuppressWarnings("rawtypes")
        Hashtable hints = new Hashtable();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        // 访问地址surl
        String surl = "http://m.zhlub.com/common/weixinaccess.html?url=http://**.com";
        BitMatrix bitMatrix = new MultiFormatWriter().encode(surl
                + "/plantAc*/aa.html?activityId=" + activityId+"&scope=snsapi_base", BarcodeFormat.QR_CODE, width,
                height, hints);
        // 二维码存放地址
//        String uploadpath = ConfigUtil.get_instances().getuploadpath() + "qr/r*shop/"+ UUID.randomUUID().toString() + ".jpg";
        // 本地保存的路径
        ServletContext application = request.getSession().getServletContext();
        // 保存的路径
        String uploadpath = application.getRealPath("/")+ "upload/QRCode/" + activityId + ".jpg";
        // 获取的路径
//        String getPath = request.getContextPath()+ "/upload/QRCode/" + activityId + ".jpg";
        String h5domain="http://localhost:8080/";
        String getPath=h5domain+ "/upload/QRCode/" + activityId + ".jpg";
        File file = new File(uploadpath);
        if (!file.exists()) {
            file.mkdirs();
        }
        JSONObject json = new JSONObject();
        try {
          MatrixToImageWriter.writeToFile(bitMatrix, format, file);
            json.put("result","0" );
            json.put("msg", getPath);
            request.setAttribute("getPath", getPath);
        } catch (Exception e) {
            json.put("result", "-1");// 异常
            json.put("msg", e.getMessage());// 异常消息
            e.printStackTrace();
        } finally {
            ado.close();
            PrintWriter out = resp.getWriter();
            out.print(json);
        }

    }
   

猜你喜欢

转载自blog.csdn.net/Grace_An_1/article/details/81297083