开发者社区> 问答> 正文

使用GLKView的模糊和拉伸摄像机视图

我使用AVCaptureSession和GLKView捕获图像。相机的视野模糊和伸展,但拍摄的图像都是好的。

我的应用程序摄像头视图

图像拉伸

和本地摄像机预览

iPhone摄像头应用程序

这是我的代码

设置摄像机视图

CameraSuperView

fileprivate lazy var cameraSuperView: VideoCameraView = {
let cameraView = VideoCameraView(frame: self.view.bounds)
cameraView.contentMode = .scaleAspectFit
cameraView.backgroundColor = UIColor.clear
cameraView.clipsToBounds = true

return cameraView
}()

将其添加到视频节目中

self.view.addSubview(cameraSuperView)
videoCamera = VideoCamera(cameraView: cameraSuperView)

视频类的init()方法

init(cameraView: VideoCameraView) {
self.cameraView = cameraView
filterImageManager = ImageFilterManager()
cameraView.layer.addSublayer(shapeLayer)

super.init()

self.setupCameraView()  <<<---<<<-----<<-------

self.cameraView.cameraLifeCicleDelegate = self
self.cameraView.flashableDelegate = self

NotificationCenter.default.addObserver(self, selector: #selector(cameraDidEnterBackground),
                                       name: NSNotification.Name.UIApplicationWillResignActive, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(cameraDidEnterForeground),
                                       name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)
}

fileprivate func setupCameraView() {    <<<---<<<-----<<-------
self.setupGLKView()  <<<<=======

let allDevices = AVCaptureDevice.devices(for: AVMediaType.video)
let aDevice: AnyObject? = allDevices.first as AnyObject?

if aDevice == nil {
  return
}

self.captureSession.beginConfiguration()
self.captureDevice = (aDevice as! AVCaptureDevice)

objCaptureDeviceInput = try! AVCaptureDeviceInput(device: self.captureDevice!)

/// Change sessionPreset on iPad & iPhone
switch deviceType {
case .phone: self.captureSession.sessionPreset = AVCaptureSession.Preset.photo//JD -> high
case .pad: self.captureSession.sessionPreset = AVCaptureSession.Preset.photo
default: break
}

self.captureSession.addInput(objCaptureDeviceInput!)

let dataOutput = AVCaptureVideoDataOutput()
dataOutput.alwaysDiscardsLateVideoFrames = true
dataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as AnyHashable: kCVPixelFormatType_32BGRA] as? [String : Any]
dataOutput.setSampleBufferDelegate(self, queue: DispatchQueue.main)
self.captureSession.addOutput(dataOutput)

self.captureSession.addOutput(self.stillImageOutput)

let connection = dataOutput.connections.first
connection?.videoOrientation = .portrait

self.captureSession.commitConfiguration()

//JDS: Setting camera exposure when camera capture session is ready "AVCaptureSessionDidStartRunning"
NotificationCenter.default.addObserver(self, selector: #selector(updateExposeMode), name: .AVCaptureSessionDidStartRunning, object: nil)
}

在这里设置GLKView

fileprivate func setupGLKView() {    <<<<=======
if let _ = self.context {
  return
}

self.context = EAGLContext(api: .openGLES2)
self.glkView = GLKView(frame: cameraView.bounds, context: self.context!)
self.glkView?.frame = cameraView.frame
self.glkView!.autoresizingMask = ([UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight])
self.glkView!.translatesAutoresizingMaskIntoConstraints = true
self.glkView!.contentScaleFactor = 1.0
self.glkView!.drawableDepthFormat = .format24


cameraView.insertSubview(self.glkView!, at: 0)
glGenRenderbuffers(1, &self.renderBuffer)
glBindRenderbuffer(GLenum(GL_RENDERBUFFER), self.renderBuffer)

self.coreImageContext = CIContext(eaglContext: self.context!, options: [kCIContextUseSoftwareRenderer: true])
EAGLContext.setCurrent(self.context!)
}

我认为问题是相机的视角,就像我捕获图像后,这一切都是好的。如何正确设置摄像机视图?

展开
收起
游客5akardh5cojhg 2019-12-11 21:24:23 727 0
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
3D动画的菜谱式灯光与云渲染 立即下载
营销设计场景下的图像和文字生成 立即下载
低代码开发师(初级)实战教程 立即下载