调用微信识别二维码接口识别图片内的二维码[java]

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

boJIke

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值