Skip to content

Instantly share code, notes, and snippets.

@jebai0521
Created June 16, 2015 03:54
Show Gist options
  • Save jebai0521/e8cd16d2ad79d8b0b59a to your computer and use it in GitHub Desktop.
Save jebai0521/e8cd16d2ad79d8b0b59a to your computer and use it in GitHub Desktop.
- (void)captureOutput1:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
if (_finished) {
NSLog(@"Enter captureOutput:didOutputSampleBuffer, but return because finished");
return;
}
_zxcount ++;
// NSLog(@"Enter captureOutput:didOutputSampleBuffer %lu", (unsigned long)_zxcount);
@autoreleasepool {
CVImageBufferRef videoFrame = CMSampleBufferGetImageBuffer(sampleBuffer);
ZXResult* result;
if (_zxcount % 2 == 0) {
CGImageRef videoFrameImage = [ZXCGImageLuminanceSource createImageFromBuffer:videoFrame];
CGImageRef rotatedImage = [self createRotatedImage:videoFrameImage degrees:90];
ZXLuminanceSource *source = [[ZXCGImageLuminanceSource alloc] initWithCGImage:rotatedImage];
ZXBinaryBitmap *bitmap = [[ZXBinaryBitmap alloc] initWithBinarizer:[[ZXHybridBinarizer alloc] initWithSource:source]];
result = [_reader decode:bitmap hints:_hints error:nil];
CGImageRelease(videoFrameImage);
CGImageRelease(rotatedImage);
} else {
ZXLuminanceSource *source = [[ZXCGImageLuminanceSource alloc] initWithBuffer:videoFrame];
ZXBinaryBitmap *bitmap = [[ZXBinaryBitmap alloc] initWithBinarizer:[[ZXHybridBinarizer alloc] initWithSource:source]];
result = [_reader decode:bitmap hints:_hints error:nil];
}
[_reader reset];
if (result) {
_finished = YES;
// NSLog(@"zbar scan count %d", [_scanner scanImage:[[ZBarImage alloc] initWithCGImage:videoFrameImage]]);
NSLog(@"ZXing type ==> %@", [BSType typeByFormat:result.barcodeFormat]);
NSLog(@"ZXing value ==> %@", result.text);
[[self results] addSymbolWithCode:result.text type:[BSType typeByFormat:result.barcodeFormat]];
if ([NSThread isMainThread])
{
[self dismissViewControllerAnimated:YES completion:^{
_completeBlock(self, [self results]);
}];;
}
else
{
dispatch_sync(dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:YES completion:^{
_completeBlock(self, [self results]);
}];
});
}
}
}
// NSLog(@"Leave captureOutput %lu", (unsigned long)_zxcount);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment