Hallo!
Ich versuche unter macOS und mit XCode 12.4 unter 10.15.7 von der Kamera einzelne Frames abzugreifen.
Das sollte laut Beschreibung so gehen (etwas gekürzt):
AVCaptureSession *session;
AVCaptureDevice *device;
AVCaptureDeviceInput *input;
AVCaptureVideoDataOutput *output;
- (void)startCamera
{
session = [[AVCaptureSession alloc] init];
device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
[session addInput:input];
output = [[AVCaptureVideoDataOutput alloc] init];
output.videoSettings = @{(NSString *)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA)};
output.alwaysDiscardsLateVideoFrames = YES;
dispatch_queue_t queue = dispatch_queue_create_with_target(NULL, DISPATCH_QUEUE_SERIAL, NULL);
[output setSampleBufferDelegate:self queue:queue];
dispatch_release(queue);
[session addOutput:output];
[session startRunning];
}
// delegate
- (void)captureOutput:(AVCaptureOutput *)output
didOutputSampleBuffer:(CMSampleBufferRef)sample
fromConnection:(AVCaptureConnection *)connection
{
CVImageBufferRef buf = CMSampleBufferGetImageBuffer(sample);
CIImage *image = [CIImage imageWithCVImageBuffer:buf];
CGColorSpaceRef csp = CGColorSpaceCreateDeviceRGB();
image = [image imageByColorMatchingWorkingSpaceToColorSpace:csp];
NSBitmapImageRep *b = [[NSBitmapImageRep alloc] initWithCIImage:image];
[[b TIFFRepresentation] writeToFile:filename atomically:YES];
}
Doch wie immer ich es auch drehe und wende, ob ich über ein CIContext und CGContext zeichne/konvertiere oder über NSBitmapImageRep, das Bild ergibt nur eine braune Brühe.
Weiß vielleicht jemand wie es richtig geht ?
Gruß
Thomas
Ich versuche unter macOS und mit XCode 12.4 unter 10.15.7 von der Kamera einzelne Frames abzugreifen.
Das sollte laut Beschreibung so gehen (etwas gekürzt):
AVCaptureSession *session;
AVCaptureDevice *device;
AVCaptureDeviceInput *input;
AVCaptureVideoDataOutput *output;
- (void)startCamera
{
session = [[AVCaptureSession alloc] init];
device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
[session addInput:input];
output = [[AVCaptureVideoDataOutput alloc] init];
output.videoSettings = @{(NSString *)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA)};
output.alwaysDiscardsLateVideoFrames = YES;
dispatch_queue_t queue = dispatch_queue_create_with_target(NULL, DISPATCH_QUEUE_SERIAL, NULL);
[output setSampleBufferDelegate:self queue:queue];
dispatch_release(queue);
[session addOutput:output];
[session startRunning];
}
// delegate
- (void)captureOutput:(AVCaptureOutput *)output
didOutputSampleBuffer:(CMSampleBufferRef)sample
fromConnection:(AVCaptureConnection *)connection
{
CVImageBufferRef buf = CMSampleBufferGetImageBuffer(sample);
CIImage *image = [CIImage imageWithCVImageBuffer:buf];
CGColorSpaceRef csp = CGColorSpaceCreateDeviceRGB();
image = [image imageByColorMatchingWorkingSpaceToColorSpace:csp];
NSBitmapImageRep *b = [[NSBitmapImageRep alloc] initWithCIImage:image];
[[b TIFFRepresentation] writeToFile:filename atomically:YES];
}
Doch wie immer ich es auch drehe und wende, ob ich über ein CIContext und CGContext zeichne/konvertiere oder über NSBitmapImageRep, das Bild ergibt nur eine braune Brühe.
Weiß vielleicht jemand wie es richtig geht ?
Gruß
Thomas
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von Thomas ()