簡體   English   中英

找不到AVCapture會話。 無法添加視頻輸入

[英]AVCapture Session not found. Couldn't add video input

我正在嘗試使用AVCaptureSession制作相機應用。 現在,我只想看看視頻輸入是否有效。 但似乎沒有任何輸入,我似乎無法理解為什么。

- (void)viewDidLoad
{
    [super viewDidLoad];

    session = [[AVCaptureSession alloc] init];

    [self addVideoPreviewLayer];

    CGRect layerRect = [[[self view] layer] bounds];

    [[self  previewLayer] setBounds:layerRect];
    [[self  previewLayer] setPosition:CGPointMake(CGRectGetMidX(layerRect),
                                                                  CGRectGetMidY(layerRect))];
    [[[self view] layer] addSublayer:[self previewLayer]];

    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    myButton.frame = CGRectMake(80, 320, 200, 44);
    [myButton setTitle:@"Click Me!" forState:UIControlStateNormal];
    [myButton addTarget:self action:@selector(scanButtonPressed) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:myButton];
}

-(void)addVideoPreviewLayer
{
    [self setPreviewLayer:[[[AVCaptureVideoPreviewLayer alloc] initWithSession:[self session]] autorelease]];
    [[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];
}

-(void) addVideoInput
{
    AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];   
    if (videoDevice) 
    {
        NSError *error;
        AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
        if (!error) 
        {
            if ([[self session] canAddInput:videoIn])
                [[self session] addInput:videoIn];
            else
                NSLog(@"Couldn't add video input");     
        }
        else
            NSLog(@"Couldn't create video input");
    }
    else
        NSLog(@"Couldn't create video capture device");
}

-(IBAction)scanButtonPressed
{
    [self addVideoInput];
}

這是我的方法。 這是從多個函數壓縮而成的,因此它可能不是可編譯的代碼,並且大部分已刪除了錯誤處理。

captureSession = [[AVCaptureSession alloc] init];
captureSession.sessionPreset = AVCaptureSessionPresetMedium;

AVCaptureDevice *videoDevice;
videoDevice = [self frontFacingCamera];
if (videoDevice == nil) {
    videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];    
}

if ( videoDevice ) {
    NSError *error;
    videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error]; 

    [captureSession addInput:self.videoInput];  
}

videoOutput = [[AVCaptureVideoDataOutput alloc] init];

[videoOutput setAlwaysDiscardsLateVideoFrames:NO];

AVCaptureConnection *conn = [videoOutput connectionWithMediaType:AVMediaTypeVideo];

if (conn.supportsVideoMinFrameDuration)
    conn.videoMinFrameDuration = CMTimeMake(1, frameRate);
if (conn.supportsVideoMaxFrameDuration)
    conn.videoMaxFrameDuration = CMTimeMake(1, frameRate);

NSDictionary *videoSettings = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
    [videoOutput setVideoSettings:videoSettings]; 
[videoOutput setSampleBufferDelegate:self queue:capture_queue];

if ([captureSession canAddOutput:videoOutput])
    [captureSession addOutput:videoOutput];
else
    NSLog(@"Couldn't add video output");    

[self.captureSession startRunning];

previewLayer.session = captureSession;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM