如何使用Swift scenekit在IOS上绘制摄像机视频作为背景? [英] How to draw camera video as background on IOS using Swift scenekit?

查看:638
本文介绍了如何使用Swift scenekit在IOS上绘制摄像机视频作为背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ios上的swift和scenekit开发增强现实应用程序。有没有办法将设备摄像头捕获的视频作为场景背景绘制?

I am trying to develop an augmented reality app using swift and scenekit on ios. Is there a way to draw the video captured by the device camera as a background of the scene?

推荐答案

这对我有用,

我使用 AVFoundation 来捕获设备相机的视频输入:

I used AVFoundation to capture the video input of the device camera:

   let captureSession = AVCaptureSession()
   let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill

    if let videoDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) {
        var err: NSError? = nil
        if let videoIn : AVCaptureDeviceInput = AVCaptureDeviceInput.deviceInputWithDevice(videoDevice, error: &err) as? AVCaptureDeviceInput {
            if(err == nil){
                if (captureSession.canAddInput(videoIn as AVCaptureInput)){
                    captureSession.addInput(videoIn as AVCaptureDeviceInput)
                }
                else {
                    println("Failed to add video input.")
                }
            }
            else {
                println("Failed to create video input.")
            }
        }
        else {
            println("Failed to create video capture device.")
        }
    }
    captureSession.startRunning()

此时,基于Apple的​​background SCNScene 的属性,我预计会添加 AVCaptureV的实例ideoPreviewLayer SCNScene background.contents ,其中包括:

At this point, based on Apple's documentation of the background property of SCNScene, I expected to add the instance of AVCaptureVideoPreviewLayer to an SCNScene's background.contents, with something like:

   previewLayer.frame = sceneView.bounds
   sceneView.scene.background.contents = previewLayer 

这会将场景的背景颜色从默认白色更改为黑色,但不提供视频输入。 这可能是一个iOS错误?。
所以计划B.相反,我添加了'AVCaptureVideoPreviewLayer'作为 UIView的子层的图层:

This changed the background color of my scene from default white to black, but offered no video input. This may be an iOS bug?. So Plan B. Instead, I added the 'AVCaptureVideoPreviewLayer' as a sublayer of a UIView's layer:

 previewLayer.frame = self.view.bounds
 self.view.layer.addSublayer(previewLayer)

然后我将 SCNView 设置为相同 UIView 的子视图,设置 SCNView 的背景颜色以清除:

I then set an SCNView as a subview of that same UIView, setting the SCNView's background color to clear:

    let sceneView = SCNView()
    sceneView.frame = self.view.bounds
    sceneView.backgroundColor = UIColor.clearColor()
    self.view.addSubview(sceneView)

设备相机的视频现在可以看作背景现场。

The device camera's video is now visible as the background of the scene.

我创建了一个小的演示

这篇关于如何使用Swift scenekit在IOS上绘制摄像机视频作为背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆