ios切换摄像头前后分辨率改变 AVCaptureSessionPresetHigh
<p><p>我使用了 GPUImage 库,我的前置摄像头 session 预设是 AVCaptureSessionPresetPhoto,后置摄像头是 AVCaptureSessionPresetHigh,</p>
<pre><code>if (self.isFrontFacingCameraPresent) {
;
} else {
;
}
;
</code></pre>
<ol>
<li><p>初始状态为前置摄像头,分辨率为1280x960;</p></li>
<li><p>现在换了后置摄像头,分辨率为1920x1080;</p></li>
<li><p>然后换前置摄像头,分辨率是1280x720,很奇怪;</p></li>
</ol>
<p>我检查了这个委托(delegate)方法:</p>
<pre><code>- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
</code></pre>
<p>获取宽度和高度:</p>
<pre><code> CVImageBufferRef cameraFrame = CMSampleBufferGetImageBuffer(sampleBuffer);
int bufferWidth = (int) CVPixelBufferGetWidth(cameraFrame);
int bufferHeight = (int) CVPixelBufferGetHeight(cameraFrame);
</code></pre>
<p>bufferHeight是720,我不明白为什么换后置前置摄像头时,高度从960变成了720!可能是苹果的bug?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我解决了这个问题,再见改变rotateCamera函数,我重写了一个用于在前后切换相机的函数:</p>
<pre><code>- (void)switchCameraFrontAndBack {
NSError *error;
AVCaptureDeviceInput *newVideoInput;
AVCaptureDevicePosition currentCameraPosition = self.cameraPosition;
if (currentCameraPosition == AVCaptureDevicePositionBack)
{
currentCameraPosition = AVCaptureDevicePositionFront;
}
else
{
currentCameraPosition = AVCaptureDevicePositionBack;
}
AVCaptureDevice *backFacingCamera = nil;
NSArray *devices = ;
for (AVCaptureDevice *device in devices)
{
if ( == currentCameraPosition)
{
backFacingCamera = device;
}
}
newVideoInput = [ initWithDevice:backFacingCamera error:&error];
if (newVideoInput != nil)
{
;
;
;
if ()
{
;
videoInput = newVideoInput;
}
else
{
;
}
;
}
_inputCamera = backFacingCamera;
;
}
- (void)configSessionPreset:(AVCaptureDevicePosition)currentPosition {
if (currentPosition == AVCaptureDevicePositionBack) {
if (WIDTH <= Iphone4SWidth) {
if () {
;
} else if () {
;
}
} else {
if () {
;
} else {
;
}
}
} else {
;
}
}
</code></pre></p>
<p style="font-size: 20px;">关于ios切换摄像头前后分辨率改变 AVCaptureSessionPresetHigh,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/33728032/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/33728032/
</a>
</p>
页:
[1]