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

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

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


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

查看所有标签

猜你喜欢:

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

服务设计与创新实践

服务设计与创新实践

宝莱恩 (Andy Polaine)、乐维亚 (Lavrans Lovlie)、里森 (Ben Reason) / 王国胜、张盈盈、付美平、赵芳 / 清华大学出版社 / 2015-6-1 / CNY 69.00

产品经济的时代渐行渐远,在以服务为主导的新经济时代,在强调体验和价值的互联网时代,如何才能做到提前想用户之所想?如何比用户想得更周到?如何设计可用、好用和体贴的服务?这些都可以从本书中找到答案。本书撷取以保险业为代表的金融服务、医疗服务、租车及其他种种服务案例,从概念到实践,有理有据地阐述了如何对服务进行重新设计?如何将用户体验和价值提前与产品设计融合在一起? 《服务设计与创新实践》适合产品......一起来看看 《服务设计与创新实践》 这本书的介绍吧!

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

在线压缩/解压 JS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

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

HEX CMYK 互转工具