获取 iPhone 的相机分辨率?
有没有办法获取iPhone相机的分辨率?显然 3G 有 1200x1600,3GS 有 1500x2000,但是我如何从代码内部获取这些值(不拍照)。我需要它们对相机预览进行一些仿射变换。
我可以检测是 3G 还是 3GS 来硬编码这些值,但这只是最后的手段。
Is there any way to get the resolution of the iPhone's camera? Apparently the 3G has 1200x1600, and 3GS has 1500x2000, but how do I obtain these values from inside my code (without taking the picture). I need them to make some affine transform to the camera preview.
I could detect if it's 3G or 3GS to hardcode these values, but it's just the last resort.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为你的“最后手段”是一个很好的解决方案。
检测模型出了什么问题?这些型号的硬件都不会改变。您可能唯一担心的是 3GSX-R 或其他型号是否配备 16mpx 相机。此时您可能必须更新您的应用程序,并且只需向列表中添加另一个值即可。
我投票支持模型检测。
I think your "last resort" is a good solution.
What's wrong with detecting the model? The hardware isn't going to change for either of those models.. The only concern you might have is if the 3GSX-R or something comes out with a 16mpx camera. At which point you'd probably have to update your app anyways and could just add another value to the list.
I vote for model detection.
这篇文章很旧,但在谷歌上几乎是第一篇,而且没有有效的答案,所以这里还有一个选择:
Solution for iOS from 4.x to 7.x
AV Foundation
框架配置并启动
AVCaptureSession
后,您可以在[[[session.inputs.lastObject] ports].lastObject formatDescription]
变量这是大概的代码:
iOS8 的解决方案
苹果再次改变了一切:现在你必须订阅
AVCaptureInputPortFormatDescriptionDidChangeNotification
这是示例:
The post is old, but it is almost first in google, and it has got no valid answer, so here's one more option:
Solution for iOS from 4.x to 7.x
It's all in terms of
AV Foundation
frameworkAfter
AVCaptureSession
is configured and started you can find video dimensions inside[[[session.inputs.lastObject] ports].lastObject formatDescription]
variableHere's approximate code:
Solution for iOS8
Apple did change everything again: now you must subscribe for
AVCaptureInputPortFormatDescriptionDidChangeNotification
Here is the sample:
您真的需要知道图像的分辨率来设置仿射变换吗?由于视图已预先设置为覆盖屏幕的宽度,因此您可以根据要占用的屏幕部分来更改变换,即,如果您需要预览宽度为 160 像素,只需将其缩小即可比默认值低 50%。我现在正在一个应用程序中执行此操作,它适用于新旧 iPhone ......
Do you really need to know the resolution of the image to set up the affine transform? Since the view is pre-set-up to cover the width of the screen, you can alter the transform based on what fraction of the screen you want to take up, i.e. if you need the preview to be 160 pixels across, just shrink it 50% from the default. I'm doing this in an app now, and it works on new and old iPhones...
由于 Apple 不允许您直接与硬件对话,因此合法的 App Store 产品实际上没有太多选择。
Since Apple doesn't let you talk to the hardware directly, there's not really much choice for a legitimate App Store product.
虽然我认为snicker的答案绝对是正确的。我想我发布这个只是为了好玩。
你可以查看iPhone上用户存储的相册(我做了一个大胆的假设,这可以在iPhone上以编程方式完成,没有任何限制!),假设手机上的照片大部分是用手机自己拍摄的,然后您可以计算出平均照片分辨率是多少,并以此为准。
While I think snicker's answer is definitely the correct one. I thought I'd post this for fun.
You could look through the users stored photo album on the iPhone (I am making a bold assumption that this can be done programatically on the iPhone without restrictions!!) making the assumption that the photos on the phone were mostly taken with the phone it's self, you could then work out what the average photo resolution is and roll with that.
你为什么不想拍照?使用 UIImagePickerController 的 takePicture 方法,指示用户这是必要的校准步骤。之后,查看图片的分辨率,持久保存该值,然后删除您拍摄的图片。之后,您将获得分辨率并能够对其应用转换。
Why don't you want to take a picture? Use UIImagePickerController's takePicture method, instructing the user that this is a necessary calibration step. After that, look at the picture's resolution, persistently save the value, and delete the picture that you took. After that, you'll have your resolution and will be able to apply the transform thereon.