JAVA语言获取小程序二维码A,B,C接口讲解及遇到的问题解决
小标 2019-01-09 来源 : 阅读 1480 评论 0

摘要:本文主要向大家介绍了JAVA语言获取小程序二维码A,B,C接口讲解及遇到的问题解决,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

本文主要向大家介绍了JAVA语言获取小程序二维码A,B,C接口讲解及遇到的问题解决,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

一、问题描述


最近公司有个需求是,从小程序哪里获取二维码,然后从二维码中解析出URL来,自己定制下二维码。。。嫌微信生成的码太难看了。

JAVA语言获取小程序二维码A,B,C接口讲解及遇到的问题解决

竟然没写返回值是什么。。最后得知,它是以流的形式返回。。。文档里有三A、B、C三种类型的接口,不清楚微信怎么想的。。。分这么多种接口,做什么。。A,B接口有生成次数限制,B接口没有限制。。


测试代码如下:


public class Test {

    public static void main(String[] args) {

        try {

            URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=你的access_token");

            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", "a=1234567890");

            paramJson.put("page", "pages/index/index");

            paramJson.put("width", 430);

            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("/Users/Xxxx/Music/abc.png"));

            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();

        }

    }

}

   

生成的二维码如下:


但是这种二维码不管怎么搞,都解析不出来二维码中的数据,我试了QRCode.jar与Zxing都不能解析出来,都是报:


com.google.zxing.NotFoundException

   

这种错,网上说是二维码复杂了,或者是中间有LOGO的时候就报错。。但是C接口返回的二维码


通过Zxing解析是也报上面那种错,但是在网上找到了中解决方法,把二维码下面的那串中文去掉就可以了。


二、解决方法


解决方法如下,在微信返回的流中,对图片解析截取:


ByteArrayInputStream inputStream= new ByteArrayInputStream(swapStream.toByteArray());

BufferedImage image = ImageIO.read(inputStream);

/**裁剪原图  目前访问微信 微信返回的是 470*535 像素 170620*/

BufferedImage subImage = image.getSubimage(0, 0, image.getWidth(), (int) (image.getHeight() * 0.85));

   

把下面的中文去掉,效果如下


相对应的URL为:


https://mp.weixin.qq.com/a/~~JtIAAAEESEGtXoPLOk~fPZXTPs0T7Y3SaxauUqasw~~

   

完整代码如下:


1.引入Zxing


<dependency>

    <groupid>com.google.zxing</groupid>

    javase</artifactid>

    <version>3.2.1</version>

</dependency>

   

2.创建BufferedImageLuminanceSource类


package com.quna.app.pay;

 

import com.google.zxing.LuminanceSource;

 

import java.awt.Graphics2D;

import java.awt.geom.AffineTransform;

import java.awt.image.BufferedImage;

 

/**

 * @version: V1.0

 * @author: fendo

 * @className: BufferedImageLuminanceSource

 * @packageName: com.xxx

 * @description:

 * @data: 2018-04-17 14:23

 **/

public final class BufferedImageLuminanceSource extends LuminanceSource {

 

    private final BufferedImage image;

    private final int left;

    private final int top;

 

    public BufferedImageLuminanceSource(BufferedImage image) {

        this(image, 0, 0, image.getWidth(), image.getHeight());

    }

 

    public BufferedImageLuminanceSource(BufferedImage image, int left, int top, int width, int height) {

        super(width, height);

 

        int sourceWidth = image.getWidth();

        int sourceHeight = image.getHeight();

        if (left + width > sourceWidth || top + height > sourceHeight) {

            throw new IllegalArgumentException("Crop rectangle does not fit within image data.");

        }

 

        for (int y = top; y < top + height; y++) {

            for (int x = left; x < left + width; x++) {

                if ((image.getRGB(x, y) & 0xFF000000) == 0) {

                    image.setRGB(x, y, 0xFFFFFFFF); // = white

                }

            }

        }


本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注编程语言JAVA频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程