Skip to content

Commit 5d8a380

Browse files
committed
Wip
1 parent d529d86 commit 5d8a380

File tree

4 files changed

+40
-35
lines changed

4 files changed

+40
-35
lines changed

R/RcppExports.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ cvmat_qrtext <- function(ptr, use_wechat = TRUE) {
157157
.Call('_opencv_cvmat_qrtext', PACKAGE = 'opencv', ptr, use_wechat)
158158
}
159159

160-
cvmat_qrmask <- function(ptr) {
161-
.Call('_opencv_cvmat_qrmask', PACKAGE = 'opencv', ptr)
160+
cvmat_qrmask <- function(ptr, use_wechat = TRUE) {
161+
.Call('_opencv_cvmat_qrmask', PACKAGE = 'opencv', ptr, use_wechat)
162162
}
163163

164164
cvversion <- function() {

R/qr.R

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
#' Detect QR codes
22
#'
3-
#' Detect a QR code
3+
#' Detect a QR code from an image. The `ocv_qrtext` function returns the text
4+
#' content from the QR, and `ocv_qrmask` returns an annotated image with the QR
5+
#' area highlighted, and the QR text as an attribute.
46
#'
57
#' @export
68
#' @inheritParams ocv_write
79
#' @rdname qrcode
10+
#' @param use_wechat use the wechat implementation.
811
#' @examples
912
#' png("test.png")
1013
#' plot(qrcode::qr_code("This is a test"))
1114
#' dev.off()
1215
#' ocv_qrtext(ocv_read('test.png'))
1316
#' unlink("test.png")
14-
ocv_qrtext <- function(image){
15-
use_wechat <- TRUE
17+
ocv_qrtext <- function(image, use_wechat = TRUE){
18+
use_wechat <- as.logical(use_wechat)
1619
cvmat_qrtext(image, use_wechat)
1720
}
1821

@@ -22,18 +25,13 @@ ocv_qrtext <- function(image){
2225
#' # QR code scanner
2326
#' ocv_camera(ocv_qrmask)
2427
#' }
25-
ocv_qrmask <- function(image){
26-
cvmat_qrmask(image)
28+
ocv_qrmask <- function(image, use_wechat = TRUE){
29+
use_wechat <- as.logical(use_wechat)
30+
cvmat_qrmask(image, use_wechat)
2731
}
2832

2933
#' @export
3034
#' @rdname qrcode
3135
qr_scanner <- function(){
32-
ocv_video(function(im){
33-
out <- ocv_qrmask(im)
34-
value <- attr(out, 'value')
35-
if(length(value)){
36-
list(out = out, value = value)
37-
}
38-
}, stop_on_result = TRUE)
36+
ocv_video(ocv_qrmask, stop_on_result = TRUE)
3937
}

src/RcppExports.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -468,13 +468,14 @@ BEGIN_RCPP
468468
END_RCPP
469469
}
470470
// cvmat_qrmask
471-
XPtrMat cvmat_qrmask(XPtrMat ptr);
472-
RcppExport SEXP _opencv_cvmat_qrmask(SEXP ptrSEXP) {
471+
SEXP cvmat_qrmask(XPtrMat ptr, bool use_wechat);
472+
RcppExport SEXP _opencv_cvmat_qrmask(SEXP ptrSEXP, SEXP use_wechatSEXP) {
473473
BEGIN_RCPP
474474
Rcpp::RObject rcpp_result_gen;
475475
Rcpp::RNGScope rcpp_rngScope_gen;
476476
Rcpp::traits::input_parameter< XPtrMat >::type ptr(ptrSEXP);
477-
rcpp_result_gen = Rcpp::wrap(cvmat_qrmask(ptr));
477+
Rcpp::traits::input_parameter< bool >::type use_wechat(use_wechatSEXP);
478+
rcpp_result_gen = Rcpp::wrap(cvmat_qrmask(ptr, use_wechat));
478479
return rcpp_result_gen;
479480
END_RCPP
480481
}
@@ -529,7 +530,7 @@ static const R_CallMethodDef CallEntries[] = {
529530
{"_opencv_cvmat_hog", (DL_FUNC) &_opencv_cvmat_hog, 1},
530531
{"_opencv_cvmat_markers", (DL_FUNC) &_opencv_cvmat_markers, 1},
531532
{"_opencv_cvmat_qrtext", (DL_FUNC) &_opencv_cvmat_qrtext, 2},
532-
{"_opencv_cvmat_qrmask", (DL_FUNC) &_opencv_cvmat_qrmask, 1},
533+
{"_opencv_cvmat_qrmask", (DL_FUNC) &_opencv_cvmat_qrmask, 2},
533534
{"_opencv_cvversion", (DL_FUNC) &_opencv_cvversion, 0},
534535
{NULL, NULL, 0}
535536
};

src/qrdetect.cpp

+23-17
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,54 @@
33

44
using namespace cv;
55

6-
std::string qr_scan_opencv(XPtrMat ptr){
6+
std::string qr_scan_opencv(XPtrMat ptr, Mat &points){
77
static QRCodeDetector qrDet = QRCodeDetector();
8-
return qrDet.detectAndDecode(get_mat(ptr));
8+
return qrDet.detectAndDecode(get_mat(ptr), points);
99
}
1010

11-
std::string qr_scan_wechat(XPtrMat ptr){
11+
std::string qr_scan_wechat(XPtrMat ptr, Mat &points){
1212
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);
1517
return data.at(0);
18+
}
1619
return std::string();
1720
}
1821

1922
// [[Rcpp::export]]
2023
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);
2226
if(data.length()){
2327
return Rcpp::CharacterVector::create(data);
2428
}
2529
return R_NilValue;
2630
}
2731

2832
// [[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));
3843
line(img, pt1, pt2, Scalar(255, 0, 0), 3);
3944
}
4045
cv::putText(img, data,
41-
cv::Point(10, img.rows / 2), //top-left position
46+
cv::Point(10, img.rows / 2), //left-middle position
4247
cv::FONT_HERSHEY_DUPLEX,
4348
1.0,
4449
CV_RGB(118, 185, 0), //font color
4550
2);
4651
ptr.attr("value") = Rcpp::CharacterVector::create(data);
52+
return ptr;
4753
}
48-
return ptr;
54+
return R_NilValue;
4955
}
5056

0 commit comments

Comments
 (0)