我使用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!)
}
我认为问题是相机的视角,就像我捕获图像后,这一切都是好的。如何正确设置摄像机视图?
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。