关于GPUImage之美颜相机

iOS GPUImage 美颜相机


GPUImage是一个强大的图像处理库,其中美颜相机,可以让我们实现自定义美颜拍照功能,这里只说拍照功能的实现。

实现思路:

  • GPUImage封装了AVFoundation相机API,所以它拥有拍照和拍摄视频的功能,即GPUImageStillCamera
  • GPUImage提供了125种滤镜,其中有曝光、磨皮、美白、饱和度这些实用滤镜,那么我们把这些滤镜使用addTarget()函数加入到相机中,是否能够实现美颜相机呢?

首先创建相机,创建滤镜属性:

/// 相机
fileprivate var camera: GPUImageStillCamera!
    
fileprivate var isFirst: Bool = true
    
/// 美白-1 ~ 1
fileprivate var brughtnessFilter: GPUImageBrightnessFilter!
/// 磨皮
fileprivate var bilateralFilter: GPUImageBilateralFilter!
/// 曝光-10 ~ 10
fileprivate var exposureFilter: GPUImageExposureFilter!
/// 饱和
fileprivate var saturationFilter: GPUImageSaturationFilter!
/// 滤镜组
fileprivate var filterGroup: GPUImageFilterGroup!

设置相机,设置滤镜,开始扫描拍照:

@IBAction func startCapturing(_ sender: UIButton) {
        if isFirst {
            // 1. 创建GPUImage中的相机
            let camera = GPUImageStillCamera(sessionPreset: AVCaptureSessionPresetHigh, cameraPosition: .front)
            camera?.outputImageOrientation = .portrait
            self.camera = camera
            
            // 2. 添加滤镜
            filterGroup = GPUImageFilterGroup()
            
            let brughtnessFilter = GPUImageBrightnessFilter()
            brughtnessFilter.brightness = 0.2
            self.brughtnessFilter = brughtnessFilter
            
            let bilateralFilter = GPUImageBilateralFilter()
            bilateralFilter.distanceNormalizationFactor = 5
            self.bilateralFilter = bilateralFilter
            
            let exposureFilter = GPUImageExposureFilter()
            exposureFilter.exposure = 0
            self.exposureFilter = exposureFilter
            
            let saturationFilter = GPUImageSaturationFilter()
            saturationFilter.saturation = 1
            self.saturationFilter = saturationFilter
            
            // 设置滤镜关系
            brughtnessFilter.addTarget(bilateralFilter)
            bilateralFilter.addTarget(exposureFilter)
            exposureFilter.addTarget(saturationFilter)
            
            filterGroup.initialFilters = [brughtnessFilter]
            filterGroup.terminalFilter = saturationFilter
            
            camera?.addTarget(filterGroup)
            
            
            // 3. 创建GPUImageView
            let gpu_ImageView = GPUImageView(frame: view.bounds)
            view.insertSubview(gpu_ImageView, at: 0)
            filterGroup.addTarget(gpu_ImageView)
            
            isFirst = false
        }
        
        // 4. 开始采集
        camera?.startCapture()
}

GPUImageFilterGroup是一个关联滤镜的滤镜组,其父类为GPUImageOutput,在扫描预览时需要加入GPUImageView对象。
有了扫描获取图像,那么就需要拍摄功能:

@IBAction func takePhoto(_ sender: UIButton) {
        camera.capturePhotoAsImageProcessedUp(toFilter: filterGroup) { (image: UIImage?, error: Error?) in
            guard let image = image else {
                return
            }
            
            UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
        }
}

拍摄获取当前扫描画面,传入的是一个滤镜,但想要加入前面多个滤镜,那么就只能传入GPUImageFilterGroup,获取到图片,写入相册!
停止扫描:

@IBAction func stopCapturing(_ sender: UIButton) {
        camera.stopCapture()
}

切换摄像头:

@IBAction func rotateCamera(_ sender: UIButton) {
        camera.rotateCamera()
}

除了扫描之外,其他3个功能几乎用一行代码就可以搞定。如此,就可以实现了我们美颜相机功能,当然,上面代码中各滤镜的相关数值都是固定死的,要想加强美颜程度,可以额外添加控件来调节,从而改变美颜效果,如用UISlider

你可能感兴趣的:(关于GPUImage之美颜相机)