gpt4 book ai didi

iphone - 切换到前置摄像头后相机卡住

转载 作者:行者123 更新时间:2023-12-03 20:27:34 25 4
gpt4 key购买 nike

我的问题

我正在使用 AVFoundation 每 5 秒捕获视频输出的帧。当用户点击按钮时,输入摄像头应从前面切换到后面,反之亦然。问题是,每次我将后置摄像头切换到前置摄像头时,它都会卡住(奇怪的是,它的工作方式相反 - 意思是,前置摄像头切换到后置摄像头)!

我使用的代码

为了在摄像头之间切换,我使用了从 Apple AVCam sample code 获取的准确代码。变量名称略有变化(因此它将与我的代码匹配):

- (BOOL)toggleCamera
{
BOOL success = NO;

if ([self cameraCount] > 1) {
NSError *error;
AVCaptureDeviceInput *newVideoInput;
AVCaptureDevicePosition position = [[self.videoInput device] position];

if (position == AVCaptureDevicePositionBack)
newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self frontFacingCamera] error:&error];

else if (position == AVCaptureDevicePositionFront)
newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self backFacingCamera] error:&error];

else
goto bail;

if (newVideoInput != nil) {

// Start configuring the session.
[[self captureSession] beginConfiguration];

// Remove the current video input device.
[[self captureSession] removeInput:[self videoInput]];

if ([[self captureSession] canAddInput:newVideoInput]) {
[[self captureSession] addInput:newVideoInput];
[self setVideoInput:newVideoInput];
}

else {
[[self captureSession] addInput:[self videoInput]];
}

[[self captureSession] commitConfiguration];

success = YES;
[newVideoInput release];
}

else if (error) {
NSLog(@"PICTURE TAKER: Failed to toggle cameras."
@"\nError: %@", error.localizedDescription);
}
}

bail:
return success;
}

为了设置 AVCaptureSession,我再次使用 AVCam sample code完全相同的代码。 ,添加的输出略有变化(以满足我的需求):

- (BOOL) setupSession
{
BOOL success = NO;

// Set torch and flash mode to auto
if ([[self backFacingCamera] hasFlash]) {
if ([[self backFacingCamera] lockForConfiguration:nil]) {
if ([[self backFacingCamera] isFlashModeSupported:AVCaptureFlashModeAuto]) {
[[self backFacingCamera] setFlashMode:AVCaptureFlashModeAuto];
}
[[self backFacingCamera] unlockForConfiguration];
}
}
if ([[self backFacingCamera] hasTorch]) {
if ([[self backFacingCamera] lockForConfiguration:nil]) {
if ([[self backFacingCamera] isTorchModeSupported:AVCaptureTorchModeAuto]) {
[[self backFacingCamera] setTorchMode:AVCaptureTorchModeAuto];
}
[[self backFacingCamera] unlockForConfiguration];
}
}

// Init the device inputs
AVCaptureDeviceInput *newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self frontFacingCamera] error:nil];

// Create a video output & configure it.
AVCaptureVideoDataOutput *newVideoDataOutput = [[AVCaptureVideoDataOutput alloc] init];
newVideoDataOutput.videoSettings = @{(NSString *)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA)};

// Set the output's delegate.
dispatch_queue_t queue = dispatch_queue_create("MyQueue", NULL);
[newVideoDataOutput setSampleBufferDelegate:self queue:queue];
dispatch_release(queue);

// Create session (use default AVCaptureSessionPresetHigh)
AVCaptureSession *newCaptureSession = [[AVCaptureSession alloc] init];

// Add inputs and output to the capture session
if ([newCaptureSession canAddInput:newVideoInput]) {
[newCaptureSession addInput:newVideoInput];
}

if ([newCaptureSession canAddOutput:newVideoDataOutput]) {
[newCaptureSession addOutput:newVideoDataOutput];
}

[self setVideoOutput:newVideoDataOutput];
[self setVideoInput:newVideoInput];
[self setCaptureSession:newCaptureSession];

[newVideoDataOutput release];
[newVideoInput release];
[newCaptureSession release];

// Get our view's layer.
CALayer *viewLayer = self.view.layer;

// Add the the session's preview layer.
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
captureVideoPreviewLayer.frame = self.view.bounds;
[viewLayer insertSublayer:captureVideoPreviewLayer below:self.imageViewOverlay.layer];
[captureVideoPreviewLayer release];

success = YES;

return success;
}

*重要说明:在调用输出委托(delegate)后,相机立即卡住。

谁能帮我解决这个问题吗?好像我几乎已经尝试了一切!

更新#1

根据要求,self 指的是呈现 session 预览层并管理与相机相关的所有内容的 View Controller 。此外,我发布了处理 frontFacingCamera 部分的代码,该代码(就像我迄今为止发布的所有代码一样)取自 Apple 的 AVCam sample code :

// Find a camera with the specificed AVCaptureDevicePosition, returning nil if one is not found
- (AVCaptureDevice *) cameraWithPosition:(AVCaptureDevicePosition) position
{
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) {
if ([device position] == position) {
return device;
}
}
return nil;
}

// Find a front facing camera, returning nil if one is not found
- (AVCaptureDevice *) frontFacingCamera
{
return [self cameraWithPosition:AVCaptureDevicePositionFront];
}

最佳答案

BOOL isUsingFrontFacingCamera;



- (BOOL) swapCameras
{
if ([self cameraCount] > 1) {
AVCaptureDevicePosition desiredPosition;
if (isUsingFrontFacingCamera) {
desiredPosition = AVCaptureDevicePositionBack;
} else {
desiredPosition = AVCaptureDevicePositionFront;
}

for (AVCaptureDevice *d in [AVCaptureDevice devicesWithMediaType: AVMediaTypeVideo]) {
if ([d position] == desiredPosition) {
[[self session] beginConfiguration];
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:d error:nil];
for (AVCaptureInput *oldInput in [[self session] inputs]) {
[[self session] removeInput:oldInput];
}
[[self session] addInput:input];
[[self session] commitConfiguration];
break;
}
}
isUsingFrontFacingCamera = !isUsingFrontFacingCamera;
return YES;
}
return NO;
}

来自here的回答

已编辑:此来自 Square Cam Apple 示例代码。工作正常。也测试过。 http://developer.apple.com/library/ios/#samplecode/SquareCam/Listings/SquareCam_SqareCamViewController_m.html

//使用前置/后置摄像头

- (IBAction)switchCameras:(id)sender

{

AVCaptureDevicePosition desiredPosition;

if (isUsingFrontFacingCamera)

desiredPosition = AVCaptureDevicePositionBack;

else

desiredPosition = AVCaptureDevicePositionFront;



for (AVCaptureDevice *d in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {

if ([d position] == desiredPosition) {

[[previewLayer session] beginConfiguration];

AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:d error:nil];

for (AVCaptureInput *oldInput in [[previewLayer session] inputs]) {

[[previewLayer session] removeInput:oldInput];

}

[[previewLayer session] addInput:input];

[[previewLayer session] commitConfiguration];

break;

}

}

isUsingFrontFacingCamera = !isUsingFrontFacingCamera;

}

关于iphone - 切换到前置摄像头后相机卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14894445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com