Search code examples
objective-ccocoaquicktime

Flipping Quicktime preview & capture


I need to horizontally flip some video I'm previewing and capturing. A-la iChat, I have a webcam and want it to appear as though the user is looking in a mirror.

I'm previewing Quicktime video in a QTCaptureView. My capturing is done frame-by-frame (for reasons I won't get into) with something like:

imageRep = [NSCIImageRep imageRepWithCIImage: [CIImage imageWithCVImageBuffer: frame]];
image = [[NSImage alloc] initWithSize: [imageRep size]];
[image addRepresentation: imageRep];
[movie addImage: image forDuration: someDuration withAttributes: someAttributes];

Any tips?


Solution

  • You could do this by taking the CIImage you're getting from the capture and running it through a Core Image filter to flip the image around. You would then pass the resulting image into your image rep rather than the original one. The code would look something like:

    CIImage* capturedImage = [CIImage imageWithCVImageBuffer:buffer];
    NSAffineTransform* flipTransform = [NSAffineTransform transform];
    CIFilter* flipFilter;
    CIImage* flippedImage;
    
    [flipTransform scaleByX:-1.0 y:1.0]; //horizontal flip
    flipFilter = [CIImage filterWithName:@"CIAffineTransform"];
    [flipFilter setValue:flipTransform forKey:@"inputTransform"];
    [flipFilter setValue:capturedImage forKey:@"inputImage"];
    flippedImage = [flipFilter valueForKey:@"outputImage"];
    imageRep = [NSCIImageRep imageRepWithCIImage:flippedImage];
    ...