I am making an application that scans a barcode that inverts color (black background and white stripes). I have to use AVFoundation. I am currently using AVCaptureMetadataOutput. I can make it work perfectly with a regular barcode. I need to invert the color to white -> black and black -> white, etc. Can I add CIColorInvertto enter inAVCaptureSession
- (void)viewDidLoad
{
[super viewDidLoad];
mCaptureSession = [[AVCaptureSession alloc] init];
AVCaptureDevice *videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoCaptureDevice error:&error];
if([mCaptureSession canAddInput:videoInput]){
[mCaptureSession addInput:videoInput];
} else {
NSLog(@"Could not add video input: %@", [error localizedDescription]);
}
AVCaptureMetadataOutput *metadataOutput = [[AVCaptureMetadataOutput alloc] init];
if([mCaptureSession canAddOutput:metadataOutput]){
[mCaptureSession addOutput:metadataOutput];
[metadataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
[metadataOutput setMetadataObjectTypes:@[AVMetadataObjectTypeCode39Code]];
} else {
NSLog(@"Could not add metadata output");
}
AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:mCaptureSession];
CGRect bounds=self.view.layer.bounds;
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
previewLayer.bounds=bounds;
previewLayer.position=CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
NSArray *filters = [[NSArray alloc] initWithObjects:[CIFilter filterWithName:@"CIColorInvert"], nil];
[previewLayer setFilters:filters];
[self.view.layer addSublayer:previewLayer];
[mCaptureSession startRunning];
}
source
share