简体   繁体   English

全屏相机

[英]Full Screen Camera

I am having trouble with AVCapture. 我在使用AVCapture时遇到了麻烦。

Backstory: I made a new storyboard for my app for the iPad my copying the iPhone Storyboard and changing the source code of the new storyboard by adding .iPad where it was needed. 背景故事:我为我的iPad应用程序制作了一个新的故事板我复制了iPhone故事板并通过在需要的地方添加.iPad来更改新故事板的源代码。 Currently, my app only uses portrait mode. 目前,我的应用只使用纵向模式。

AVCapture however, (which I am using to show a live feed of the camera) only fills part of the screen, with black bars on the outside. 然而,AVCapture(我用于显示相机的实时馈送)仅填充屏幕的一部分,外部有黑条。 I also noticed it does this on the 3.5" screen iPhones. 我也注意到它在3.5英寸屏幕iPhone上做到了这一点。

I will add code as requested. 我会按要求添加代码。

Please make all instructions super easy to follow, this is my first app, and I am a newbie. 请使所有说明超级易于遵循,这是我的第一个应用程序,我是一个新手。

Thanks!! 谢谢!! Branch

EDIT: I tried this code from Apple's documentation, but it didn't work: 编辑:我从Apple的文档中尝试了这段代码,但它没有用:

AVCaptureSession *captureSession; AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession]; UIView *aView; previewLayer.frame = aView.bounds; [aView.layer addSublayer:previewLayer];

The camera's aspect ratio is different from the iPad screen's aspect ratio. 相机的宽高比与iPad屏幕的宽高比不同。 You can change the videoGravity property of your AVCaptureVideoPreviewLayer to fill the whole layer (this will crop the input, but you won't see the bars): 您可以更改AVCaptureVideoPreviewLayervideoGravity属性以填充整个图层(这将裁剪输入,但您不会看到条形图):

captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

You'll still get the whole camera input, not just the layer preview, when you actually capture the image. 当您实际捕获图像时,您仍将获得整个相机输入,而不仅仅是图层预览。 You'll need to do math (geometry) based on your UI to rotate and crop it to get the image that the user is actually seeing. 您需要根据UI进行数学运算(几何)以旋转并裁剪它以获取用户实际看到的图像。

Maybe my issue is similar. 也许我的问题很相似。 For solution, do these as follow: 对于解决方案,请执行以下操作:

  1. set session preset as AVCaptureSessionPresetHigh 将会话预设为AVCaptureSessionPresetHigh
  2. set previewLayer's videoGravity property to AVLayerVideoGravityResizeAspectFill 将previewLayer的videoGravity属性设置为AVLayerVideoGravityResizeAspectFill
  3. set preview's frame as device screen's bounds 将预览的帧设置为设备屏幕的边界

I posted my solutions today for reporting an issue by using the DBCamera, here is the full contents: 我今天发布了我的解决方案,通过使用DBCamera报告问题,这里是完整的内容:


This is maybe not a big thing, but it's useful for whoever cares about why photo taken from custom full-screen camera preview is not look exactly same. 这可能不是什么大事,但对于那些关心为什么从自定义全屏相机预览中拍摄的照片看起来不完全相同的人来说,这是有用的。

I noticed that using a custom camera view, which subclassed from DBCameraView, and call openCustomCamera from the given example, will generate a full-screen preview. 我注意到使用自定义的摄像机视图(从DBCameraView继承,并从给定的示例调用openCustomCamera)将生成全屏预览。 And the captured photo from this full-screen preview on iPhone 5s and 6s will get some extra space from both side, which means the final photo will not look exactly the same from the preview before trigger the camera. 从iPhone 5s和6s上的全屏预览拍摄的照片将从两侧获得一些额外的空间,这意味着在触发相机之前,最终照片在预览中看起来不完全相同。

This is because: 这是因为:
1. the session preset in code is AVCaptureSessionPresetPhoto 1.代码中预设的会话是AVCaptureSessionPresetPhoto
2. the previewLayer's videoGravity is set to AVLayerVideoGravityResizeAspectFill 2. previewLayer的videoGravity设置为AVLayerVideoGravityResizeAspectFill
3. the preview's frame is device screen's bounds 3.预览的框架是设备屏幕的边界

The videoGravity for previewLayer is AVLayerVideoGravityResizeAspect by system default. 用于previewLayer的videoGravity是系统默认的AVLayerVideoGravityResizeAspect。 When you make the session preset as AVCaptureSessionPresetPhoto and make the preview's size as screen size, the system can not satisfy the both conditions because I think the AVCaptureSessionPresetPhoto will adjust the preview's size, which is resize aspect, to corresponds the high-resolution captured photo ( 4:3 ?). 当您将会话预设为AVCaptureSessionPresetPhoto并将预览的大小设置为屏幕大小时,系统无法满足这两个条件,因为我认为AVCaptureSessionPresetPhoto将调整预览的大小(即调整大小方面)以对应高分辨率捕获的照片( 4:3?)。 It's aspect ratio seems not as same as screen's (less obvious for 4s), so you can see the black areas appear on both top and bottom even if you set preview's frame equal to screen's bounds . 它的宽高比似乎与屏幕不同(4s不太明显), 因此即使您将预览的帧设置为等于屏幕边界您也可以看到黑色区域同时出现在顶部和底部 However the DBCamera set AVLayerVideoGravityResizeAspectFill to videoGravity in code. 但是DBCamera在代码中将AVLayerVideoGravityResizeAspectFill设置为videoGravity。 It will cause the preview is trying to fill the full screen and maintain its correct aspect ratio. 这将导致预览尝试填满整个屏幕并保持其正确的宽高比。 The custom camera view seems filling the entire device screen, but the actual preview is bigger than the screen size with extra spaces extended beyond screen from both left and right side. 自定义摄像机视图似乎填满整个设备屏幕,但实际预览大于屏幕尺寸,从左侧和右侧延伸超出屏幕的额外空间。

To fix the problem to get the exact same photo from full screen preview, set AVCaptureSessionPresetHigh instead of AVCaptureSessionPresetPhoto. 要解决问题以从全屏预览中获取完全相同的照片,请设置AVCaptureSessionPresetHigh而不是AVCaptureSessionPresetPhoto。 It will work. 它会工作。 The downside is the quality of photo may not remain the same, that is the trade off. 缺点是照片的质量可能不会保持不变,那就是权衡。

Correct me if I am wrong, and hopefully it helps. 如果我错了,请纠正我,希望它有所帮助。

https://github.com/danielebogo/DBCamera/issues/194 https://github.com/danielebogo/DBCamera/issues/194

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

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