2016-12-27 4 views
0

В настоящее время я создаю приложение iOS, которое использует камеру для захвата 15 кадров в секунду в течение 30 секунд (всего 450 кадров). Проблема в том, что [self.session startRunning] (последняя строка предоставленного кода), похоже, не работает; Я говорю это, потому что я создал массив под названием hues, чтобы принимать средние красные значения каждого из 450 кадров, которые должны быть захвачены. Однако даже после запуска и остановки обнаружения массив остается пустым. Что мне не хватает?self.startRunning не работает должным образом

#import "ViewController.h" 
#import <AVFoundation/AVFoundation.h> 

@interface ViewController() 

@property (nonatomic, strong) AVCaptureSession *session; 
@property (nonatomic, strong) NSMutableArray *hues; 

@end 

const int SECONDS = 15; 
const int FPS = 30; 

@implementation ViewController 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

self.hues = [[NSMutableArray alloc] init]; // initiate things (from old code) 
self.session = [[AVCaptureSession alloc] init]; 
self.session.sessionPreset = AVCaptureSessionPresetLow; 

NSInteger numberOfFramesCaptured = self.hues.count; 

// initialize the session with proper settings (from docs) 

NSError *error = nil; 

AVCaptureDevice *captureDevice; // initialize captureDevice and input, and add input (from old code) 
AVCaptureDeviceInput *videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:captureDevice error:&error]; 
if ([self.session canAddInput:videoInput]) 
{ // fails 
    [self.session addInput:videoInput]; 
} 

// find the max fps we can get from the given device (from old code) 
AVCaptureDeviceFormat *currentFormat = [captureDevice activeFormat]; 

for (AVCaptureDeviceFormat *format in captureDevice.formats) // executes twice 
{ // skips all of this 
    NSArray *ranges = format.videoSupportedFrameRateRanges; 
    AVFrameRateRange *frameRates = ranges[0]; 

    // find the lowest resolution format at the frame rate we want (from old code) 
    if (frameRates.maxFrameRate == FPS && (!currentFormat || (CMVideoFormatDescriptionGetDimensions(format.formatDescription).width < CMVideoFormatDescriptionGetDimensions(currentFormat.formatDescription).width && CMVideoFormatDescriptionGetDimensions(format.formatDescription).height < CMVideoFormatDescriptionGetDimensions(currentFormat.formatDescription).height))) 
    { 
     currentFormat = format; 
    } 
} 

// tell the device to use the max frame rate (from old code) 
[captureDevice lockForConfiguration:nil]; 
captureDevice.torchMode = AVCaptureTorchModeOn; 
captureDevice.activeFormat = currentFormat; 
captureDevice.activeVideoMinFrameDuration = CMTimeMake(1, FPS); 
captureDevice.activeVideoMaxFrameDuration = CMTimeMake(1, FPS); 
[captureDevice unlockForConfiguration]; 

// set the output (from old code) 
AVCaptureVideoDataOutput *videoOutput = [[AVCaptureVideoDataOutput alloc] init]; 

// create a queue to run the capture on (from old code) 
dispatch_queue_t queue = dispatch_queue_create("queue", NULL); 

// setup our delegate (from old code) 
[videoOutput setSampleBufferDelegate:self queue:queue]; 

// configure the pixel format (from old code) 
videoOutput.videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA], (id)kCVPixelBufferPixelFormatTypeKey, nil]; 
videoOutput.alwaysDiscardsLateVideoFrames = NO; 

[self.session addOutput:videoOutput]; 

// start the video session 
[self.session startRunning]; // PROBLEM OCCURS HERE 

ответ

0

В вашем коде AVCaptureDevice *captureDevice; не инициализирован.

+0

Это проблема? – fi12

+0

Как я могу инициализировать его? – fi12

+0

Вы должны выбрать подходящее устройство из '[AVCaptureDevice devicesWithMediaType: AVMediaTypeVideo];', это может быть передняя или задняя камера – vojer

Смежные вопросы