最近公司有在做一款APP和小程序,里面有一个扫一扫调到小程序页面的功能,所以就写了下面的一些内容,记录一下:
public class QrCodeUtils {
public static String decodeQrcode(BufferedImage image) throws NotFoundException {
MultiFormatReader formatReader = new MultiFormatReader();
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
//定义二维码的参数:
Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");//定义字符集
hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
com.google.zxing.Result result = formatReader.decode(binaryBitmap, hints);//开始解析
return result.getText();
}
public static String get(String url) {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
StringBuffer stringBuffer = new StringBuffer();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStreamReader inputStreamReader = new InputStreamReader(entity.getContent(), "utf-8");
char[] charbuffer;
while (inputStreamReader.read(charbuffer = new char[10]) > 0) {
stringBuffer.append(charbuffer);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
httpGet.releaseConnection();
}
return stringBuffer.toString();
}
public static String getToken() {
try {
String result = get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" +Status.appid_s + "&secret=" + Status.secret_s);
//我这边采用了截取的方法,不知道为什么 我在本地测试的话下面注释的方法是可以的,一放到服务器就不行了,我就用了最笨的方法截取,也可以采用下面我注释的方法
int index = result.indexOf(":");
String newStr = result.substring(index + 1);
int indexs = newStr.indexOf("\"");
String newStrs = newStr.substring(indexs + 1);
String access_token=newStrs.substring(0, newStrs.indexOf("\""));
// String access_token = JSONObject.parseObject(result).getString("access_token");
if (access_token != null || access_token != "") {
return access_token;
} else {
return Status.message_null;
}
} catch (Exception e) {
e.printStackTrace();
return Status.message_fail;
}
}
public static String getResult(int punchid,int userid){
String lastName=null;
String returnPath = "";
try {
URL url = new URL("https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=" + getToken());
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();
//这里是参数 不懂得可以看微信官方文档,不能写到path里面
paramJson.put("scene", punchid);//参数
//这里要写二维码的路径
paramJson.put("path", "pages/discover/sign_detail/index");
paramJson.put("width", 430);
printWriter.write(paramJson.toString());
// flush输出流的缓冲
printWriter.flush();
//开始获取数据
BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[100];
int rc = 0;
while ((rc = bis.read(buff, 0, 100)) > 0) {
swapStream.write(buff, 0, rc);
}
ByteArrayInputStream inputStream = new ByteArrayInputStream(swapStream.toByteArray());
BufferedImage image = ImageIO.read(inputStream);
BufferedImage subImage = image.getSubimage(0, 0, image.getWidth(), (int) (image.getHeight() * 0.85));
BufferedImage inputbig = new BufferedImage(256, 256, BufferedImage.TYPE_INT_BGR);
Graphics2D g = (Graphics2D) inputbig.getGraphics();
g.drawImage(subImage, 0, 0, 256, 256, null);
g.dispose();
inputbig.flush();
String dateName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
// 重构文件名称
String guid = RandomUtil.getYcode();
String images = dateName+guid + ".jpg" ;
//这边是我们公司自己的上传图片的方法,也可以采用下面注解的方法测试下载到本地
returnPath = FormUtil.aliyunImageUploadByByte(swapStream.toByteArray(), Status.public_img, images);
// StringBuffer wayName=new StringBuffer("D:/data");
// wayName.append(dateName+".jpg");
// lastName=wayName.toString();
// pic_url="https://"+bucketName+"."+endpoint+"/"+filename; Status.public_img
// boolean jpg = ImageIO.write(inputbig, "jpg", new File(lastName));
} catch (Exception e) {
e.printStackTrace();
}
return returnPath;
}}
大致就是这么多,这边我返回的是我们服务器上的一个路径,然后前端获取展示就可以扫描了,具体根据实际业务来。由于占时还没有数据,二维码我就不放上去了,可以自己随意测试。