Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QR code detector #31

Open
aidenmurphy89 opened this issue Feb 20, 2021 · 0 comments
Open

QR code detector #31

aidenmurphy89 opened this issue Feb 20, 2021 · 0 comments

Comments

@aidenmurphy89
Copy link

Can you please have a look at the below code and see if there are any reasons why this would not detect a QR code when a photo is taken? for the detectAndDecode method it returns an empty string and detect method it returns 0.

RCT_EXPORT_METHOD(checkForQRCode:(NSString )imageAsBase64) {
RCTLog(@"%@", imageAsBase64);
UIImage
image = [self decodeBase64ToImage:imageAsBase64];

cv::QRCodeDetector qrDecoder = cv::QRCodeDetector();

BOOL isQRPresentResult = [self isQRPresent:image];

NSString *decodedQrData = [self decodeQRCode:image];
//BOOL isQRPresentResult = 1;

//std::string data = qrDecoder.detectAndDecode(matImage);
//std::string data = "testing";

//NSString* result = [NSString stringWithUTF8String:data.c_str()];
//NSString* result = @"test";
RCTLogInfo(@"Pretending to create an event %@", decodedQrData);
RCTLog(isQRPresentResult ? @"yes" : @"No");
};

-(BOOL)isQRPresent:(UIImage*)image{

cv::Mat matImage = [self convertUIImageToCVMat:image];
cv::Mat matImageGrey;
// converting image's color space (RGB) to grayscale
cv::cvtColor(matImage, matImageGrey, cv::COLOR_BGRA2GRAY);
std::vectorcv::Point points;

cv::QRCodeDetector qrDecoder = cv::QRCodeDetector();
return qrDecoder.detect(matImageGrey, points);

};
-(NSString*)decodeQRCode:(UIImage*)image{

cv::Mat matImage = [self convertUIImageToCVMat:image];
cv::Mat matImageGrey;
// converting image's color space (RGB) to grayscale
cv::cvtColor(matImage, matImageGrey, cv::COLOR_BGRA2GRAY);

cv::QRCodeDetector qrDecoder = cv::QRCodeDetector();
std::string qrData;
qrData = qrDecoder.detectAndDecode(matImageGrey);
return [NSString stringWithUTF8String:qrData.c_str()];

};

  • (cv::Mat)convertUIImageToCVMat:(UIImage *)image {
    CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
    CGFloat cols = image.size.width;
    CGFloat rows = image.size.height;

    cv::Mat cvMat(rows, cols, CV_8UC4); // 8 bits per component, 4 channels (color channels + alpha)

    CGContextRef contextRef = CGBitmapContextCreate(cvMat.data, // Pointer to data
    cols, // Width of bitmap
    rows, // Height of bitmap
    8, // Bits per component
    cvMat.step[0], // Bytes per row
    colorSpace, // Colorspace
    kCGImageAlphaNoneSkipLast |
    kCGBitmapByteOrderDefault); // Bitmap info flags

    CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage);
    CGContextRelease(contextRef);

    return cvMat;
    };

  • (UIImage *)decodeBase64ToImage:(NSString *)strEncodeData {
    NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];
    return [UIImage imageWithData:data];
    };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant