gpt4 book ai didi

ios - Swift iOS 实时对视频应用滤镜?

转载 作者:行者123 更新时间:2023-11-28 08:24:34 25 4
gpt4 key购买 nike

我正在尝试实时对视频应用滤镜,但是当我点击按钮来转换 CALayer 时,没有应用任何更改;这很好奇,因为过滤器已通过但在屏幕上不可见。这是我用来呈现 AVOutput 的 UIView 的子类...

//AVCamPreview Class
import Foundation
import UIKit
import AVFoundation


class AVCamPreviewView: UIView {


var session: AVCaptureSession? {
get {

return (self.layer as! AVCaptureVideoPreviewLayer).session
}
set (session) {

(self.layer as! AVCaptureVideoPreviewLayer).session = session
}
}

override class func layerClass() -> AnyClass {


return AVCaptureVideoPreviewLayer.self
}
}




//CameraViewController
//Properties

lazy var context: CIContext = {
let eaglContext = EAGLContext(API: EAGLRenderingAPI.OpenGLES2)
let options = [kCIContextWorkingColorSpace : NSNull()]
return CIContext(EAGLContext: eaglContext, options: options)
}()
lazy var filterNames: [String] = {
return ["#nofilter","CIPhotoEffectNoir","CIPhotoEffectProcess"]
}()

//Apply Filter func
@IBAction func applyFilter(_sender: UIButton) {
let filterName = filterNames[_sender.tag]
filter = CIFilter(name: filterName)
self.filterButtonsContainer.center.x = self.filterButtonsContainer.center.x - self.view.frame.width
}

// MARK: - AVCaptureVideoDataOutputSampleBufferDelegate
func captureOutput(captureOutput: AVCaptureOutput!,didOutputSampleBuffer sampleBuffer: CMSampleBuffer!,fromConnection connection: AVCaptureConnection!) {
autoreleasepool {
let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)!

let formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer)!
self.currentVideoDimensions = CMVideoFormatDescriptionGetDimensions(formatDescription)
self.currentSampleTime = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer)

var outputImage = CIImage(CVPixelBuffer: imageBuffer)

if self.filter != nil {

self.filter.setValue(outputImage, forKey: kCIInputImageKey)
outputImage = self.filter.outputImage!


}


if self.isWriting {
if self.assetWriterPixelBufferInput?.assetWriterInput.readyForMoreMediaData == true {
var newPixelBuffer: CVPixelBuffer? = nil

CVPixelBufferPoolCreatePixelBuffer(nil, self.assetWriterPixelBufferInput!.pixelBufferPool!, &newPixelBuffer)

self.context.render(outputImage, toCVPixelBuffer: newPixelBuffer!, bounds: outputImage.extent, colorSpace: nil)

let success = self.assetWriterPixelBufferInput?.appendPixelBuffer(newPixelBuffer!, withPresentationTime: self.currentSampleTime!)

if success == false {
print("Pixel Buffer failed")
}
}
}

let orientation = UIDevice.currentDevice().orientation
var t: CGAffineTransform!
if orientation == UIDeviceOrientation.Portrait {
t = CGAffineTransformMakeRotation(CGFloat(-M_PI / 2.0))
} else if orientation == UIDeviceOrientation.PortraitUpsideDown {
t = CGAffineTransformMakeRotation(CGFloat(M_PI / 2.0))
} else if (orientation == UIDeviceOrientation.LandscapeRight) {
t = CGAffineTransformMakeRotation(CGFloat(M_PI))
} else {
t = CGAffineTransformMakeRotation(0)
}
outputImage = outputImage.imageByApplyingTransform(t)

let cgImage = self.context.createCGImage(outputImage, fromRect: outputImage.extent)

self.ciImage = outputImage

dispatch_sync(dispatch_get_main_queue(), {


(self.previewView.layer as! AVCaptureVideoPreviewLayer).contents = cgImage


})
}
}

最佳答案

https://github.com/jabson/MagicVideo

在项目中,过滤后的 CIImage 正在使用 CIContext 在 OpenGLView 上绘制,并且工作正常。也有写入照片库的可能性

关于ios - Swift iOS 实时对视频应用滤镜?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40451784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com