小程序二维码(扫描二维码到小程序页面)

最近公司有在做一款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;
    }}

大致就是这么多,这边我返回的是我们服务器上的一个路径,然后前端获取展示就可以扫描了,具体根据实际业务来。由于占时还没有数据,二维码我就不放上去了,可以自己随意测试。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值