ios – 从Swift中的AVCaptureSession获取输出以发送到服务器

栏目: Swift · 发布时间: 7年前

内容简介:Apple在ObjectiveC中有一个示例项目AVCam,可以处理这些事情.我个人使用这个

我设法编写一些打开相机并预览视频的代码.我现在想从输出中捕获帧,发送到理想编码为H.264的服务器

这就是我所得到的:

import UIKit
import AVFoundation

class ViewController: UIViewController {

    let captureSession = AVCaptureSession()
    var previewLayer : AVCaptureVideoPreviewLayer?

    // If we find a device we'll store it here for later use
    var captureDevice : AVCaptureDevice?

    override func viewDidLoad() {
        super.viewDidLoad()            
        // Do any additional setup after loading the view, typically from a nib.

        captureSession.sessionPreset = AVCaptureSessionPresetHigh

        let devices = AVCaptureDevice.devices()

        // Loop through all the capture devices on this phone
        for device in devices {
            // Make sure this particular device supports video
            if (device.hasMediaType(AVMediaTypeVideo)) {
                // Finally check the position and confirm we've got the back camera
                if(device.position == AVCaptureDevicePosition.Back) {
                    captureDevice = device as? AVCaptureDevice
                    if captureDevice != nil {
                        println("Capture device found")
                        beginSession()
                    }
                }
            }
        }

    }

    func beginSession() {

        var err : NSError? = nil
        captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &err))

        if err != nil {
            println("error: \(err?.localizedDescription)")
        }

        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        self.view.layer.addSublayer(previewLayer)
        previewLayer?.frame = self.view.layer.frame

        captureSession.startRunning()

    }

}

这可以成功打开相机,我可以预览镜头.

我发现这个Objective C代码看起来像得到输出,但是我不知道如何将它转换成swift.它使用AVCaptureVideoDataOutput,AVAssetWriter,AVAssetWriterInput和AVAssetWriterInputPixelBufferAdaptor将帧写入H.264编码的电影文件.

Can use AVCaptureVideoDataOutput and AVCaptureMovieFileOutput at the same time?

有人可以帮助您转换它,还是给我指点如何将框架从我当前的代码中取出?

Apple在ObjectiveC中有一个示例项目AVCam,可以处理这些事情.

Here’s 关于在Swift中使用AVCamera的另一个问题.

我个人使用这个 https://github.com/alex-chan/AVCamSwift ,没关系.我只需要将其转换为Xcode中的最新Swift语法,并且工作正常.

另一个建议是使用您发现的ObjectiveC代码并将其导入到Swift代码中,通过桥接头.

代码日志版权声明:

翻译自:http://stackoverflow.com/questions/26197992/get-output-from-avcapturesession-in-swift-to-send-to-server


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

算法设计与分析基础

算法设计与分析基础

Anany levitin / 潘彦 / 清华大学出版社 / 2007-1-1 / 49.00元

作者基于丰富的教学经验,开发了一套对算法进行分类的新方法。这套方法站在通用问题求解策略的高度,能对现有的大多数算法都能进行准确分类,从而使本书的读者能够沿着一条清晰的、一致的、连贯的思路来探索算法设计与分析这一迷人领域。本书作为第2版,相对第1版增加了新的习题,还增加了“迭代改进”一章,使得原来的分类方法更加完善。 本书十分适合作为算法设计和分析的基础教材,也适合任何有兴趣探究算法奥秘的读者......一起来看看 《算法设计与分析基础》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具