iOS 5后置摄像头在非全屏模式下预览

时间:2011-12-23 05:00:02

标签: ios api camera scale preview

只是想知道这是否可行:

我一直在寻找各种显示相机预览的解决方案;虽然在全屏模式下这样做是相对简单的,但我想做的是将它缩放到屏幕的50%并且与图形并排显示(不是叠加,而是单独的图形)在相机预览的左侧占用相等的空间)。基本上,目的是允许用户将相机预览与图形进行比较。

所以,我需要知道的是: a)是否可以将相机预览缩放到较低的分辨率 b)它可以在iPad上与另一个不是叠加的图形共享屏幕 c)如果a和b为真,请问有什么示例来源吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用下一个代码:

previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
previewLayer.opaque = YES;
previewLayer.contentsScale = self.view.contentScaleFactor;
previewLayer.frame = self.view.bounds;
previewLayer.needsDisplayOnBoundsChange = YES;
[self.view.layer addSublayer:previewLayer];

只需替换第5行即可将预览图层设置为另一帧。 您可以使用此代码

创建captureSession
captureSession = [[AVCaptureSession alloc] init];

if(!captureSession)
{
    NSLog(@"Failed to create video capture session");
    return NO;
}

[captureSession beginConfiguration];

captureSession.sessionPreset = AVCaptureSessionPreset640x480;

AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
videoDevice.position = AVCaptureDevicePositionFront;

if(!videoDevice)
{
    NSLog(@"Couldn't create video capture device");
    [captureSession release];
    captureSession = nil;
    return NO;
}

if([videoDevice isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus])
{
    NSError *deviceError = nil;

    if([videoDevice lockForConfiguration:&deviceError])
    {
        [videoDevice setFocusMode:AVCaptureFocusModeContinuousAutoFocus];
        [videoDevice unlockForConfiguration];
    }
    else
    {
        NSLog(@"Couldn't lock device for configuration");
    }
}

NSError *error;
AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];

if(!videoIn)
{
    NSLog(@"Couldn't create video capture device input: %@ - %@", [error localizedDescription], [error localizedFailureReason]);
    [captureSession release];
    captureSession = nil;
    return NO;
}

if(![captureSession canAddInput:videoIn])
{
    NSLog(@"Couldn't add video capture device input");
    [captureSession release];
    captureSession = nil;
    return NO;
}

[captureSession addInput:videoIn];
[captureSession commitConfiguration];