因为优快云文章要么是过时的,要么就是复制粘贴,让人看的云里雾里,半天没看明白,单纯做个代码搬运工吧,copy下去的代码还是错的。
我将搜索到的结果都点开看了,有的说要下dll文件,有的手动引入jar包,还都提供了资源文件,点进去一看需要积分!让人无语…
以下是我的版本,不需要任何dll文件,按照以下步骤直接用。
第一步引入包
因为微信已经将二维码识别源码提供给了OPENCV所以我们直接引入包就可以了
(这包特别大,建议你要针对不同平台建议按照linux之类的去替换 windows-x86_64这里)
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacpp</artifactId>
<version>1.5.7</version>
<classifier>windows-x86_64</classifier>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>openblas</artifactId>
<version>0.3.19-1.5.7</version>
<classifier>windows-x86_64</classifier>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>opencv</artifactId>
<version>4.5.5-1.5.7</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>opencv</artifactId>
<version>4.5.5-1.5.7</version>
<classifier>windows-x86_64</classifier>
</dependency>
第二步直接创建类测试
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_core.StringVector;
import org.bytedeco.opencv.opencv_wechat_qrcode.WeChatQRCode;
import java.nio.charset.StandardCharsets;
import static org.bytedeco.opencv.global.opencv_imgcodecs.imread;
/**
* @author by Guoshun
* @version 1.0.0
* @description 微信二维码识别,如果有疑问请加qq:1101165230
* @date 2024/3/11 11:40
*/
public class WeChatDeCode {
public static void main(String... args) {
//TODO 路径替换成你的图片路径
Mat img = imread("D:\\other\\temp\\picture\\frame_570.jpg");
System.out.println(deCode(img));
}
private static String deCode(Mat img) {
// 微信二维码对象,要返回二维码坐标前2个参数必传;后2个在二维码小或不清晰时必传。
WeChatQRCode we = new WeChatQRCode();
// List<Mat> points = new ArrayList<Mat>();
// 微信二维码引擎解码,返回的valList中存放的是解码后的数据,points中Mat存放的是二维码4个角的坐标
StringVector stringVector = we.detectAndDecode(img);
if (stringVector.empty()) {
return "0";
}
return stringVector.get(0).getString(StandardCharsets.UTF_8);
}
}
OK!完成,就这么简单!不需要什么复杂过程,不需要什么dll文件。