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


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

查看所有标签

猜你喜欢:

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

Operating Systems

Operating Systems

Remzi Arpaci-Dusseau、Andrea Arpaci-Dusseau / Arpaci-Dusseau Books / 2012-8-19 / USD 21.00

A book about modern operating systems. Topics are broken down into three major conceptual pieces: Virtualization, Concurrency, and Persistence. Includes all major components of modern systems includin......一起来看看 《Operating Systems》 这本书的介绍吧!

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

在线压缩/解压 JS 代码

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

Base64 编码/解码

SHA 加密
SHA 加密

SHA 加密工具