繁体   English   中英

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

[英]iOS Camera Video Live Preview Is Offset To Picture Taken

我正在使用相机。

摄像机向用户呈现实时供稿,并且当他们单击图像时,图像就会创建并传递给用户。

问题在于图像被设计为移至最高位置,该位置高于实时预览显示的位置。

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

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

 //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的名气,我只会得到显示偏移量的黑条。 我非常好奇为什么视频帧的顶部与我的输出图像不匹配。

我“修剪”了照相机,使其成为一个完美的正方形,但是为什么实时摄影机供稿的顶部不是实际的顶部(因为图像默认显示在照相机供稿的更高位置)

更新:

这是我在说的前后屏幕截图

之前: 之前的图像这是实时供稿显示的内容

之后: 图像之后这是用户单击拍照后生成的图像

代替

_videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill

你可以尝试

_videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspect

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

我遇到了同样的问题,此代码对我有用:

previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
var bounds = UIScreen.mainScreen().bounds

previewLayer?.bounds = bounds
previewLayer?.videoGravity = AVLayerVideoGravityResize
previewLayer?.position = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds))
self.cameraPreview.layer.addSublayer(previewLayer)
captureSession.startRunning()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM