前言:發(fā)現(xiàn)網(wǎng)上
GPUImage2的資源比較少,我最近又在使用這個(gè)庫(kù),所以我就把我使用的情況總結(jié)下吧。需要的人自取哦!
首先感謝@Willie文章提供參考
安裝
1.pod安裝
傳送門
這個(gè)是英文版的。內(nèi)容如下
- 下載
GPUImage2.podspec文件下載地址(路徑與Podfle同層級(jí)) -
Podfile中添加pod 'GPUImage2', :podspec => './GPUImage2.podspec' pod installimport GPUImage2
2.直接導(dǎo)入庫(kù)
原庫(kù)下載地址
同@Willie文章導(dǎo)入 不同在于:不導(dǎo)入Source文件夾,其他一樣
如果需要更改庫(kù)的話,建議直接導(dǎo)入庫(kù)的方式使用
簡(jiǎn)單使用
1.創(chuàng)建濾鏡視頻
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()
// 這里是修改源碼,增加的一個(gè)方法(修改見下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<UInt8>) -> 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 {
// 獲取到后,做點(diǎn)什么?
}
}
}
3.修改RawDataOutput
一、添加屬性
public var dataAvailableCallbackSize:(([UInt8], _ frameBuffer: Framebuffer, _ cmtime: CMTime) -> ())?
二、在newFramebufferAvailable方法最后面調(diào)用
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)
三、會(huì)出現(xiàn)藍(lán)色陰影???
修改文件中 GL_RGBA為GL_BGRA
glReadPixels(0, 0, framebuffer.size.width, framebuffer.size.height, GLenum(GL_BGRA), GLenum(GL_UNSIGNED_BYTE), &data)
4.切換攝像頭,修改 Camera
增加方法,外部直接調(diào)用即可
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
}
如果這個(gè)文章幫到了你,一定給我
Star、點(diǎn)擊關(guān)注哦!