Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. import UIKit
  2. import AVFoundation
  3. import Accelerate
  4.  
  5. var customPreviewLayer: AVCaptureVideoPreviewLayer?
  6.  
  7. class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {
  8.  
  9. var captureSession: AVCaptureSession?
  10. var dataOutput: AVCaptureVideoDataOutput?
  11. //var customPreviewLayer: AVCaptureVideoPreviewLayer?
  12.  
  13. @IBOutlet weak var camView: UIView!
  14.  
  15. override func viewWillAppear(animated: Bool) {
  16. super.viewDidAppear(animated)
  17. //setupCameraSession()
  18. }
  19.  
  20. override func viewDidLoad() {
  21. super.viewDidLoad()
  22. // Do any additional setup after loading the view, typically from a nib.
  23. //captureSession?.startRunning()
  24. setupCameraSession()
  25. self.captureSession?.startRunning()
  26. }
  27.  
  28. override func didReceiveMemoryWarning() {
  29. super.didReceiveMemoryWarning()
  30. // Dispose of any resources that can be recreated.
  31. }
  32.  
  33. func setupCameraSession() {
  34. // Session
  35. self.captureSession = AVCaptureSession()
  36. self.captureSession!.sessionPreset = AVCaptureSessionPreset1920x1080
  37. // Capture device
  38. let inputDevice: AVCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
  39. var deviceInput = AVCaptureDeviceInput()
  40. // Device input
  41. //var deviceInput: AVCaptureDeviceInput? = AVCaptureDeviceInput.deviceInputWithDevice(inputDevice, error: error)
  42. do {
  43. deviceInput = try AVCaptureDeviceInput(device: inputDevice)
  44.  
  45. } catch let error as NSError {
  46. // Handle errors
  47. print(error)
  48. }
  49. if self.captureSession!.canAddInput(deviceInput) {
  50. self.captureSession!.addInput(deviceInput)
  51. }
  52. // Preview
  53. customPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
  54. customPreviewLayer!.frame = camView.bounds
  55. customPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspect
  56. customPreviewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.Portrait
  57. self.camView.layer.addSublayer(customPreviewLayer!)
  58. print("Cam layer added")
  59.  
  60. self.dataOutput = AVCaptureVideoDataOutput()
  61. self.dataOutput!.videoSettings = [
  62. String(kCVPixelBufferPixelFormatTypeKey) : Int(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)
  63. ]
  64.  
  65. self.dataOutput!.alwaysDiscardsLateVideoFrames = true
  66. if self.captureSession!.canAddOutput(dataOutput) {
  67. self.captureSession!.addOutput(dataOutput)
  68. }
  69. self.captureSession!.commitConfiguration()
  70. let queue: dispatch_queue_t = dispatch_queue_create("VideoQueue", DISPATCH_QUEUE_SERIAL)
  71. //let delegate = VideoDelegate()
  72. self.dataOutput!.setSampleBufferDelegate(self, queue: queue)
  73. }
  74.  
  75.  
  76.  
  77.  
  78. func captureOutput(captureOutput: AVCaptureOutput, didOutputSampleBuffer sampleBuffer: CMSampleBufferRef, fromConnection connection: AVCaptureConnection) {
  79. print("buffered")
  80. let imageBuffer: CVImageBufferRef = CMSampleBufferGetImageBuffer(sampleBuffer)!
  81. CVPixelBufferLockBaseAddress(imageBuffer, 0)
  82. let width: size_t = CVPixelBufferGetWidthOfPlane(imageBuffer, 0)
  83. let height: size_t = CVPixelBufferGetHeightOfPlane(imageBuffer, 0)
  84. let bytesPerRow: size_t = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer, 0)
  85. let lumaBuffer: UnsafeMutablePointer = CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0)
  86. let grayColorSpace: CGColorSpaceRef = CGColorSpaceCreateDeviceGray()!
  87. let context: CGContextRef = CGBitmapContextCreate(lumaBuffer, width, height, 8, bytesPerRow, grayColorSpace, CGImageAlphaInfo.PremultipliedLast.rawValue)!//problematic
  88.  
  89. let dstImageFilter: CGImageRef = CGBitmapContextCreateImage(context)!
  90. dispatch_sync(dispatch_get_main_queue(), {() -> Void in
  91. customPreviewLayer!.contents = dstImageFilter as AnyObject
  92. })
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement