如何使用 swift xcode 访问 Mac 默认相机

How to access Mac default camera using swift xcode

今天我第一次为 Mac 编码。我想要做的是访问默认相机并显示预览。第二步,如果我需要,我会记录或拍一张快照。对于第一步,我编写了以下代码

import Cocoa
import AVFoundation
class ViewController: NSViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        var session:AVCaptureSession = AVCaptureSession()
        session.sessionPreset = AVCaptureSessionPresetLow
        var device:AVCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
        //Preview
        var previewLayer:AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer(session: session)
        var myView:NSView = self.view
        previewLayer.frame = myView.bounds
        previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
        self.view.layer?.addSublayer(previewLayer)
        session.startRunning()
    }
    override var representedObject: AnyObject? {
        didSet {
        // Update the view, if already loaded.
        }
    }
}

我没有看到此代码正在打开我的笔记本电脑默认摄像头或在视图上显示任何内容。我在这里做错了什么?我可以寻找任何方向或任何示例,即使它在 Obj-C 中真的很有帮助。 TIA.

在你的代码中

self.view.layer?.addSublayer(previewLayer)

不会被执行,因为 self.view.layernil 所以不会被执行。

唉,这似乎不是唯一的问题,因为即使在添加图层时相机也不会开始工作。您可能需要深入研究:

https://developer.apple.com/library/mac/samplecode/AVRecorder/Introduction/Intro.html

一定要检查你的视图在 IB 中是否有核心动画层:

如果有人仍然希望在 Mac OS X 中使用 swift 3

启动网络摄像头

这是代码

@IBOutlet var previewCam: PreviewCam!
override func viewDidLoad()
{
    super.viewDidLoad()
    view.wantsLayer = true
    previewCam.wantsLayer = true
    let session:AVCaptureSession = AVCaptureSession()
    session.sessionPreset = AVCaptureSessionPresetLow
    let device:AVCaptureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
    print("device found = ",device)

    let device_input : AVCaptureDeviceInput = try! AVCaptureDeviceInput(device: device)


    let previewLayer:AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer(session: session)

    previewLayer.frame = previewCam.bounds
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
    self.previewCam.layer?.addSublayer(previewLayer)
    if session.canAddInput(device_input)
    {
        session.addInput(device_input)
    }
    session.startRunning()
}

对于那些询问预览摄像头的人

#import "PreviewCam.h"

@implementation PreviewCam

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];

    // Drawing code here.
    CGContextRef context = [[NSGraphicsContext currentContext]graphicsPort];
    CGContextSetRGBFillColor(context, 0, 0, 0, 0.75);
    CGContextFillRect(context, NSRectToCGRect(dirtyRect));
    self.layer.borderColor = [[NSColor whiteColor]CGColor];
    self.layer.borderWidth = 2.0f;
}

swift5 中的工作代码

import Cocoa
import AVFoundation

class SomwViewController: NSViewController {
    @IBOutlet private var cameraView: NSView?
    let session: AVCaptureSession = AVCaptureSession()

    override func viewDidLoad() {
        super.viewDidLoad()
        cameraView?.wantsLayer = true
        cameraView?.layer?.backgroundColor = NSColor.black.cgColor
        session.sessionPreset = AVCaptureSession.Preset.low
        let input: AVCaptureInput = try! AVCaptureDeviceInput(device: AVCaptureDevice.default(for: .video)!)
        session.addInput(input)
        let previewLayer: AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer(session: session)
        previewLayer.frame = cameraView!.bounds
        previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill

        cameraView?.layer?.addSublayer(previewLayer)
    }

    override func viewDidAppear() {
        session.startRunning()
    }

    override func viewDidDisappear() {
        session.stopRunning()
    }
}

你绝对不应该使用强制解包,这只是一个例子。