前言:發(fā)現(xiàn)網(wǎng)上
GPUImage2
的資源比較少,我最近又在使用這個庫,所以我就把我使用的情況總結(jié)下吧。需要的人自取哦!
首先感謝@Willie文章提供參考
安裝
1.pod
安裝
傳送門
這個是英文版的村生。內(nèi)容如下
- 下載
GPUImage2.podspec
文件下載地址(路徑與Podfle同層級) -
Podfile
中添加pod 'GPUImage2', :podspec => './GPUImage2.podspec'
pod install
import GPUImage2
2.直接導入庫
原庫下載地址
同@Willie文章導入 不同在于:不導入Source文件夾,其他一樣
如果需要更改庫的話饼丘,建議直接導入庫的方式使用
簡單使用
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()
// 這里是修改源碼趁桃,增加的一個方法(修改見下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 {
// 獲取到后,做點什么肄鸽?
}
}
}
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)
三典徘、會出現(xià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
}
如果這個文章幫到了你,一定給我
Star
裆甩、點擊關注
哦冗锁!