生成小程序二维码
引入依赖
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-jpeg</artifactId>
<version>3.0-rc5</version>
</dependency>
接口C:普通方形二维码
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appKey&secret=secretKey
String result = HttpClient4.doGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appKey&secret=secretKey");
Map map = (Map) JSON.parse(result);
String access_token = StringUtils.getStringByObj(map.get("access_token"));
String url ="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=";
Map<String, Object> createMap = new HashMap<>();
createMap.put("path", "pages/clodRecord/index/index?traceCode="+URLEncoder.encode(record.getQrCode()));
createMap.put("width", "430");
Object json = JSON.toJSON(createMap);
String res = this.httpPostWithJSON(url + access_token, json.toString(),URLEncoder.encode(record.getQrCode()));
String result1 = this.changeImage(res,record.getProcName() + " NO." + record.getProductionBach());
renderJson(ErrCode.retData(ErrCode.SUCCESS, "data:image/png;base64,"+result1));
public String httpPostWithJSON(String url, String json,String id)
throws Exception {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
StringEntity se = new StringEntity(json);
se.setContentType("application/json");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"UTF-8"));
httpPost.setEntity(se);
HttpResponse response = httpClient.execute(httpPost);
String uploadSysUrl = super.getRequest().getServletContext().getRealPath("")+"/tempQrCode/";
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
InputStream instreams = resEntity.getContent();
System.out.println("uploadSysUrl="+uploadSysUrl);
File saveFile = new File(uploadSysUrl+id+".png");
if (!saveFile.getParentFile().exists()) {
saveFile.getParentFile().mkdirs();
}
saveToImgByInputStream(instreams, uploadSysUrl, id+".png");
}
}
httpPost.abort();
return uploadSysUrl+id+".png";
}
public static int saveToImgByInputStream(InputStream instreams,String imgPath,String imgName){
int stateInt = 1;
if(instreams != null){
try {
File file=new File(imgPath+imgName);
FileOutputStream fos=new FileOutputStream(file);
byte[] b = new byte[1024];
int nRead = 0;
while ((nRead = instreams.read(b)) != -1) {
fos.write(b, 0, nRead);
}
fos.flush();
fos.close();
} catch (Exception e) {
stateInt = 0;
e.printStackTrace();
} finally {
try {
instreams.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return stateInt;
}
public String changeImage(String imageurl,String text ){
try {
String logoPath = super.getRequest().getServletContext().getRealPath("/img") + "\\icon.png";
InputStream imagein = new FileInputStream(logoPath);
InputStream imagein2 = new FileInputStream(imageurl);
BufferedImage image = ImageIO.read(imagein);
System.out.println("logoPath="+image.getHeight()+"==||=="+image.getWidth());
BufferedImage image2 = ImageIO.read(imagein2);
System.out.println("imageurl="+image2.getHeight()+"==||=="+image2.getWidth());
Graphics g = image2.getGraphics();
g.drawImage(image, image2.getWidth()/2-60,image2.getHeight()/2-90,new Double(image2.getWidth()/4).intValue(),new Double(image2.getHeight()/4.5).intValue(),null);
OutputStream outImage = new FileOutputStream(imageurl);
JPEGImageEncoder enc = JPEGCodec.createJPEGEncoder(outImage);
enc.encode(image2);
BufferedImage bimg=ImageIO.read(new FileInputStream(imageurl));
image2 = this.withLogo(bimg, text);
String base64Data = new String(Base64.getEncoder().encode(QRCodeUtils.getByte(image2)));
imagein.close();
imagein2.close();
outImage.close();
return base64Data;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
接口B 生成的二维码
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("scene", reqJson.getString("traceCode"));
paramMap.put("scene", record.getQrCode());
paramMap.put("path", "pages/clodRecord/index/index");
paramMap.put("page", "pages/clodRecord/index/index");
paramMap.put("width", 430);
paramMap.put("auto_color", false);
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + access_token);
post.setEntity(new StringEntity(JSON.toJSONString(paramMap), ContentType.APPLICATION_JSON));
CloseableHttpResponse response = client.execute(post);
byte[] bytes = EntityUtils.toByteArray(response.getEntity());
response.close();
String base64Data = new String(Base64.getEncoder().encode(bytes));
renderJson(ErrCode.retData(ErrCode.SUCCESS, "data:image/png;base64," + base64Data));