一段识别二维码的Python代码
/opencv/QR/rtsp.py
import sys
import cv2
import numpy as np
def qrcode(imgPath):
detect_obj = cv2.wechat_qrcode_WeChatQRCode('D:\opencv\QR\detect.prototxt','D:\opencv\QR\detect.caffemodel','D:\opencv\QR\sr.prototxt','D:\opencv\QR\sr.caffemodel')
img = cv2.imread(imgPath)
res,points = detect_obj.detectAndDecode(img)
return res[0]
if __name__ == '__main__':
a = sys.argv[1]
print(qrcode(a))
在Java代码中调用Python识别二维码的代码,将识别的二维码内容进行返回
public static String weChatQRCode(String imgPath){
String code = "";
try {
String[] args1 = new String[] { "python", "/opencv/QR/rtsp.py", imgPath};
Process proc = Runtime.getRuntime().exec(args1);// 执行py文件
InputStream inputStream = proc.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
code = in.readLine();
in.close();
proc.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return code;
}