Untitled

 avatar
unknown
objectivec
a year ago
2.7 kB
0
Indexable
```
- (void) setupCamera {
    __weak typeof (self) weakSelf = self;
    self.textRequest = [[VNRecognizeTextRequest alloc] initWithCompletionHandler:^(VNRequest * _Nonnull request, NSError * _Nullable error) {
        __strong typeof(self) strongSelf = weakSelf;
        if (!strongSelf.isScanning) {
            return;
        }
        if (error) {
            NSLog(@"Tanishq_hello_got_error_in_setupcamera");
            return;
        }
        [strongSelf processVNRequest:request];
    }];
    AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithDeviceType:AVCaptureDeviceTypeBuiltInWideAngleCamera mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionBack];
    
    self.captureDevice = captureDevice;
    self.captureSession = [[AVCaptureSession alloc] init];
    self.captureSession.sessionPreset = AVCaptureSessionPreset1920x1080;
    
    NSError *deviceInputError;
    AVCaptureDeviceInput *deviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:captureDevice error:&deviceInputError];
    if (deviceInputError) {
        // [self stopWithError:[STPCardScanner stp_cardScanningError]];
        return;
    }
    
    if ([self.captureSession canAddInput:deviceInput]) {
        [self.captureSession addInput:deviceInput];
    } else {
        // [self stopWithError:[STPCardScanner stp_cardScanningError]];
        return;
    }
    
    self.videoDataOutputQueue = dispatch_queue_create("tanishq.VideoDataOutputQueue", nil);
    self.videoDataOutput = [[AVCaptureVideoDataOutput alloc] init];
    self.videoDataOutput.alwaysDiscardsLateVideoFrames = YES;
    [self.videoDataOutput setSampleBufferDelegate:self queue:self.videoDataOutputQueue];
    
    [self.videoDataOutput setVideoSettings:@{(id)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)}];
        
    if ([self.captureSession canAddOutput:self.videoDataOutput]) {
        NSLog(@"Tanishq_adding_videodataoutput");
        [self.captureSession addOutput:self.videoDataOutput];
    } else {
        return;
    }
    
    [[self.videoDataOutput connectionWithMediaType:AVMediaTypeVideo] setPreferredVideoStabilizationMode:AVCaptureVideoStabilizationModeAuto];
    [self.captureSession startRunning];
    
    NSError *lockError;
    [self.captureDevice lockForConfiguration:&lockError];
    if (lockError == nil) {
        self.captureDevice.autoFocusRangeRestriction = AVCaptureAutoFocusRangeRestrictionNear;
    }
    
    return;
}
```

And capturing delegate output using :

```
- (void)captureOutput:(AVCaptureOutput *)output didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
    NSLog(@"Tanishq_hello_captureOutput_called");
    return;
}
```
Editor is loading...
Leave a Comment