|
3 | 3 |
|
4 | 4 | using namespace cv;
|
5 | 5 |
|
6 |
| -std::string qr_scan_opencv(XPtrMat ptr){ |
| 6 | +std::string qr_scan_opencv(XPtrMat ptr, Mat &points){ |
7 | 7 | static QRCodeDetector qrDet = QRCodeDetector();
|
8 |
| - return qrDet.detectAndDecode(get_mat(ptr)); |
| 8 | + return qrDet.detectAndDecode(get_mat(ptr), points); |
9 | 9 | }
|
10 | 10 |
|
11 |
| -std::string qr_scan_wechat(XPtrMat ptr){ |
| 11 | +std::string qr_scan_wechat(XPtrMat ptr, Mat &points){ |
12 | 12 | static wechat_qrcode::WeChatQRCode qrDet = wechat_qrcode::WeChatQRCode();
|
13 |
| - std::vector<std::string> data = qrDet.detectAndDecode(get_mat(ptr)); |
14 |
| - if(data.size() > 0) |
| 13 | + std::vector<Mat> pointsvec; |
| 14 | + std::vector<std::string> data = qrDet.detectAndDecode(get_mat(ptr), pointsvec); |
| 15 | + if(data.size() > 0){ |
| 16 | + points = pointsvec.at(0); |
15 | 17 | return data.at(0);
|
| 18 | + } |
16 | 19 | return std::string();
|
17 | 20 | }
|
18 | 21 |
|
19 | 22 | // [[Rcpp::export]]
|
20 | 23 | Rcpp::RObject cvmat_qrtext(XPtrMat ptr, bool use_wechat = true){
|
21 |
| - std::string data = use_wechat ? qr_scan_wechat(ptr) : qr_scan_opencv(ptr); |
| 24 | + Mat points; |
| 25 | + std::string data = use_wechat ? qr_scan_wechat(ptr, points) : qr_scan_opencv(ptr, points); |
22 | 26 | if(data.length()){
|
23 | 27 | return Rcpp::CharacterVector::create(data);
|
24 | 28 | }
|
25 | 29 | return R_NilValue;
|
26 | 30 | }
|
27 | 31 |
|
28 | 32 | // [[Rcpp::export]]
|
29 |
| -XPtrMat cvmat_qrmask(XPtrMat ptr){ |
30 |
| - static QRCodeDetector qrDet = QRCodeDetector(); |
31 |
| - Mat img = get_mat(ptr); |
32 |
| - std::vector<Point> points; |
33 |
| - std::string data = qrDet.detectAndDecode(img, points); |
34 |
| - if (data.length() && !points.empty()) { |
35 |
| - for (int i = 0; i < points.size(); i++) { |
36 |
| - Point pt1 = points[i]; |
37 |
| - Point pt2 = points[(i + 1) % 4]; |
| 33 | +SEXP cvmat_qrmask(XPtrMat ptr, bool use_wechat = true){ |
| 34 | + Mat points; |
| 35 | + std::string data = use_wechat ? qr_scan_wechat(ptr, points) : qr_scan_opencv(ptr, points); |
| 36 | + if (data.length()) { |
| 37 | + cv::Mat img = get_mat(ptr); |
| 38 | + if(!use_wechat) |
| 39 | + points = points.reshape(1, 4); |
| 40 | + for (int i = 0; i < points.size().height; i++) { |
| 41 | + Point pt1 = cv::Point(points.row(i)); |
| 42 | + Point pt2 = cv::Point(points.row((i + 1) % 4)); |
38 | 43 | line(img, pt1, pt2, Scalar(255, 0, 0), 3);
|
39 | 44 | }
|
40 | 45 | cv::putText(img, data,
|
41 |
| - cv::Point(10, img.rows / 2), //top-left position |
| 46 | + cv::Point(10, img.rows / 2), //left-middle position |
42 | 47 | cv::FONT_HERSHEY_DUPLEX,
|
43 | 48 | 1.0,
|
44 | 49 | CV_RGB(118, 185, 0), //font color
|
45 | 50 | 2);
|
46 | 51 | ptr.attr("value") = Rcpp::CharacterVector::create(data);
|
| 52 | + return ptr; |
47 | 53 | }
|
48 |
| - return ptr; |
| 54 | + return R_NilValue; |
49 | 55 | }
|
50 | 56 |
|
0 commit comments