#import #import @interface Capture : NSObject { QTCaptureDevice *device; QTCaptureSession *session; QTCaptureDeviceInput *cdi; QTCaptureDecompressedVideoOutput *dvo; NSString *fileName; int frame; } - (id) initWithDevice: (QTCaptureDevice*) aDevice; - (void) setFileName: (NSString*) newName; - (BOOL) start; - (BOOL) stop; @end @implementation Capture - (id) initWithDevice: (QTCaptureDevice*) aDevice { self = [self init]; if (self) { device = aDevice; [device retain]; session = nil; frame = 0; fileName = [@"image%04d.jpeg" copy]; } return self; } - (void) setFileName: (NSString*) newName { if (newName == fileName) return; [fileName release]; [(fileName = newName) retain]; } - (BOOL) start { NSError *error = nil; if (![device isOpen] && ![device open:&error]) { NSLog(@"cannot open %@: %@", device, error); return NO; } if (!session) { session = [[QTCaptureSession alloc] init]; cdi = [[QTCaptureDeviceInput alloc] initWithDevice:device]; if (![session addInput:cdi error:&error]) { NSLog(@"cannot add %@ to session: %@", device, error); return NO; } dvo = [[QTCaptureDecompressedVideoOutput alloc] init]; [dvo setDelegate:self]; [dvo setMinimumVideoFrameInterval:1.0]; if (![session addOutput:dvo error:&error]) { NSLog(@"cannot add %@ to decompressor: %@", device, error); return NO; } } [session startRunning]; return YES; } - (void) dealloc { if (session) { if ([session isRunning]) [self stop]; [session release]; [dvo release]; [cdi release]; } [device release]; [fileName release]; [super dealloc]; } - (BOOL) stop { if (!session) return NO; [session stopRunning]; return YES; } - (void)captureOutput:(QTCaptureOutput *)captureOutput didOutputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection { NSLog(@"captureOutput called"); CIImage *image = [CIImage imageWithCVImageBuffer:videoFrame]; NSLog(@"icaptureOutput: %@", captureOutput); NSBitmapImageRep *ir = [[NSBitmapImageRep alloc] initWithCIImage:image]; NSLog(@" ir: %@", ir); NSData *bin = [ir representationUsingType:NSJPEGFileType properties:nil]; NSLog(@" jpeg (%d)", [bin length]); [bin writeToFile:[NSString stringWithFormat:fileName, frame++] atomically:YES]; [ir release]; } @end int active = 1; int main() { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; [NSApplication sharedApplication]; NSArray *devices = [QTCaptureDevice inputDevicesWithMediaType: QTMediaTypeVideo]; int devId = 0; for (QTCaptureDevice *device in devices) { NSLog(@"device: %@", device); Capture *cap = [[Capture alloc] initWithDevice:(QTCaptureDevice*) device]; [cap setFileName:[NSString stringWithFormat:@"image.%d.%%04d.jpeg", ++devId]]; [cap start]; } if ([devices count] == 0) { NSLog(@"no devices found, terminating"); return 1; } while (active) { // we run it by hand in case we implement a way to set "active" [[NSRunLoop currentRunLoop] runUntilDate: [NSDate dateWithTimeIntervalSinceNow:1.0]]; } [pool release]; return 0; }