iOS 相机视频实时预览与拍摄的照片有偏移

标签 ios swift camera uikit avfoundation

我正在使用相机。

相机向用户呈现实时画面,当他们点击时,会创建图像并将其传递给用户。

问题是图像被设计为位于最顶部的位置,高于实时预览显示的位置。

您知道如何调整相机的框架,使实时视频源的顶部与他们要拍摄的照片的顶部相匹配吗?

我认为这可以做到这一点,但事实并非如此。这是我当前的相机框架代码:

 //Add the device to the session, get the video feed it produces and add it to the video feed layer
    func initSessionFeed()
    {
_session = AVCaptureSession()
        _session.sessionPreset = AVCaptureSessionPresetPhoto
        updateVideoFeed()

        _videoPreviewLayer = AVCaptureVideoPreviewLayer(session: _session)
        _videoPreviewLayer.frame = CGRectMake(0,0, self.frame.width, self.frame.width) //the live footage IN the video feed view
        _videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
        self.layer.addSublayer(_videoPreviewLayer)//add the footage from the device to the video feed layer
    }

    func initOutputCapture()
    {
        //set up output settings
        _stillImageOutput = AVCaptureStillImageOutput()
        var outputSettings:Dictionary = [AVVideoCodecJPEG:AVVideoCodecKey]
        _stillImageOutput.outputSettings = outputSettings
        _session.addOutput(_stillImageOutput)
        _session.startRunning()
    }

    func configureDevice()
    {
        if _currentDevice != nil
        {
            _currentDevice.lockForConfiguration(nil)
            _currentDevice.focusMode = .Locked
            _currentDevice.unlockForConfiguration()
        }
    }

    func captureImage(callback:(iImage)->Void)
    {
        if(_captureInProcess == true)
        {
            return
        }
        _captureInProcess = true

        var videoConnection:AVCaptureConnection!
        for connection in _stillImageOutput.connections
        {
            for port in (connection as AVCaptureConnection).inputPorts
            {
                if (port as AVCaptureInputPort).mediaType == AVMediaTypeVideo
                {
                    videoConnection = connection as AVCaptureConnection
                    break;
                }

                if videoConnection != nil
                {
                    break;
                }
            }
        }

        if videoConnection  != nil
        {
            _stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection)
            {
                (imageSampleBuffer : CMSampleBuffer!, _) in
                let imageDataJpeg = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageSampleBuffer)
                var pickedImage = UIImage(data: imageDataJpeg, scale: 1)
                UIGraphicsBeginImageContextWithOptions(pickedImage.size, false, pickedImage.scale)
                pickedImage.drawInRect(CGRectMake(0, 0, pickedImage.size.width, pickedImage.size.height))
                pickedImage = UIGraphicsGetImageFromCurrentImageContext() //this returns a normalized image
                if(self._currentDevice == self._frontCamera)
                {
                    var context:CGContextRef = UIGraphicsGetCurrentContext()
                    pickedImage = UIImage(CGImage: pickedImage.CGImage, scale: 1.0, orientation: .UpMirrored)
                    pickedImage.drawInRect(CGRectMake(0, 0, pickedImage.size.width, pickedImage.size.height))
                    pickedImage = UIGraphicsGetImageFromCurrentImageContext()
                }
                UIGraphicsEndImageContext()
                var image:iImage = iImage(uiimage: pickedImage)
                self._captureInProcess = false
                callback(image)
            }
        }
    }

如果我通过提高 y 值来调整 AVCaptureVideoPreviewLayer 的知名度,我只会得到一个显示偏移量的黑条。我很好奇为什么最上面的视频帧与我的输出图像不匹配。

我确实“裁剪”了相机,所以它是一个完美的正方形,但为什么实时相机画面的顶部不是实际顶部(因为图像默认位于更高的位置,相机画面不显示)

更新:

这是我所说的之前和之后的屏幕截图

之前: Before image这就是实时提要显示的内容

之后: After image这是用户点击拍照时的结果图像

最佳答案

代替

_videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill

你可以试试

_videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspect

一般来说,预览和捕获的图像宽度和高度必须匹配。您可能需要对预览或最终图像或两者进行更多“裁剪”。

关于iOS 相机视频实时预览与拍摄的照片有偏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26447180/

相关文章:

security - 如何通过软件连接到CCTV摄像机?

ios - Swift:使用 Realm 重新连接互联网时,如何将 iPhone 中的本地数据与后端服务器同步?

Windows 与 Sony Camera Remote API 的兼容性

ios - 如何在 objective-c 中调用的方法中传递五个值

arrays - 在 SWIFT 中过滤字符串数组的谓词抛出错误,指出 NSCFString 不是键值编码

ios - 是否有可能从 swift 中的二维码读取中触发 segue

swift - 在 Swift 中向数组中插入一个新元素

iphone - ipad 2 摄像头支持检测

ios - 如何使用 NSExtensionPrincipalClass 显示 Storyboard UI 进行共享扩展

iphone - iOS PhotoPicker 示例在 4.0 模拟器下崩溃