2010-05-12 11:13:27 -07:00
|
|
|
/**
|
|
|
|
* Copyright 2009 Jeff Verkoeyen
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "ZXingWidgetController.h"
|
|
|
|
#import "Decoder.h"
|
|
|
|
#import "NSString+HTML.h"
|
|
|
|
#import "ResultParser.h"
|
|
|
|
#import "ParsedResult.h"
|
|
|
|
#import "ResultAction.h"
|
2011-01-27 08:45:25 -08:00
|
|
|
#import "TwoDDecoderResult.h"
|
2010-05-12 11:13:27 -07:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
|
2010-08-17 14:18:56 -07:00
|
|
|
#import <AVFoundation/AVFoundation.h>
|
|
|
|
|
2010-05-12 11:13:27 -07:00
|
|
|
#define CAMERA_SCALAR 1.12412 // scalar = (480 / (2048 / 480))
|
|
|
|
#define FIRST_TAKE_DELAY 1.0
|
2010-05-18 18:52:38 -07:00
|
|
|
#define ONE_D_BAND_HEIGHT 10.0
|
2010-05-12 11:13:27 -07:00
|
|
|
|
2010-05-30 04:40:09 -07:00
|
|
|
@interface ZXingWidgetController ()
|
|
|
|
|
|
|
|
@property BOOL showCancel;
|
|
|
|
@property BOOL oneDMode;
|
2011-03-16 01:25:50 -07:00
|
|
|
@property BOOL isStatusBarHidden;
|
2010-05-30 04:40:09 -07:00
|
|
|
|
2010-08-17 14:18:56 -07:00
|
|
|
- (void)initCapture;
|
|
|
|
- (void)stopCapture;
|
2010-06-06 05:42:31 -07:00
|
|
|
|
2010-05-30 04:40:09 -07:00
|
|
|
@end
|
|
|
|
|
2010-05-12 11:13:27 -07:00
|
|
|
@implementation ZXingWidgetController
|
2010-08-17 14:18:56 -07:00
|
|
|
|
|
|
|
#if HAS_AVFF
|
|
|
|
@synthesize captureSession;
|
|
|
|
@synthesize prevLayer;
|
|
|
|
#endif
|
|
|
|
@synthesize result, delegate, soundToPlay;
|
2010-05-30 04:40:09 -07:00
|
|
|
@synthesize overlayView;
|
2011-03-16 01:25:50 -07:00
|
|
|
@synthesize oneDMode, showCancel, isStatusBarHidden;
|
2010-06-15 15:01:04 -07:00
|
|
|
@synthesize readers;
|
2010-06-06 05:42:31 -07:00
|
|
|
|
|
|
|
|
2010-05-30 04:40:09 -07:00
|
|
|
- (id)initWithDelegate:(id<ZXingDelegate>)scanDelegate showCancel:(BOOL)shouldShowCancel OneDMode:(BOOL)shouldUseoOneDMode {
|
2010-12-22 09:33:00 -08:00
|
|
|
self = [super init];
|
|
|
|
if (self) {
|
2010-05-29 04:31:41 -07:00
|
|
|
[self setDelegate:scanDelegate];
|
2010-05-30 04:40:09 -07:00
|
|
|
self.oneDMode = shouldUseoOneDMode;
|
|
|
|
self.showCancel = shouldShowCancel;
|
2010-05-29 04:31:41 -07:00
|
|
|
self.wantsFullScreenLayout = YES;
|
2010-06-06 05:42:31 -07:00
|
|
|
beepSound = -1;
|
2010-08-17 14:18:56 -07:00
|
|
|
decoding = NO;
|
2010-06-06 05:42:31 -07:00
|
|
|
OverlayView *theOverLayView = [[OverlayView alloc] initWithFrame:[UIScreen mainScreen].bounds
|
|
|
|
cancelEnabled:showCancel
|
|
|
|
oneDMode:oneDMode];
|
|
|
|
[theOverLayView setDelegate:self];
|
|
|
|
self.overlayView = theOverLayView;
|
|
|
|
[theOverLayView release];
|
2010-05-29 04:31:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
2010-05-12 11:13:27 -07:00
|
|
|
}
|
|
|
|
|
2010-05-13 09:05:02 -07:00
|
|
|
- (void)dealloc {
|
2011-01-05 17:46:15 -08:00
|
|
|
if (beepSound != (SystemSoundID)-1) {
|
2010-05-29 04:31:41 -07:00
|
|
|
AudioServicesDisposeSystemSoundID(beepSound);
|
|
|
|
}
|
2010-08-17 14:18:56 -07:00
|
|
|
|
|
|
|
[self stopCapture];
|
|
|
|
|
|
|
|
[soundToPlay release];
|
2010-05-30 04:40:09 -07:00
|
|
|
[overlayView release];
|
2010-06-15 15:01:04 -07:00
|
|
|
[readers release];
|
2010-05-29 04:31:41 -07:00
|
|
|
[super dealloc];
|
2010-05-13 09:05:02 -07:00
|
|
|
}
|
|
|
|
|
2010-05-12 11:13:27 -07:00
|
|
|
- (void)cancelled {
|
2010-08-17 14:18:56 -07:00
|
|
|
[self stopCapture];
|
2011-03-16 01:25:50 -07:00
|
|
|
if (!self.isStatusBarHidden) {
|
|
|
|
[[UIApplication sharedApplication] setStatusBarHidden:NO];
|
|
|
|
}
|
|
|
|
|
2010-06-19 09:57:05 -07:00
|
|
|
wasCancelled = YES;
|
2010-05-29 04:31:41 -07:00
|
|
|
if (delegate != nil) {
|
2010-06-19 09:57:05 -07:00
|
|
|
[delegate zxingControllerDidCancel:self];
|
2010-05-29 04:31:41 -07:00
|
|
|
}
|
2010-05-12 11:13:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)getPlatform {
|
2010-05-29 04:31:41 -07:00
|
|
|
size_t size;
|
|
|
|
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
|
|
|
|
char *machine = malloc(size);
|
|
|
|
sysctlbyname("hw.machine", machine, &size, NULL, 0);
|
|
|
|
NSString *platform = [NSString stringWithCString:machine encoding:NSASCIIStringEncoding];
|
|
|
|
free(machine);
|
|
|
|
return platform;
|
2010-05-12 11:13:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)fixedFocus {
|
2010-05-29 04:31:41 -07:00
|
|
|
NSString *platform = [self getPlatform];
|
|
|
|
if ([platform isEqualToString:@"iPhone1,1"] ||
|
2010-06-19 09:57:05 -07:00
|
|
|
[platform isEqualToString:@"iPhone1,2"]) return YES;
|
|
|
|
return NO;
|
2010-05-12 11:13:27 -07:00
|
|
|
}
|
|
|
|
|
2010-05-13 10:34:36 -07:00
|
|
|
- (void)viewWillAppear:(BOOL)animated {
|
2010-05-29 04:31:41 -07:00
|
|
|
[super viewWillAppear:animated];
|
2010-06-06 05:42:31 -07:00
|
|
|
self.wantsFullScreenLayout = YES;
|
2010-05-29 04:31:41 -07:00
|
|
|
if ([self soundToPlay] != nil) {
|
|
|
|
OSStatus error = AudioServicesCreateSystemSoundID((CFURLRef)[self soundToPlay], &beepSound);
|
|
|
|
if (error != kAudioServicesNoError) {
|
|
|
|
NSLog(@"Problem loading nearSound.caf");
|
|
|
|
}
|
|
|
|
}
|
2010-05-13 09:05:02 -07:00
|
|
|
}
|
|
|
|
|
2010-05-12 11:13:27 -07:00
|
|
|
- (void)viewDidAppear:(BOOL)animated {
|
2010-05-29 04:31:41 -07:00
|
|
|
[super viewDidAppear:animated];
|
2011-03-16 01:25:50 -07:00
|
|
|
self.isStatusBarHidden = [[UIApplication sharedApplication] isStatusBarHidden];
|
|
|
|
if (!isStatusBarHidden)
|
|
|
|
[[UIApplication sharedApplication] setStatusBarHidden:YES];
|
2010-08-17 14:18:56 -07:00
|
|
|
|
|
|
|
decoding = YES;
|
|
|
|
|
|
|
|
[self initCapture];
|
|
|
|
[self.view addSubview:overlayView];
|
2010-06-06 05:42:31 -07:00
|
|
|
|
2010-05-29 04:31:41 -07:00
|
|
|
[overlayView setPoints:nil];
|
2010-06-19 09:57:05 -07:00
|
|
|
wasCancelled = NO;
|
2010-06-06 05:42:31 -07:00
|
|
|
}
|
|
|
|
|
2010-08-17 14:18:56 -07:00
|
|
|
- (void)viewDidDisappear:(BOOL)animated {
|
|
|
|
[super viewDidDisappear:animated];
|
2011-03-16 01:25:50 -07:00
|
|
|
if (!isStatusBarHidden)
|
|
|
|
[[UIApplication sharedApplication] setStatusBarHidden:NO];
|
2010-08-17 14:18:56 -07:00
|
|
|
[self.overlayView removeFromSuperview];
|
|
|
|
[self stopCapture];
|
2010-05-12 11:13:27 -07:00
|
|
|
}
|
|
|
|
|
2010-05-18 18:52:38 -07:00
|
|
|
- (CGImageRef)CGImageRotated90:(CGImageRef)imgRef
|
|
|
|
{
|
2010-05-29 04:31:41 -07:00
|
|
|
CGFloat angleInRadians = -90 * (M_PI / 180);
|
|
|
|
CGFloat width = CGImageGetWidth(imgRef);
|
|
|
|
CGFloat height = CGImageGetHeight(imgRef);
|
|
|
|
|
|
|
|
CGRect imgRect = CGRectMake(0, 0, width, height);
|
|
|
|
CGAffineTransform transform = CGAffineTransformMakeRotation(angleInRadians);
|
|
|
|
CGRect rotatedRect = CGRectApplyAffineTransform(imgRect, transform);
|
|
|
|
|
|
|
|
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
|
|
|
CGContextRef bmContext = CGBitmapContextCreate(NULL,
|
|
|
|
rotatedRect.size.width,
|
|
|
|
rotatedRect.size.height,
|
|
|
|
8,
|
|
|
|
0,
|
|
|
|
colorSpace,
|
|
|
|
kCGImageAlphaPremultipliedFirst);
|
|
|
|
CGContextSetAllowsAntialiasing(bmContext, FALSE);
|
|
|
|
CGContextSetInterpolationQuality(bmContext, kCGInterpolationNone);
|
|
|
|
CGColorSpaceRelease(colorSpace);
|
|
|
|
// CGContextTranslateCTM(bmContext,
|
|
|
|
// +(rotatedRect.size.width/2),
|
|
|
|
// +(rotatedRect.size.height/2));
|
|
|
|
CGContextScaleCTM(bmContext, rotatedRect.size.width/rotatedRect.size.height, 1.0);
|
|
|
|
CGContextTranslateCTM(bmContext, 0.0, rotatedRect.size.height);
|
|
|
|
CGContextRotateCTM(bmContext, angleInRadians);
|
|
|
|
// CGContextTranslateCTM(bmContext,
|
|
|
|
// -(rotatedRect.size.width/2),
|
|
|
|
// -(rotatedRect.size.height/2));
|
|
|
|
CGContextDrawImage(bmContext, CGRectMake(0, 0,
|
|
|
|
rotatedRect.size.width,
|
|
|
|
rotatedRect.size.height),
|
|
|
|
imgRef);
|
|
|
|
|
|
|
|
CGImageRef rotatedImage = CGBitmapContextCreateImage(bmContext);
|
|
|
|
CFRelease(bmContext);
|
|
|
|
[(id)rotatedImage autorelease];
|
|
|
|
|
|
|
|
return rotatedImage;
|
2010-05-18 18:52:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (CGImageRef)CGImageRotated180:(CGImageRef)imgRef
|
|
|
|
{
|
2010-05-29 04:31:41 -07:00
|
|
|
CGFloat angleInRadians = M_PI;
|
|
|
|
CGFloat width = CGImageGetWidth(imgRef);
|
|
|
|
CGFloat height = CGImageGetHeight(imgRef);
|
|
|
|
|
|
|
|
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
|
|
|
CGContextRef bmContext = CGBitmapContextCreate(NULL,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
8,
|
|
|
|
0,
|
|
|
|
colorSpace,
|
|
|
|
kCGImageAlphaPremultipliedFirst);
|
|
|
|
CGContextSetAllowsAntialiasing(bmContext, FALSE);
|
|
|
|
CGContextSetInterpolationQuality(bmContext, kCGInterpolationNone);
|
|
|
|
CGColorSpaceRelease(colorSpace);
|
|
|
|
CGContextTranslateCTM(bmContext,
|
|
|
|
+(width/2),
|
|
|
|
+(height/2));
|
|
|
|
CGContextRotateCTM(bmContext, angleInRadians);
|
|
|
|
CGContextTranslateCTM(bmContext,
|
|
|
|
-(width/2),
|
|
|
|
-(height/2));
|
|
|
|
CGContextDrawImage(bmContext, CGRectMake(0, 0, width, height), imgRef);
|
|
|
|
|
|
|
|
CGImageRef rotatedImage = CGBitmapContextCreateImage(bmContext);
|
|
|
|
CFRelease(bmContext);
|
|
|
|
[(id)rotatedImage autorelease];
|
|
|
|
|
|
|
|
return rotatedImage;
|
2010-05-18 18:52:38 -07:00
|
|
|
}
|
|
|
|
|
2010-05-12 11:13:27 -07:00
|
|
|
// DecoderDelegate methods
|
|
|
|
|
|
|
|
- (void)decoder:(Decoder *)decoder willDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset{
|
2010-05-13 10:34:36 -07:00
|
|
|
#ifdef DEBUG
|
2010-05-29 04:31:41 -07:00
|
|
|
NSLog(@"DecoderViewController MessageWhileDecodingWithDimensions: Decoding image (%.0fx%.0f) ...", image.size.width, image.size.height);
|
2010-05-13 10:34:36 -07:00
|
|
|
#endif
|
2010-05-12 11:13:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)decoder:(Decoder *)decoder
|
|
|
|
decodingImage:(UIImage *)image
|
2010-08-17 14:18:56 -07:00
|
|
|
usingSubset:(UIImage *)subset {
|
2010-05-12 11:13:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)presentResultForString:(NSString *)resultString {
|
2010-05-29 04:31:41 -07:00
|
|
|
self.result = [ResultParser parsedResultForString:resultString];
|
2011-01-05 17:46:15 -08:00
|
|
|
if (beepSound != (SystemSoundID)-1) {
|
2010-05-29 04:31:41 -07:00
|
|
|
AudioServicesPlaySystemSound(beepSound);
|
|
|
|
}
|
2010-05-12 11:13:27 -07:00
|
|
|
#ifdef DEBUG
|
2010-05-29 04:31:41 -07:00
|
|
|
NSLog(@"result string = %@", resultString);
|
2010-05-12 11:13:27 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-01-27 08:45:25 -08:00
|
|
|
- (void)presentResultPoints:(NSArray *)resultPoints
|
2010-05-12 11:13:27 -07:00
|
|
|
forImage:(UIImage *)image
|
|
|
|
usingSubset:(UIImage *)subset {
|
2010-05-29 04:31:41 -07:00
|
|
|
// simply add the points to the image view
|
2011-01-27 08:45:25 -08:00
|
|
|
NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithArray:resultPoints];
|
|
|
|
[overlayView setPoints:mutableArray];
|
|
|
|
[mutableArray release];
|
2010-05-12 11:13:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)decoder:(Decoder *)decoder didDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset withResult:(TwoDDecoderResult *)twoDResult {
|
2010-05-29 04:31:41 -07:00
|
|
|
[self presentResultForString:[twoDResult text]];
|
|
|
|
[self presentResultPoints:[twoDResult points] forImage:image usingSubset:subset];
|
|
|
|
// now, in a selector, call the delegate to give this overlay time to show the points
|
2011-03-16 08:57:43 -07:00
|
|
|
[self performSelector:@selector(notifyDelegate:) withObject:[[twoDResult text] copy] afterDelay:0.0];
|
2010-05-29 04:31:41 -07:00
|
|
|
decoder.delegate = nil;
|
2010-05-13 07:10:18 -07:00
|
|
|
}
|
|
|
|
|
2011-03-16 08:57:43 -07:00
|
|
|
- (void)notifyDelegate:(id)text {
|
|
|
|
if (!isStatusBarHidden) [[UIApplication sharedApplication] setStatusBarHidden:NO];
|
|
|
|
[delegate zxingController:self didScanResult:text];
|
2010-08-17 14:18:56 -07:00
|
|
|
[text release];
|
2010-05-12 11:13:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)decoder:(Decoder *)decoder failedToDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset reason:(NSString *)reason {
|
2010-05-29 04:31:41 -07:00
|
|
|
decoder.delegate = nil;
|
|
|
|
[overlayView setPoints:nil];
|
2010-08-17 14:18:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)decoder:(Decoder *)decoder foundPossibleResultPoint:(CGPoint)point {
|
|
|
|
[overlayView setPoint:point];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
- (void)stopPreview:(NSNotification*)notification {
|
|
|
|
// NSLog(@"stop preview");
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)notification:(NSNotification*)notification {
|
|
|
|
// NSLog(@"notification %@", notification.name);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2011-03-16 01:11:20 -07:00
|
|
|
#pragma mark -
|
|
|
|
#pragma mark AVFoundation
|
|
|
|
|
2010-08-17 14:18:56 -07:00
|
|
|
- (void)initCapture {
|
|
|
|
#if HAS_AVFF
|
|
|
|
AVCaptureDeviceInput *captureInput =
|
|
|
|
[AVCaptureDeviceInput deviceInputWithDevice:
|
|
|
|
[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]
|
|
|
|
error:nil];
|
|
|
|
AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init];
|
|
|
|
captureOutput.alwaysDiscardsLateVideoFrames = YES;
|
|
|
|
[captureOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
|
|
|
|
NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
|
|
|
|
NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
|
|
|
|
NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];
|
|
|
|
[captureOutput setVideoSettings:videoSettings];
|
|
|
|
self.captureSession = [[AVCaptureSession alloc] init];
|
|
|
|
[self.captureSession release];
|
|
|
|
self.captureSession.sessionPreset = AVCaptureSessionPresetMedium; // 480x360 on a 4
|
|
|
|
|
|
|
|
[self.captureSession addInput:captureInput];
|
|
|
|
[self.captureSession addOutput:captureOutput];
|
|
|
|
|
|
|
|
[captureOutput release];
|
|
|
|
|
|
|
|
/*
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
addObserver:self
|
|
|
|
selector:@selector(stopPreview:)
|
|
|
|
name:AVCaptureSessionDidStopRunningNotification
|
|
|
|
object:self.captureSession];
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
addObserver:self
|
|
|
|
selector:@selector(notification:)
|
|
|
|
name:AVCaptureSessionDidStopRunningNotification
|
|
|
|
object:self.captureSession];
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
addObserver:self
|
|
|
|
selector:@selector(notification:)
|
|
|
|
name:AVCaptureSessionRuntimeErrorNotification
|
|
|
|
object:self.captureSession];
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
addObserver:self
|
|
|
|
selector:@selector(notification:)
|
|
|
|
name:AVCaptureSessionDidStartRunningNotification
|
|
|
|
object:self.captureSession];
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
addObserver:self
|
|
|
|
selector:@selector(notification:)
|
|
|
|
name:AVCaptureSessionWasInterruptedNotification
|
|
|
|
object:self.captureSession];
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
addObserver:self
|
|
|
|
selector:@selector(notification:)
|
|
|
|
name:AVCaptureSessionInterruptionEndedNotification
|
|
|
|
object:self.captureSession];
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!self.prevLayer) {
|
|
|
|
self.prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
|
2010-05-29 04:31:41 -07:00
|
|
|
}
|
2010-08-17 14:18:56 -07:00
|
|
|
// NSLog(@"prev %p %@", self.prevLayer, self.prevLayer);
|
|
|
|
self.prevLayer.frame = self.view.bounds;
|
|
|
|
self.prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
|
|
|
|
[self.view.layer addSublayer: self.prevLayer];
|
2010-09-09 08:10:50 -07:00
|
|
|
|
|
|
|
[self.captureSession startRunning];
|
2010-08-17 14:18:56 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if HAS_AVFF
|
|
|
|
- (void)captureOutput:(AVCaptureOutput *)captureOutput
|
|
|
|
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
|
|
|
|
fromConnection:(AVCaptureConnection *)connection
|
|
|
|
{
|
|
|
|
if (!decoding) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
|
|
|
|
/*Lock the image buffer*/
|
|
|
|
CVPixelBufferLockBaseAddress(imageBuffer,0);
|
|
|
|
/*Get information about the image*/
|
|
|
|
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
|
|
|
|
size_t width = CVPixelBufferGetWidth(imageBuffer);
|
|
|
|
size_t height = CVPixelBufferGetHeight(imageBuffer);
|
|
|
|
|
|
|
|
uint8_t* baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);
|
|
|
|
void* free_me = 0;
|
|
|
|
if (true) { // iOS bug?
|
|
|
|
uint8_t* tmp = baseAddress;
|
|
|
|
int bytes = bytesPerRow*height;
|
|
|
|
free_me = baseAddress = (uint8_t*)malloc(bytes);
|
|
|
|
baseAddress[0] = 0xdb;
|
|
|
|
memcpy(baseAddress,tmp,bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
|
|
|
CGContextRef newContext =
|
|
|
|
CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace,
|
|
|
|
kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst);
|
|
|
|
|
|
|
|
CGImageRef capture = CGBitmapContextCreateImage(newContext);
|
|
|
|
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
|
|
|
|
free(free_me);
|
|
|
|
|
|
|
|
CGContextRelease(newContext);
|
|
|
|
CGColorSpaceRelease(colorSpace);
|
|
|
|
|
|
|
|
CGRect cropRect = [overlayView cropRect];
|
|
|
|
if (oneDMode) {
|
|
|
|
// let's just give the decoder a vertical band right above the red line
|
|
|
|
cropRect.origin.x = cropRect.origin.x + (cropRect.size.width / 2) - (ONE_D_BAND_HEIGHT + 1);
|
|
|
|
cropRect.size.width = ONE_D_BAND_HEIGHT;
|
|
|
|
// do a rotate
|
|
|
|
CGImageRef croppedImg = CGImageCreateWithImageInRect(capture, cropRect);
|
|
|
|
capture = [self CGImageRotated90:croppedImg];
|
|
|
|
capture = [self CGImageRotated180:capture];
|
|
|
|
// UIImageWriteToSavedPhotosAlbum([UIImage imageWithCGImage:capture], nil, nil, nil);
|
|
|
|
CGImageRelease(croppedImg);
|
|
|
|
cropRect.origin.x = 0.0;
|
|
|
|
cropRect.origin.y = 0.0;
|
|
|
|
cropRect.size.width = CGImageGetWidth(capture);
|
|
|
|
cropRect.size.height = CGImageGetHeight(capture);
|
|
|
|
}
|
|
|
|
|
2011-04-13 12:47:41 -07:00
|
|
|
// N.B.
|
|
|
|
// - Won't work if the overlay becomes uncentered ...
|
|
|
|
// - iOS always takes videos in landscape
|
|
|
|
// - images are always 4x3; device is not
|
|
|
|
// - iOS uses virtual pixels for non-image stuff
|
2010-08-17 14:18:56 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
float height = CGImageGetHeight(capture);
|
|
|
|
float width = CGImageGetWidth(capture);
|
|
|
|
|
|
|
|
CGRect screen = UIScreen.mainScreen.bounds;
|
|
|
|
float tmp = screen.size.width;
|
|
|
|
screen.size.width = screen.size.height;;
|
|
|
|
screen.size.height = tmp;
|
|
|
|
|
|
|
|
cropRect.origin.x = (width-cropRect.size.width)/2;
|
|
|
|
cropRect.origin.y = (height-cropRect.size.height)/2;
|
|
|
|
}
|
|
|
|
CGImageRef newImage = CGImageCreateWithImageInRect(capture, cropRect);
|
|
|
|
CGImageRelease(capture);
|
|
|
|
UIImage *scrn = [[UIImage alloc] initWithCGImage:newImage];
|
|
|
|
CGImageRelease(newImage);
|
|
|
|
Decoder *d = [[Decoder alloc] init];
|
|
|
|
d.readers = readers;
|
|
|
|
d.delegate = self;
|
2011-06-01 11:17:37 -07:00
|
|
|
cropRect.origin.x = 0.0;
|
2010-08-17 14:18:56 -07:00
|
|
|
cropRect.origin.y = 0.0;
|
|
|
|
decoding = [d decodeImage:scrn cropRect:cropRect] == YES ? NO : YES;
|
|
|
|
[d release];
|
|
|
|
[scrn release];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
- (void)stopCapture {
|
|
|
|
decoding = NO;
|
|
|
|
#if HAS_AVFF
|
|
|
|
[captureSession stopRunning];
|
|
|
|
AVCaptureInput* input = [captureSession.inputs objectAtIndex:0];
|
|
|
|
[captureSession removeInput:input];
|
|
|
|
AVCaptureVideoDataOutput* output = (AVCaptureVideoDataOutput*)[captureSession.outputs objectAtIndex:0];
|
|
|
|
[captureSession removeOutput:output];
|
|
|
|
[self.prevLayer removeFromSuperlayer];
|
|
|
|
|
|
|
|
/*
|
|
|
|
// heebee jeebees here ... is iOS still writing into the layer?
|
|
|
|
if (self.prevLayer) {
|
|
|
|
layer.session = nil;
|
|
|
|
AVCaptureVideoPreviewLayer* layer = prevLayer;
|
|
|
|
[self.prevLayer retain];
|
|
|
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 12000000000), dispatch_get_main_queue(), ^{
|
|
|
|
[layer release];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
self.prevLayer = nil;
|
|
|
|
self.captureSession = nil;
|
|
|
|
#endif
|
2010-05-12 11:13:27 -07:00
|
|
|
}
|
|
|
|
|
2011-03-16 01:11:20 -07:00
|
|
|
#pragma mark - Torch
|
|
|
|
|
|
|
|
- (void)setTorch:(BOOL)status {
|
2011-07-29 15:24:06 -07:00
|
|
|
#if HAS_AVFF
|
2011-03-16 01:11:20 -07:00
|
|
|
Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
|
|
|
|
if (captureDeviceClass != nil) {
|
|
|
|
|
2011-07-29 15:24:06 -07:00
|
|
|
AVCaptureDevice *device = [captureDeviceClass defaultDeviceWithMediaType:AVMediaTypeVideo];
|
2011-03-16 01:11:20 -07:00
|
|
|
|
|
|
|
[device lockForConfiguration:nil];
|
|
|
|
if ( [device hasTorch] ) {
|
|
|
|
if ( status ) {
|
|
|
|
[device setTorchMode:AVCaptureTorchModeOn];
|
|
|
|
} else {
|
2011-07-29 15:24:06 -07:00
|
|
|
[device setTorchMode:AVCaptureTorchModeOn];
|
2011-03-16 01:11:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
[device unlockForConfiguration];
|
|
|
|
|
|
|
|
}
|
2011-07-29 15:24:06 -07:00
|
|
|
#endif
|
2011-03-16 01:11:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)torchIsOn {
|
2011-07-29 15:24:06 -07:00
|
|
|
#if HAS_AVFF
|
2011-03-16 01:11:20 -07:00
|
|
|
Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
|
|
|
|
if (captureDeviceClass != nil) {
|
|
|
|
|
2011-07-29 15:24:06 -07:00
|
|
|
AVCaptureDevice *device = [captureDeviceClass defaultDeviceWithMediaType:AVMediaTypeVideo];
|
2011-03-16 01:11:20 -07:00
|
|
|
|
|
|
|
if ( [device hasTorch] ) {
|
|
|
|
return [device torchMode] == AVCaptureTorchModeOn;
|
|
|
|
}
|
|
|
|
[device unlockForConfiguration];
|
|
|
|
}
|
2011-07-29 15:24:06 -07:00
|
|
|
#endif
|
2011-03-16 01:11:20 -07:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2010-05-12 11:13:27 -07:00
|
|
|
@end
|