0

This the code read QRcode

- (instancetype)init {
    if (self = [super init]) {

        if (self.session == nil)
            self.session = [[AVCaptureSession alloc] init];

        //device
        if (self.device == nil)
            self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

        //output
        if (self.output == nil)
            self.output = [[AVCaptureMetadataOutput alloc] init];
    }
    return self;
}

- (void)creatScanQR{

    NSError *error = nil;
    //input
    if (self.input == nil)
        self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:&error];

    if(self.input) {
        [self.session addInput:self.input];
    } else {
        NSLog(@"%@", error);
        return;
    }

    [self.session addOutput:self.output];
    [self.output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
    [self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    [self.session startRunning];
}

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {

    for (AVMetadataMachineReadableCodeObject *metadata in metadataObjects) {
        if ([metadata.type isEqualToString:AVMetadataObjectTypeQRCode]) {

            NSLog(@"======%@=======",metadata.stringValue);
        }
    }
}

It works in native app. But my app is build by Unity, it used Vuforia, When I use AVCapture read QRcode, vuforia is black screen. Because camera are only one which is using by Voforia. How can I use AVCaptureInput to read QRcode and vuforia is still working?

My planB is get the vuforia view , write a image by vuforia view , use iOS CIDetector read the qrcode, but I got a nil image.why ?

    UIView *view =  UnityGetGLView();
    UIGraphicsBeginImageContext(view.bounds.size);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image= UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData *imageData = UIImagePNGRepresentation(image);
    CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:[CIContext contextWithOptions:nil] options:@{CIDetectorAccuracy:CIDetectorAccuracyLow}];
    NSArray *features = [detector featuresInImage:[CIImage imageWithData:imageData]];
    for (CIFeature *feature in features) {
         NSLog(@"%@",feature.type);
         if ([feature isKindOfClass:[CIQRCodeFeature class]]) {
             NSLog(@"?????? %@ ????? ", ((CIQRCodeFeature *)feature).messageString);
             dispatch_sync(queue, ^{ dispatch_suspend(timer); });
         }
     }
ChokWah
  • 105
  • 8

2 Answers2

0

sounds like your problem is caused by competition of using camera. The only solution may be to use the exactly same view in vuforia and QRCode reading, namely workaround the competition, and share the camera images between vuforia and QRCode reading.

  • yeah, I think so. So I want get the same view with Vuforia. How to get the same view. I wannt get it in xcode . – ChokWah Mar 30 '17 at 11:59
0

I make some change for Plan B. Use GCD timer every second get the image from rootview, the shot view method is

- (BOOL)drawViewHierarchyInRect:(CGRect)rect afterScreenUpdates:(BOOL)afterUpdates NS_AVAILABLE_IOS(7_0);

.Then CIDetector read then qrcode from image.It works.It looks well.

I don't know how many bugs in it.But there is no way to do. Boss pushed me if I didn't use plan b. He wants Vuforia can read qrcode same time, and quickly no slowly.So problem temporarily solved . If you have better idea, love to listen.

ChokWah
  • 105
  • 8