生成二维码
<!--生成二维码依赖-->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
##生成普通二维码
public class CodeService {
/*二维码跳转的url*/
@Value("${code.url}")
private String url;
// 嵌入二维码的图片路径
@Value("${code.imgpath}")
private String imgPath;
//生成的二维码的路径
private String destPath;
//嵌入二维码的图片保存地址
private String imgPath_new;
//上传服务器类
@Autowired
private FileUploadController uploadController;
/**
* 生成二维码 * @param qrCode
* @return
*/
public String addQrCode(QrCode qrCode) throws Exception {
URL url = new URL(imgPath);
// 打开URL连接
HttpsURLConnection con = (HttpsURLConnection)
url.openConnection();
// 得到URL的输入流
InputStream input = con.getInputStream();
// 设置数据缓冲
byte[] bs = new byte[1024 * 2];
// 读取到的数据长度
int len;
// 输出的文件流保存图片至本地
File file = new File("");
String uuid = GeneratorUtil.genFileName();
String filePath = file.getCanonicalPath()+"srcmainresourcesstatic";//获取当前项目路径+保存的路径
imgPath_new=filePath+uuid+".jpg"; //图标保存地址
OutputStream os = new FileOutputStream(filePath+uuid+".jpg");
while ((len = input.read(bs)) != -1) {
os.write(bs, 0, len);
}
destPath=filePath+uuid+".jpg"; //二维码存放地址
String url_new="跳转地址"+"?qrId="+qrCode.getId()+
"&communityId="+
qrCode.getCommunityId()+"&name="+
qrCode.getRemarks();
QRCodeUtil.encode(url_new,
imgPath_new,destPath,true);
InputStream stream=new FileInputStream(destPath);
String path = uploadController.inputUplaod(stream); //上传到服务器
os.close();
input.close();
stream.close();
return path;
}
}
生成小程序码(B接口)
//获取AccessToken路径
private static final String AccessToken_URL
= "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";//小程序id
//获取二维码路径
private static final String WxCode_URL
= "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN";//小程序密钥
/**
* 生成微信二维码 * */public static String addWxCode(String accessToken,QrCode qrCodes) {
String path = "";
String filePath = "c://zsyy_test/";
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
String picName = UUID.randomUUID().toString() + ".jpg";
String savePath = filePath+picName;
try{
String wxCodeURL =WxCode_URL.replace
("ACCESS_TOKEN",accessToken);
URL url = new URL(wxCodeURL);
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());
String param = "qrId="+qrCodes.getId()+
"&communityId="+qrCodes.getCommunityId();
// 发送请求参数
JSONObject paramJson = new JSONObject();
paramJson.put("scene", param);
//跳转已经发布的小程序路径
paramJson.put("page", "pages/proper/photograph");
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(savePath));
int len;
byte[] arr = new byte[1024];
while ((len = bis.read(arr)) != -1)
{
os.write(arr, 0, len);
os.flush();
}
File getFile = new File(savePath);
InputStream stream=new FileInputStream(getFile);
FileUploadController upload = new
FileUploadController();
path = upload.inputUplaod(stream); //上传到服务器
os.close();
stream.close();
//删除本地文件
File filePaths = new
File("c://zsyy_test/"+picName);
filePaths.delete();
File getParentFile = new File(filePath);
getParentFile.delete();
}
catch (Exception e)
{
e.printStackTrace();
}
return path;
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。