welcome to linkAR technical documentation

previous

Processing Single Frame

Add this code to process only frames after certain events (like button pressed).
First of all disable the Median Filter

- (void)viewDidLoad{
    ...
    [_cvView setEnableMedianFilter:NO];
    ...
}

Also, add the following function in myCameraCapture.h

// Add still image output. 
- (void)addStillImageOutput 
{
    [self setStillImageOutput:[[AVCaptureStillImageOutput alloc] init]];
    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey,nil];
    [[self stillImageOutput] setOutputSettings:outputSettings];
 
    AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection in [[self stillImageOutput] connections]) {
        for (AVCaptureInputPort *port in [connection inputPorts]) {
            if ([[port mediaType] isEqual:AVMediaTypeVideo] ) {
                videoConnection = connection;
                break;
            }
        }
        if (videoConnection) { 
            break; 
        }
    }
 
    [[self captureSession] addOutput:[self stillImageOutput]];
}


You can configure alternative outputsettings


To capture a frame, add the following function:

// Capture still image.
- (void)captureStillImage
{  
    AVCaptureConnection *videoConnection = nil;
	for (AVCaptureConnection *connection in [[self stillImageOutput] connections]) {
		for (AVCaptureInputPort *port in [connection inputPorts]) {
			if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
				videoConnection = connection;
				break;
			}
		}
		if (videoConnection) { 
            break; 
        }
	}
 
    [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection 
                 completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) { 
                     NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];    
                     UIImage *image = [[UIImage alloc] initWithData:imageData];
                     [self setStillImage:image];
                     // Launch processImageCamera function (function of ViewController)
                     [videocontroller performSelectorOnMainThread:@selector(processImageCamera) withObject:nil waitUntilDone:YES];
 
 
                 }];
}



Then, add the following function in ViewController.m:

+ (UIImage *)captureView: (UIView *)view inSize: (CGSize)size
{
    UIGraphicsBeginImageContext(size);
    CGSize viewSize = view.frame.size;
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextScaleCTM( context, size.width/viewSize.width, size.height/viewSize.height);
 
    [view.layer renderInContext: UIGraphicsGetCurrentContext()];
 
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return viewImage;
}



Finally, modify the processImageCamera function to process each frame as follows:

-(void)processImageCamera
{
        UIImageView* myv = [[UIImageView alloc] initWithImage:captureManager.stillImage];
        [myv setFrame:self.view.frame];
        [self.view addSubview:myv];
 
        [_cvView performSelectorInBackground:@selector(processUIImage:) withObject:[ViewController captureView:self.view inSize:self.view.frame.size]];
 
        [myv removeFromSuperview];
}



previous