前言:发现网上
GPUImage2
的资源比较少,我最近又在使用这个库,所以我就把我使用的情况总结下吧。需要的人自取哦!
首先感谢@Willie文章提供参考
安装
1.pod
安装
传送门
这个是英文版的。内容如下
- 下载
GPUImage2.podspec
文件下载地址(路径与Podfle同层级) -
Podfile
中添加pod 'GPUImage2', :podspec => './GPUImage2.podspec'
pod install
import GPUImage2
2.直接导入库
原库下载地址
同@Willie文章导入 不同在于:不导入Source文件夹,其他一样
如果需要更改库的话,建议直接导入库的方式使用
简单使用
1.创建滤镜视频
do {
camera = try Camera(sessionPreset: .vga640x480,
cameraDevice: nil,
location: .frontFacing,
captureAsYUV: true)
} catch {
print(error)
return
}
//磨皮 - 美白
let basicOperation = BilateralBlur()
basicOperation.distanceNormalizationFactor = 7
let brightnessAdjustment = BrightnessAdjustment()
brightnessAdjustment.brightness = 0.11
//显示
renderView = RenderView(frame: view.bounds)
view.addSubview(renderView)
camera --> brightnessAdjustment --> basicOperation --> renderView
basicOperation --> self.videoOutput
2.获取滤镜后的视频流
// 全局变量
videoOutput = RawDataOutput()
// 这里是修改源码,增加的一个方法(修改见下3)
videoOutput.dataAvailableCallbackSize = { [weak self] (videoData, framebuffer, cmtime) in
guard let `self` = self else { return }
let numberOfBytesPerRow = framebuffer.size.width
let getData = Data(bytes: videoData)
getData.withUnsafeBytes { (u8Ptr: UnsafePointer) -> Void in
let rawPtr = UnsafeMutableRawPointer(mutating: u8Ptr)
var pixelBuffer : CVPixelBuffer?;
_ = CVPixelBufferCreateWithBytes(kCFAllocatorDefault,
Int(framebuffer.size.width),
Int(framebuffer.size.height),
kCVPixelFormatType_32BGRA,
rawPtr,
Int(numberOfBytesPerRow*4), nil, nil, nil,
&pixelBuffer);
if pixelBuffer != nil {
// 获取到后,做点什么?
}
}
}
3.修改RawDataOutput
一、添加属性
public var dataAvailableCallbackSize:(([UInt8], _ frameBuffer: Framebuffer, _ cmtime: CMTime) -> ())?
二、在newFramebufferAvailable
方法最后面调用
var time: CMTime = CMTime(seconds: 0, preferredTimescale: 1000)
if let timestamp = framebuffer.timingStyle.timestamp {
time = CMTime(value: timestamp.value, timescale: timestamp.timescale)
}
dataAvailableCallbackSize?(data, framebuffer, time)
三、会出现蓝色阴影???
修改文件中 GL_RGBA
为GL_BGRA
glReadPixels(0, 0, framebuffer.size.width, framebuffer.size.height, GLenum(GL_BGRA), GLenum(GL_UNSIGNED_BYTE), &data)
4.切换摄像头,修改 Camera
增加方法,外部直接调用即可
public func switchCamera() {
self.location = self.location == .backFacing ? .frontFacing : .backFacing;
}
修改原属性 location
public var location:PhysicalCameraLocation {
didSet {
// TODO: Swap the camera locations, framebuffers as needed
guard let newVideoInput = try? AVCaptureDeviceInput(device: location.device()!) else { return }
self.captureSession.beginConfiguration()
self.captureSession.removeInput(self.videoInput)
self.captureSession.addInput(newVideoInput)
self.captureSession.commitConfiguration()
self.videoInput = newVideoInput
}
}
5.视屏镜像
var captureConnection: AVCaptureConnection!
for connection in videoOutput.connections {
for port in connection.inputPorts {
if (port as AnyObject).mediaType == AVMediaType.video {
captureConnection = connection
captureConnection.isVideoMirrored = location == .frontFacing
}
}
}
if captureConnection.isVideoOrientationSupported {
captureConnection.videoOrientation = .portraitUpsideDown
}
如果这个文章帮到了你,一定给我
Star
、点击关注
哦!