[英]Capture image with GPUImage filter on Swift
I'm using GPUImage Framework for Image filters. 我正在使用GPUImage Framework for Image过滤器。
I have applied the ToonFilter() on camera for cartoon filter effect. 我在相机上应用了ToonFilter()来实现卡通滤镜效果。
My issue is while capture image on camera with ToonFilter effect with GPUImage. 我的问题是使用GPUImage在ToonFilter效果的相机上捕获图像。
Below is the code to show the ToonFilter with GPUImage camera & to capture a new image with the same effect. 下面是使用GPUImage相机显示ToonFilter并捕获具有相同效果的新图像的代码。
Please guide me how I may capture an image with the same filter? 请指导我如何使用相同的过滤器捕获图像?
Note : I am successfully able to open camera with ToonFilter effect - the issue is only with capture a new image with a filter effect. 注意:我成功地能够使用ToonFilter效果打开相机 - 问题只是捕获具有滤镜效果的新图像。
Code : 代码:
import UIKit
import GPUImage
import AVFoundation
class SelfieFilterVC: UIViewController, UISplitViewControllerDelegate {
@IBOutlet var filterSlider: UISlider?
@IBOutlet var filterView: RenderView?
let videoCamera:Camera?
var blendImage:PictureInput?
override func viewDidLoad() {
super.viewDidLoad()
self.filterOperation = FilterOperation(
filter:{ToonFilter()},
listName:"Toon",
titleName:"Toon",
sliderConfiguration:.disabled,
sliderUpdateCallback: nil,
filterOperationType:.singleInput
)
self.configureView()
}
required init(coder aDecoder: NSCoder)
{
do {
videoCamera = try Camera(sessionPreset:AVCaptureSessionPreset640x480, location:.backFacing)
videoCamera!.runBenchmark = true
} catch {
videoCamera = nil
print("Couldn't initialize camera with error: \(error)")
}
super.init(coder: aDecoder)!
}
var filterOperation: FilterOperationInterface?
@IBAction func btnCapture(_ sender: Any) {
videoCamera?.startCapture()
let pictureOutput = PictureOutput()
pictureOutput.encodedImageFormat = .jpeg
pictureOutput.encodedImageAvailableCallback = {imageData in
if imageData != nil {
let captureDetailVC = self.storyboard?.instantiateViewController(withIdentifier: "CaptureDetailVC") as! CaptureDetailVC
captureDetailVC.aCaptureSelectedData = imageData
self.show(captureDetailVC, sender: true)
}
}
}
@IBAction func btnBackAction(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
func configureView() {
guard let videoCamera = videoCamera else {
let errorAlertController = UIAlertController(title: NSLocalizedString("Error", comment: "Error"), message: "Couldn't initialize camera", preferredStyle: .alert)
errorAlertController.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "OK"), style: .default, handler: nil))
self.present(errorAlertController, animated: true, completion: nil)
return
}
if let currentFilterConfiguration = self.filterOperation {
self.title = currentFilterConfiguration.titleName
// Configure the filter chain, ending with the view
if let view = self.filterView {
switch currentFilterConfiguration.filterOperationType {
case .singleInput:
videoCamera.addTarget(currentFilterConfiguration.filter)
currentFilterConfiguration.filter.addTarget(view)
case .blend:
videoCamera.addTarget(currentFilterConfiguration.filter)
self.blendImage = PictureInput(imageName:blendImageName)
self.blendImage?.addTarget(currentFilterConfiguration.filter)
self.blendImage?.processImage()
currentFilterConfiguration.filter.addTarget(view)
case let .custom(filterSetupFunction:setupFunction):
currentFilterConfiguration.configureCustomFilter(setupFunction(videoCamera, currentFilterConfiguration.filter, view))
}
videoCamera.startCapture()
}
// Hide or display the slider, based on whether the filter needs it
if let slider = self.filterSlider {
switch currentFilterConfiguration.sliderConfiguration {
case .disabled:
slider.isHidden = true
// case let .Enabled(minimumValue, initialValue, maximumValue, filterSliderCallback):
case let .enabled(minimumValue, maximumValue, initialValue):
slider.minimumValue = minimumValue
slider.maximumValue = maximumValue
slider.value = initialValue
slider.isHidden = false
self.updateSliderValue()
}
}
}
}
@IBAction func updateSliderValue() {
if let currentFilterConfiguration = self.filterOperation {
switch (currentFilterConfiguration.sliderConfiguration) {
case .enabled(_, _, _): currentFilterConfiguration.updateBasedOnSliderValue(Float(self.filterSlider!.value))
case .disabled: break
}
}
}
override func viewWillDisappear(_ animated: Bool) {
if let videoCamera = videoCamera {
videoCamera.stopCapture()
videoCamera.removeAllTargets()
blendImage?.removeAllTargets()
}
super.viewWillDisappear(animated)
}
}
The way you capture an image with GPUImage is with the method imageFromCurrentFramebuffer
that you can user con your filter. 使用GPUImage捕获图像的方法是使用
imageFromCurrentFramebuffer
方法,您可以使用它来过滤您的过滤器。
It returns a UIImage
which you can later convert to whatever image format you need and save it. 它返回一个
UIImage
,您可以稍后将其转换为您需要的任何图像格式并保存。
let image:UIImage = filter.imageFromCurrentFramebuffer()
if let data = UIImageJPEGRepresentation(image, 0.8) {
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let filename = documentsDirectory.appendingPathComponent("image.jpg")
try? data.write(to: filename)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.