栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在iOS中的AVCaptureDevice的输出上设置灰度

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在iOS中的AVCaptureDevice的输出上设置灰度

CoreImage
提供了大量用于使用GPU调整图像的过滤器,并且可以有效地与来自摄像机源或视频文件的视频数据一起使用。

objc.io上有一篇文章介绍了如何执行此操作。例子在Objective-C中,但是解释应该足够清楚以至于可以遵循。

基本步骤是:

  1. 创建一个
    EAGLContext
    配置为使用OpenGLES2的。
  2. 使用创建一个
    GLKView
    以显示渲染的输出
    EAGLContext
    。
  3. 创建一个
    CIContext
    ,使用相同的
    EAGLContext
    。
  4. CIFilter
    使用
    CIColorMonochrome
    CoreImage过滤器创建一个。
  5. 使用创建
    AVCaptureSession
    一个
    AVCaptureVideoDataOutput
    。
  6. 在
    AVCaptureVideoDataOutputDelegate
    方法中,将转换
    CMSampleBuffer
    为
    CIImage
    。将应用于
    CIFilter
    图像。将过滤后的图像绘制到
    CIImageContext
    。

该流水线确保视频像素缓冲区保留在GPU(从摄像机到显示器)上,并避免将数据移至CPU,以保持实时性能。

要保存过滤后的视频,请实施

AVAssetWriter
,然后将样本缓冲区附加到
AVCaptureVideoDataOutputDelegate
进行过滤的位置。

这是Swift中的示例。

GitHub上的示例。

import UIKitimport GLKitimport AVFoundationprivate let rotationTransform = CGAffineTransformMakeRotation(CGFloat(-M_PI * 0.5))class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {    private var context: CIContext!    private var targetRect: CGRect!    private var session: AVCaptureSession!    private var filter: CIFilter!    @IBOutlet var glView: GLKView!    override func prefersStatusBarHidden() -> Bool {        return true    }    override func viewDidAppear(animated: Bool) {        super.viewDidAppear(animated)        let whiteColor = CIColor( red: 1.0, green: 1.0, blue: 1.0        )        filter = CIFilter( name: "CIColorMonochrome", withInputParameters: [     "inputColor" : whiteColor,     "inputIntensity" : 1.0 ]        )        // GL context        let glContext = EAGLContext( API: .OpenGLES2        )        glView.context = glContext        glView.enableSetNeedsDisplay = false        context = CIContext( EAGLContext: glContext, options: [     kCIContextOutputColorSpace: NSNull(),     kCIContextWorkingColorSpace: NSNull(), ]        )        let screenSize = UIScreen.mainScreen().bounds.size        let screenScale = UIScreen.mainScreen().scale        targetRect = CGRect( x: 0, y: 0, width: screenSize.width * screenScale, height: screenSize.height * screenScale        )        // Setup capture session.        let cameraDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)        let videoInput = try? AVCaptureDeviceInput( device: cameraDevice        )        let videoOutput = AVCaptureVideoDataOutput()        videoOutput.setSampleBufferDelegate(self, queue: dispatch_get_main_queue())        session = AVCaptureSession()        session.beginConfiguration()        session.addInput(videoInput)        session.addOutput(videoOutput)        session.commitConfiguration()        session.startRunning()    }    func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {        guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return        }        let originalImage = CIImage( CVPixelBuffer: pixelBuffer, options: [     kCIImageColorSpace: NSNull() ]        )        let rotatedImage = originalImage.imageByApplyingTransform(rotationTransform)        filter.setValue(rotatedImage, forKey: kCIInputImageKey)        guard let filteredImage = filter.outputImage else { return        }        context.drawImage(filteredImage, inRect: targetRect, fromRect: filteredImage.extent)        glView.display()    }    func captureOutput(captureOutput: AVCaptureOutput!, didDropSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {        let seconds = CMTimeGetSeconds(CMSampleBufferGetPresentationTimeStamp(sampleBuffer))        print("dropped sample buffer: (seconds)")    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/413220.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号