AVCaptureVideoPreviewLayer在iOS中添加叠加并捕获照片 [英] AVCaptureVideoPreviewLayer add overlays and capture photo in iOS
问题描述
最初的想法是通过AVCaptureSession
启动摄像机流,在原始CMSampleBuffer
中找到人脸,然后在AVCaptureVideoPreviewLayer
上添加一些图像作为图层,然后进行截图.
The initial idea was to start a camera stream via AVCaptureSession
, find faces in that raw CMSampleBuffer
and then add some images as layers on AVCaptureVideoPreviewLayer
and then take a screenshot.
完成此操作后,后来发现UIGraphicsGetImageFromCurrentImageContext
无法与AVCaptureVideoPreviewLayer
配合使用,因此在此处截屏无法解决我的目的.
After completing that, found out later that the UIGraphicsGetImageFromCurrentImageContext
won't work with AVCaptureVideoPreviewLayer
, so taking screenshot would not solve my purpose here.
因此,我改用了Metal
和MTKView
来执行一些实时渲染,并且结合使用CoreImage滤镜和Metal可以获得很好的效果.我已经知道如何使用内置的CoreImage滤镜检测面部并更改面部的该部分,但是我找不到合适的方法将一张图像添加到另一张图像上.
So I used Metal
and MTKView
instead to perform some live rendering and the results are good with the combination of CoreImage Filters and Metal. I already know how to detect faces and alter that part of the face using inbuilt CoreImage filters but I can't find a suitable method to add an image on to another image.
如何相对于背景图像中的位置融合两个图像?我有与CIImage合作.
How can I blend two images with respect to positioning in the background image? I have CIImage to work with.
推荐答案
您可以将叠加层加载到CIImage
中,然后使用transformed(by matrix: CGAffineTransform)
将其移动到面部位置,最后使用composited(over dest: CIImage)
进行混合通过视频缓冲区中的CIImage
.
您可能需要进行一些工作才能在不同的坐标空间之间进行转换.
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.
还有许多更复杂的合成过滤器可用.在CICategoryCompositeOperation
类别中查看过滤器.
There are also a lot of more complex compositing filters available. Check out the filters in the CICategoryCompositeOperation
category.
这篇关于AVCaptureVideoPreviewLayer在iOS中添加叠加并捕获照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!