Created
April 27, 2020 09:28
-
-
Save kishikawakatsumi/14b240f8d427d3dc0f7a6df15215e5a4 to your computer and use it in GitHub Desktop.
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
import Cocoa | |
import AVFoundation | |
class ViewController: NSViewController, AVCaptureVideoDataOutputSampleBufferDelegate { | |
private let session = AVCaptureSession() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.wantsLayer = true | |
AVCaptureDevice.DiscoverySession(deviceTypes: [.externalUnknown], mediaType: .video, position: .unspecified).devices.forEach { | |
do { | |
let input = try AVCaptureDeviceInput(device: $0) // 他にもプラグインがある場合は適当に調整する | |
if session.canAddInput(input) { | |
session.addInput(input) | |
} | |
} catch { | |
print(error) | |
} | |
} | |
let previewLayer = AVCaptureVideoPreviewLayer() | |
previewLayer.autoresizingMask = [.layerWidthSizable, .layerHeightSizable] | |
previewLayer.session = session | |
if let layer = view.layer { | |
previewLayer.frame = layer.bounds | |
layer.addSublayer(previewLayer) | |
} | |
session.startRunning() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment