Commit cc8f9ed8 authored by Matthias Rütten's avatar Matthias Rütten

cleanup code

parent 73cd2c0f
......@@ -66,8 +66,6 @@
- (void)captureOutput:(AVCaptureOutput*)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection*)connection;
- (NSString*)formatStringFrom:(zxing::BarcodeFormat)format;
- (UIImage*)getImageFromSample:(CMSampleBufferRef)sampleBuffer;
- (zxing::Ref<zxing::LuminanceSource>) getLuminanceSourceFromSample:(CMSampleBufferRef)sampleBuffer imageBytes:(uint8_t**)ptr;
- (UIImage*) getImageFromLuminanceSource:(zxing::LuminanceSource*)luminanceSource;
@end
//------------------------------------------------------------------------------
......@@ -445,128 +443,6 @@
if (format.type == AVMetadataObjectTypePDF417Code) return @"PDF_417";
return @"???";
}
//--------------------------------------------------------------------------
// convert capture's sample buffer (scanned picture) into the thing that
// zxing needs.
//--------------------------------------------------------------------------
- (zxing::Ref<zxing::LuminanceSource>) getLuminanceSourceFromSample:(CMSampleBufferRef)sampleBuffer imageBytes:(uint8_t**)ptr {
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer, 0);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
uint8_t* baseAddress = (uint8_t*) CVPixelBufferGetBaseAddress(imageBuffer);
// only going to get 90% of the min(width,height) of the captured image
size_t greyWidth = 9 * MIN(width, height) / 10;
uint8_t* greyData = (uint8_t*) malloc(greyWidth * greyWidth);
// remember this pointer so we can free it later
*ptr = greyData;
if (!greyData) {
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
throw new zxing::ReaderException("out of memory");
}
size_t offsetX = (width - greyWidth) / 2;
size_t offsetY = (height - greyWidth) / 2;
// pixel-by-pixel ...
for (size_t i=0; i<greyWidth; i++) {
for (size_t j=0; j<greyWidth; j++) {
// i,j are the coordinates from the sample buffer
// ni, nj are the coordinates in the LuminanceSource
// in this case, there's a rotation taking place
size_t ni = greyWidth-j;
size_t nj = i;
size_t baseOffset = (j+offsetY)*bytesPerRow + (i + offsetX)*4;
// convert from color to grayscale
// http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale
size_t value = 0.11 * baseAddress[baseOffset] +
0.59 * baseAddress[baseOffset + 1] +
0.30 * baseAddress[baseOffset + 2];
greyData[nj*greyWidth + ni] = value;
}
}
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
using namespace zxing;
Ref<LuminanceSource> luminanceSource (
new GreyscaleLuminanceSource(greyData, (int)greyWidth, (int)greyWidth, 0, 0, (int)greyWidth, (int)greyWidth)
);
return luminanceSource;
}
//--------------------------------------------------------------------------
// for debugging
//--------------------------------------------------------------------------
- (UIImage*) getImageFromLuminanceSource:(zxing::LuminanceSource*)luminanceSource {
unsigned char* bytes = luminanceSource->getMatrix();
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef context = CGBitmapContextCreate(
bytes,
luminanceSource->getWidth(), luminanceSource->getHeight(), 8, luminanceSource->getWidth(),
colorSpace,
kCGImageAlphaNone
);
CGImageRef cgImage = CGBitmapContextCreateImage(context);
UIImage* image = [[UIImage alloc] initWithCGImage:cgImage];
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
CGImageRelease(cgImage);
free(bytes);
return image;
}
//--------------------------------------------------------------------------
// for debugging
//--------------------------------------------------------------------------
- (UIImage*)getImageFromSample:(CMSampleBufferRef)sampleBuffer {
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer, 0);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
uint8_t* baseAddress = (uint8_t*) CVPixelBufferGetBaseAddress(imageBuffer);
int length = (int)(height * bytesPerRow);
uint8_t* newBaseAddress = (uint8_t*) malloc(length);
memcpy(newBaseAddress, baseAddress, length);
baseAddress = newBaseAddress;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(
baseAddress,
width, height, 8, bytesPerRow,
colorSpace,
kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst
);
CGImageRef cgImage = CGBitmapContextCreateImage(context);
UIImage* image = [[UIImage alloc] initWithCGImage:cgImage];
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
CGImageRelease(cgImage);
free(baseAddress);
return image;
}
@end
//------------------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment