一、问题描述
最近公司有个需求是,从小程序哪里获取二维码,然后从二维码中解析出URL来,自己定制下二维码。。。嫌微信生成的码太难看了。。。微信小程序的获取二维码的文档如下:https://developers.weixin.qq.com/miniprogram/dev/api/qrcode.html 看着这个文档。。。就想吐槽下。。写的太含糊了。
竟然没写返回值是什么。。最后得知,它是以流的形式返回。。。文档里有三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>
<artifactId>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
}
}
}
this.image = new BufferedImage(sourceWidth, sourceHeight, BufferedImage.TYPE_BYTE_GRAY);
this.image.getGraphics().drawImage(image, 0, 0, null);
this.left = left;
this.top = top;
}
@Override
public byte[] getRow(int y, byte[] row) {
if (y < 0 || y >= getHeight()) {
throw new IllegalArgumentException("Requested row is outside the image: " + y);
}
int width = getWidth();
if (row == null || row.length < width) {
row = new byte[width];
}
image.getRaster().getDataElements(left, top + y, width, 1, row);
return row;
}
@Override
public byte[] getMatrix() {
int width = getWidth();
int height = getHeight();
int area = width * height;
byte[] matrix = new byte[area];
image.getRaster().getDataElements(left, top, width, height, matrix);
return matrix;
}
@Override
public boolean isCropSupported() {
return true;
}
@Override
public LuminanceSource crop(int left, int top, int width, int height) {
return new BufferedImageLuminanceSource(image, this.left + left, this.top + top, width, height);
}
@Override
public boolean isRotateSupported() {
return true;
}
@Override
public LuminanceSource rotateCounterClockwise() {
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, sourceWidth);
BufferedImage rotatedImage = new BufferedImage(sourceHeight, sourceWidth, BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g = rotatedImage.createGraphics();
g.drawImage(image, transform, null);
g.dispose();
int width = getWidth();
return new BufferedImageLuminanceSource(rotatedImage, top, sourceWidth - (left + width), getHeight(), width);
}
}
3.二维码工具类
package com.quna.app.pay;
import com.google.zxing.*;
import com.google.zxing.common.HybridBinarizer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* @version: V1.0
* @author: fendo
* @className: QrCodeUtils
* @packageName: com.xxx
* @description: 二维码工具类
* @data: 2018-04-17 14:23
**/
public class QrCodeUtils {
/**
* 解析二维码(QRCode)
* @param image
* @return
*/
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);
Result result = formatReader.decode(binaryBitmap, hints);//开始解析
return result.getText();
}
/**
* 流图片解码
* @param input
* @return String
*/
public static String decodeQrcode(InputStream input) throws NotFoundException, IOException {
BufferedImage image = ImageIO.read(input);
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Map<DecodeHintType,Object> hints = new LinkedHashMap<DecodeHintType,Object>();
// 解码设置编码方式为:utf-8,
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
//优化精度
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
//复杂模式,开启PURE_BARCODE模式
hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
Result result = new MultiFormatReader().decode(bitmap, hints);
return result.getText();
}
}
4.测试类
package com.quna.app.pay;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* @version: V1.0
* @author: fendo
* @className: TestQR
* @packageName: com.xxx
* @description: 二维码测试类
* @data: 2018-04-17 14:23
**/
public class TestQR {
public static void main(String[] args) {
try {
URL url = new URL("https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=你的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("path", "pages/home/index");
paramJson.put("width", 430);
printWriter.write(paramJson.toString());
// flush输出流的缓冲
printWriter.flush();
//开始获取数据
BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
//buff用于存放循环读取的临时数据
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);
/**裁剪原图 目前访问微信 微信返回的是 470*535 像素 170620*/
BufferedImage subImage = image.getSubimage(0, 0, image.getWidth(), (int) (image.getHeight() * 0.85));
System.out.println(QrCodeUtils.decodeQrcode(subImage));
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();
ImageIO.write(inputbig, "jpg", new File("C:\\Users\\luojialin\\Desktop\\jar\\2018-4-20\\124567890.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
}