一、普通二维码。代码实例(测试在service层)
@Override
public boolean addQrcodefirst(WkrjQrcodefirst wkrjQrcodefirst, HttpServletRequest request) {
String dept_id =wkrjQrcodefirst.getDept_id();
String guid=Guid.getGuid();
String realPath = request.getSession().getServletContext().getRealPath("/") + "/upload/qrcode/";
String realPath1 = request.getSession().getServletContext().getRealPath("/");
File file=new File(realPath);
if(!file .exists()){
file .mkdir();
}
String path="upload/qrcode/"+guid+".png";
//String logorealPath = request.getSession().getServletContext().getRealPath("/") + "/system/imgs/logo11.png";//logo存放的位置
String logorealPath ="";
String encoderContent= request.getScheme() +"://" + request.getServerName() + ":" +request.getServerPort()+request.getContextPath()+"/frontnew/indexfirst.jsp?qrcode_id="+wkrjQrcodefirst.getQrcodefirst_id();
String imgPath=realPath+guid+".png";
List<Map<String,Object>> list=InfomanageDao.getInfomanageBydeptid(dept_id);
if(list.size()>0){
Map<String, Object> map = list.get(0);
if(!StringUtils.isEmpty(map)){
String infomanage_firstUrl=map.get("infomanage_firstUrl")+"";
if(!StringUtils.isEmpty(infomanage_firstUrl)){
logorealPath=request.getSession().getServletContext().getRealPath("/")+infomanage_firstUrl;
}
}
}
try {
QRCodeUtil.encode(encoderContent,logorealPath,imgPath, true);
} catch (Exception e) {
e.printStackTrace();
return false;
}
wkrjQrcodefirst.setQrcodefirst_url(path);
return dao.addQrcodefirst(wkrjQrcodefirst);
}
二、小程序二维码(批量生产)
1、工具类
package com.wkrj.qrcodeUtils;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONObject;
public class XcxQRCode {
/*
* 获取 二维码图片
*
*/
public static boolean getminiqrQr( String accessToken,HttpServletRequest request,String param
,String imgname,String path) {
String p=request.getSession().getServletContext().getRealPath("/");
String codeUrl=p+"/upload/qrcode/"+imgname;
//String twoCodeUrl=imgname;
try
{
URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+accessToken);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");// 提交模式
// conn.setConnectTimeout(10000);//连接超时 单位毫秒
// conn.setReadTimeout(2000);//读取超时 单位毫秒
// 发送POST请求必须设置如下两行
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
// 获取URLConnection对象对应的输出流
PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
// 发送请求参数
JSONObject paramJson = new JSONObject();
paramJson.put("scene", param);
paramJson.put("path", path);
paramJson.put("width", 430);
paramJson.put("is_hyaline", true);
paramJson.put("auto_color", true);
/**
* line_color生效
* paramJson.put("auto_color", false);
* JSONObject lineColor = new JSONObject();
* lineColor.put("r", 0);
* lineColor.put("g", 0);
* lineColor.put("b", 0);
* paramJson.put("line_color", lineColor);
* */
printWriter.write(paramJson.toString());
// flush输出流的缓冲
printWriter.flush();
//开始获取数据
BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
OutputStream os = new FileOutputStream(new File(codeUrl));
int len;
byte[] arr = new byte[1024];
while ((len = bis.read(arr)) != -1)
{
os.write(arr, 0, len);
os.flush();
}
os.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return true;
}
}
2、调用方法
@RequestMapping("creatQrcodeByXcx")
@ResponseBody
public AjaxJson creatQrcodeByXcx(String dept_name,String id,HttpServletRequest request) throws IOException {
AjaxJson json=new AjaxJson();
//小程序
String AppId ="wx*******************"; //小程序appid
String AppSecret ="*******************";//小程序秘钥
String param="dept_id="+id;//(组织机构)部门id
String imgname=id+".png";//生成的图片名称
String path="pages/index/index";//
Token token = CommonUtil.getToken(AppId, AppSecret);//token
String accessToken = token.getAccessToken();
if(XcxQRCode.getminiqrQr(accessToken,request,param,imgname,path)){
qrcodeService.delQrcodeFile(id);//删除原有的数据
boolean c=qrcodeService.addQrcodeFile(id);//添加新的数据
if(c){
json.setSuccess(true);
json.setMsg("二维码生成成功!");
}
}
return json;
}