2012-01-06 2 views
0

AVCaptureSessionを使用してカメラアプリケーションを作成しようとしています。今のところ、私はちょうどビデオ入力が動作するかどうかを見たいと思っています。しかし、入力がないように見えますし、なぜ私は理解できないようです。AVCaptureセッションが見つかりません。ビデオ入力を追加できませんでした

- (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]; 
} 
+0

このコードの結果(コンソール出力)は何ですか? – Till

+0

@Tillビデオ入力を追加できませんでした。 – ilaunchpad

答えて

0

これはどのように行うのですか。これは複数の関数から集約されているため、コンパイル可能なコードではない可能性があり、大部分はエラー処理が削除されています。

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; 
+0

ありがとうございます。私はいくつかのものを変更し、それは働いた。 – ilaunchpad

関連する問題