从 iOS 相机捕获图片 [英] Capture picture from iOS camera

查看:15
本文介绍了从 iOS 相机捕获图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过相机捕捉图像.但它给出了以下错误:'NSInvalidArgumentException',原因:'无法直接实例化 AVCaptureDevice.'

I need to capture an image through the camera. But it gives the following error: 'NSInvalidArgumentException', reason: 'Cannot instantiate a AVCaptureDevice directly.'

按照代码:

    var session: AVCaptureSession!
    var device: AVCaptureDevice!
    var captureVideoPreviewLayer: AVCaptureVideoPreviewLayer!
    var image: UIImage!
    var input: AVCaptureDeviceInput!
    var stillImageOutput: AVCaptureStillImageOutput!
    var viewControllerScheduling: RentalSchedulingViewController!

    override func viewDidLoad() {
        super.viewDidLoad()

        session = AVCaptureSession()
        session.sessionPreset = AVCaptureSessionPresetPhoto;
        captureVideoPreviewLayer = AVCaptureVideoPreviewLayer(session: session)
        captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
        captureVideoPreviewLayer.frame = self.view.frame
        self.view.layer.addSublayer(captureVideoPreviewLayer)
        var error = NSErrorPointer()
        captureVideoPreviewLayer.session.beginConfiguration()
        input = AVCaptureDeviceInput(device: self.getBackCamera(), error: error)
        session.addInput(input)
        captureVideoPreviewLayer.session.commitConfiguration()
        stillImageOutput = AVCaptureStillImageOutput()
        var outputSettings = NSDictionary(objectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey)
        stillImageOutput.outputSettings = outputSettings
        session.addOutput(stillImageOutput)
        session.startRunning()
    }

取回相机:

    func getBackCamera() -> AVCaptureDevice {
        var devic = AVCaptureDevice()
        let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo) as NSArray
        for dev in devices {
            devic = dev as AVCaptureDevice
            if devic.position == AVCaptureDevicePosition.Back {
                return devic
            }
        }
        return devic
    }

捕获图像的事件:

   @IBAction func capture(sender: AnyObject) {
        var videoConnection = AVCaptureConnection()

        for con in stillImageOutput.connections {
            let connection = con as AVCaptureConnection

            for p in connection.inputPorts {
                let port = p as AVCaptureInputPort

                if port.mediaType == AVMediaTypeVideo {
                    videoConnection = connection
                    break;
                }
            }
        }

        stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) {
            (imageSampleBuffer, error) in
            if imageSampleBuffer != nil {
                let imageData = AVCaptureStillImageOutput .jpegStillImageNSDataRepresentation(imageSampleBuffer)
                self.processImage(UIImage(data: imageData)!)
            }
        }
    }

处理图像:

    func processImage(image: UIImage) {
        UIGraphicsBeginImageContext(CGSizeMake(320, 426));
        image.drawInRect(CGRectMake(0, 0, 320, 426))
        let smallImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext();

        let cropRect = CGRectMake(0, 55, 320, 320)
        let imageRef = CGImageCreateWithImageInRect(smallImage.CGImage, cropRect)
        self.image = UIImage(CGImage: imageRef)
    }

错误发生在这里:

input = AVCaptureDeviceInput(device: self.getBackCamera(), error: error)

推荐答案

我一直在寻找这个问题.看来您实际上无法实例化设备 AVCaptureDevice 对象.必须通过引用传递.这就是我克服挑战的方式.我没有实例化类,我只是将它作为对 AVCaptureDeviceInput 对象的引用传入.

I've been looking up this issue. It seems that you can't actually instantiate the device AVCaptureDevice object. have to pass it by reference. This is how I overcame the challenge. I didn't instantiate the class I simply passed it in as a reference to the AVCaptureDeviceInput Object.

希望这有帮助!

    var captureDevice : AVCaptureDevice?

    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
            }
        }
    }

}

这篇关于从 iOS 相机捕获图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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