简体   繁体   English

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

[英]iOS 5 rear camera preview in non full-screen mode

just wondering if this is possible: 只是想知道这是否可能:

I've been looking at various solutions for displaying the camera preview; 我一直在寻找用于显示摄像机预览的各种解决方案。 and while doing so in full-screen mode is relatively straight-forward, what I'd like to do is to have it scaled to 50% of the screen and presented side by side with a graphic (not an overlay, but a separate graphic to the left of the camera preview which takes up equal space). 虽然在全屏模式下这样做相对简单,但我想做的是将其缩放到屏幕的50%,并与图形(不是重叠图形,而是单独的图形)并排显示相机预览的左侧(占据相等的空间)。 Basically the purpose is to allow the user to compare the camera preview with the graphic. 基本上,目的是允许用户将相机预览与图形进行比较。

So, what I need to know is: a) is it possible to scale the camera preview to a lower resolution b) can it share the screen on an iPad with another graphic which isn't an overlay c) if a and b are true, is there any example source I might be pointed to please? 因此,我需要知道的是:a)是否可以将摄像头预览缩放到较低的分辨率b)是否可以将iPad上的屏幕与另一幅不重叠的图形共享屏幕c)如果a和b为true ,请问是否有任何示例来源可供参考?

Thanks! 谢谢!

You can just use next code: 您可以只使用下一个代码:

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];

Just replace line 5 to set preview layer another frame. 只需替换第5行,即可将预览层设置为另一帧。 You can create captureSession with this code 您可以使用以下代码创建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];

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

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