Avcapturevideopreviewlayer Add Overlays and Capture Photo in iOS

AVCaptureVideoPreviewLayer add overlays and capture photo in iOS

You can load your overlay into a CIImage, then use transformed(by matrix: CGAffineTransform) to move it to the face position, and finally use composited(over dest: CIImage) to blend it over the CIImage from the video buffer.
You probably have to put in some work to transfer between the different coordinate spaces.

There are also a lot of more complex compositing filters available. Check out the filters in the CICategoryCompositeOperation category.

How to customize the camera view and capture the part in the overlay?

Actually the answer from Nishant Bhindi needs some corrections. The code below will do the work:

func cropToBounds(image: UIImage) -> UIImage
{
let contextImage: UIImage = UIImage(cgImage: image.cgImage!)
let contextSize: CGSize = contextImage.size
let widthRatio = contextSize.height/UIScreen.main.bounds.size.height
let heightRatio = contextSize.width/UIScreen.main.bounds.size.width

let width = (self.imgOverlay?.frame.size.width)!*widthRatio
let height = (self.imgOverlay?.frame.size.height)!*heightRatio
let x = (contextSize.width/2) - width/2
let y = (contextSize.height/2) - height/2
let rect = CGRect(x: x, y: y, width: width, height: height)

let imageRef: CGImage = contextImage.cgImage!.cropping(to: rect)!
let image: UIImage = UIImage(cgImage: imageRef, scale: 0, orientation: image.imageOrientation)
return image
}

Still Image using AVCaptureDevice with overlay image

hi this will help you as it works for me

- (void)didTakePicture:(UIImage *)picture
{
UIImage *frm = self.overlayViewController.imgOverlay.image;
NSLog(@"width %f height %f",picture.size.width,picture.size.height);
UIGraphicsBeginImageContext(picture.size);
[picture drawAtPoint:CGPointMake(0,0)];
[frm drawInRect:CGRectMake(0, 0, picture.size.width, picture.size.height)];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSLog(@"view %@",viewImage.debugDescription);
[self.capturedImages addObject:viewImage];
}


Related Topics



Leave a reply



Submit