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

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

内容简介: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


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

查看所有标签

猜你喜欢:

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

数据结构与算法分析

数据结构与算法分析

Frank.M.Carrano / 金名 / 清华大学出版社 / 2007-11 / 98.00元

“数据结构”是计算机专业的基础与核心课程之一,Java是现今一种热门的语言。本书在编写过程中特别考虑到了面向对象程序设计(OOP)的思想与Java语言的特性。它不是从基于另一种程序设计语言的数据结构教材简单地“改编”而来的,因此在数据结构的实现上更加“地道”地运用了Java语言,并且自始至终强调以面向对象的方式来思考、分析和解决问题。 本书是为数据结构入门课程(通常课号是CS-2)而编写的教......一起来看看 《数据结构与算法分析》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具