//spring mvc框架谨慎使用 clean所有缓存再进行启动
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>opencv</artifactId>
<version>4.5.5-1.5.7</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacpp</artifactId>
<version>1.5.7</version>
<classifier>linux-x86_64</classifier>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>openblas</artifactId>
<version>0.3.19-1.5.7</version>
<classifier>linux-x86_64</classifier>
</dependency>
<dependency>
//注意此处jar包为线上liunx使用
<groupId>org.bytedeco</groupId>
<artifactId>opencv</artifactId>
<version>4.5.5-1.5.7</version>
<classifier>linux-x86_64</classifier>
</dependency>
try{
//获取图片进行上传 params 为图片前端给的图片参数 通过url连接下载图片 然后进行扫描识别 如果是直接是file文件 可直接去进行扫码
HttpURLConnection httpUrl = (HttpURLConnection) new URL(params.getFilePath() == null || params.getFilePath() == "" ? params.getUrl() : "" ).openConnection();
httpUrl.connect();
file = inputStreamToFile(httpUrl.getInputStream(),"imageInfo.jpg");
httpUrl.disconnect();
if(file == null ){
return AjaxResult.error("获取图片文件失败,请重新上传!");
}
cqr = WeChatQRCodeUtil.deCode(imread(file.getPath()));
}catch(Exception e){
e....
}
public static File inputStreamToFile(InputStream ins, String name) throws Exception {
File file = new File(System.getProperty("java.io.tmpdir") + File.separator + name);
OutputStream os = new FileOutputStream(file);
int len = 8192;
byte[] buffer = new byte[len];
int bytesRead;
while ((bytesRead = ins.read(buffer, 0, len)) != -1){
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
return file;
}
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;
public class WeChatQRCodeUtil {
public static void main(String[] args) {
Mat img = imread("D:\\imagesc.jpg");
System.out.println(deCode(img).toString().trim());
}
public 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).trim();
}
}