Created
September 5, 2017 03:05
-
-
Save kasimok/de8c70b4b43d9049477bfa123d62be15 to your computer and use it in GitHub Desktop.
Blur effect like iOS's facetime.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIViewController { | |
var previewView : UIView!; | |
var blurView: UIVisualEffectView!; | |
//Camera Capture requiered properties | |
var videoDataOutput: AVCaptureVideoDataOutput! | |
var videoDataOutputQueue: DispatchQueue! | |
var previewLayer:AVCaptureVideoPreviewLayer! | |
var captureDevice : AVCaptureDevice! | |
let session = AVCaptureSession() | |
var currentFrame: CIImage! | |
var done = false | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
previewView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)) | |
previewView.contentMode = UIViewContentMode.scaleAspectFill | |
view.addSubview(previewView) | |
//Add a box view | |
let commonBlurEffect = UIBlurEffect(style: .dark) | |
blurView = UIVisualEffectView(effect: commonBlurEffect) | |
blurView.frame = previewView.frame | |
view.addSubview(blurView) | |
self.setupAVCapture() | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
override func viewWillAppear(_ animated: Bool) { | |
if !done { | |
session.startRunning() | |
} | |
} | |
override var shouldAutorotate: Bool { | |
if (UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft || | |
UIDevice.current.orientation == UIDeviceOrientation.landscapeRight || | |
UIDevice.current.orientation == UIDeviceOrientation.unknown) { | |
return false | |
} | |
else { | |
return true | |
} | |
} | |
} | |
// AVCaptureVideoDataOutputSampleBufferDelegate protocol and related methods | |
extension ViewController: AVCaptureVideoDataOutputSampleBufferDelegate{ | |
func setupAVCapture(){ | |
session.sessionPreset = .high | |
guard let device = AVCaptureDevice | |
.default(for: .video) else{ | |
return | |
} | |
captureDevice = device | |
beginSession() | |
done = true | |
} | |
func beginSession(){ | |
var err : NSError? = nil | |
var deviceInput:AVCaptureDeviceInput? | |
do { | |
deviceInput = try AVCaptureDeviceInput(device: captureDevice) | |
} catch let error as NSError { | |
err = error | |
deviceInput = nil | |
} | |
if err != nil { | |
print("error: \(String(describing: err?.localizedDescription))") | |
} | |
if self.session.canAddInput(deviceInput!){ | |
self.session.addInput(deviceInput!) | |
} | |
videoDataOutput = AVCaptureVideoDataOutput() | |
videoDataOutput.alwaysDiscardsLateVideoFrames=true | |
videoDataOutputQueue = DispatchQueue(label: "VideoDataOutputQueue") | |
videoDataOutput.setSampleBufferDelegate(self, queue:self.videoDataOutputQueue) | |
if session.canAddOutput(self.videoDataOutput){ | |
session.addOutput(self.videoDataOutput) | |
} | |
videoDataOutput.connection(with: AVMediaType.video)?.isEnabled = true | |
self.previewLayer = AVCaptureVideoPreviewLayer(session: self.session) | |
self.previewLayer.videoGravity = AVLayerVideoGravity.resizeAspect | |
let rootLayer: CALayer = self.previewView.layer | |
rootLayer.masksToBounds = true | |
self.previewLayer.frame = rootLayer.bounds | |
rootLayer.addSublayer(self.previewLayer) | |
session.startRunning() | |
} | |
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) { | |
currentFrame = self.convertImageFromCMSampleBufferRef(sampleBuffer) | |
} | |
// clean up AVCapture | |
func stopCamera(){ | |
session.stopRunning() | |
done = false | |
} | |
func convertImageFromCMSampleBufferRef(_ sampleBuffer:CMSampleBuffer) -> CIImage{ | |
let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)! | |
let ciImage:CIImage = CIImage(cvImageBuffer: pixelBuffer) | |
return ciImage | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
preview