ZXingObjC二维码扫描
#import "QRScanViewController.h" #import "AppDelegate.h" @interface QRScanViewController () { BOOL infoShowing; UIAlertView *alert; BOOL upToDown; int num; UIImageView *lineImageView; NSTimer *timer; } @end @implementation QRScanViewController @synthesize capture; @synthesize scanRectView; @synthesize backImageView; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization [self initViewFrame]; } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.capture = [[ZXCapture alloc] init]; self.capture.camera = self.capture.back; self.capture.focusMode = AVCaptureFocusModeContinuousAutoFocus; self.capture.rotation = 90.0f; self.capture.layer.frame = self.view.bounds; [self.view.layer addSublayer:self.capture.layer]; [self.view bringSubviewToFront:self.backImageView]; [self.view bringSubviewToFront:self.scanRectView]; lineImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"scan_line.png"]]; lineImageView.frame = CGRectMake(self.scanRectView.frame.origin.x, self.scanRectView.frame.origin.y, self.scanRectView.frame.size.width, 4); [self.view addSubview:lineImageView]; } - (void)lineAnimation { if (!([self isViewLoaded] && [self.view superview])) { return; } if (upToDown) { num++; float temp = 0.0; if (4*num > self.scanRectView.frame.size.height-4) { temp = self.scanRectView.frame.size.height+self.scanRectView.frame.origin.y; upToDown = NO; }else{ temp = self.scanRectView.frame.origin.y + 4*num; } lineImageView.frame = CGRectMake(self.scanRectView.frame.origin.x, temp, self.scanRectView.frame.size.width, 4); }else{ num--; float temp = 0.0; if (num <= 0) { temp = self.scanRectView.frame.origin.y; upToDown = YES; }else{ temp = self.scanRectView.frame.origin.y + 4*num; } lineImageView.frame = CGRectMake(self.scanRectView.frame.origin.x, temp, self.scanRectView.frame.size.width, 4); } } - (void)viewDidAppear:(BOOL)animated { if (timer) { [timer invalidate]; timer = nil; } upToDown = YES; num = 0; lineImageView.frame = CGRectMake(self.scanRectView.frame.origin.x, self.scanRectView.frame.origin.y, self.scanRectView.frame.size.width, 4); timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(lineAnimation) userInfo:nil repeats:YES]; } - (void)stopTimer { if (timer) { [timer invalidate]; timer = nil; } } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.capture.delegate = self; self.capture.layer.frame = self.view.bounds; // CGAffineTransform captureSizeTransform = CGAffineTransformMakeScale(320 / self.view.frame.size.width, 480 / self.view.frame.size.height); // self.capture.scanRect = CGRectApplyAffineTransform(self.scanRectView.frame, captureSizeTransform); if (viewer.device == DEVICE_IPAD || viewer.device == DEVICE_IPAD3) { [self showaCapture]; }else{ self.capture.rotation = 90.0f; CGAffineTransform transform = CGAffineTransformMakeRotation(0); [self.capture setTransform:transform]; CGRect f = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height); self.view.layer.frame=f; self.capture.layer.frame = f; } } - (void)showaCapture { CGAffineTransform transform; if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft) { self.capture.rotation = 180.0f; transform = CGAffineTransformMakeRotation(M_PI/2); } else if (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) { self.capture.rotation = 0.0f; transform = CGAffineTransformMakeRotation(-M_PI/2); } else if (self.interfaceOrientation == UIInterfaceOrientationPortrait) { self.capture.rotation = 90.0f; transform = CGAffineTransformMakeRotation(0); } else if (self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) { self.capture.rotation = 270.0f; transform = CGAffineTransformMakeRotation(M_PI); } [self.capture setTransform:transform]; CGRect f = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height); self.view.layer.frame=f; self.capture.layer.frame = f; } #pragma mark - ZXCaptureDelegate Methods //- (void)captureCameraIsReady:(ZXCapture *)capture //{ // //} // //- (void)captureSize:(ZXCapture *)capture width:(NSNumber *)width height:(NSNumber *)height //{ // //} - (void)captureResult:(ZXCapture *)capture result:(ZXResult *)result { if (!result) { return; } NSLog(@"结果是什么= %@",result.text); NSString *formatString = [self barcodeFormatToString:result.barcodeFormat]; if (![formatString isEqualToString:@"QR Code"]) { return; } NSString *display = [NSString stringWithFormat:@"Scanned!\n\nFormat: %@\n\nContents:\n%@", formatString, result.text]; NSLog(@"二维码是%@",display); // [decodedLabel performSelectorOnMainThread:@selector(setText:) withObject:display waitUntilDone:YES]; AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); if (infoShowing) { return; } if (alert) { [alert release]; alert = nil; } alert = [[UIAlertView alloc] initWithTitle:@"信息" message:result.text delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; alert.delegate = self; [alert show]; infoShowing = YES; } #pragma mark - Private Methods - (NSString *)barcodeFormatToString:(ZXBarcodeFormat)format { switch (format) { case kBarcodeFormatAztec: return @"Aztec"; case kBarcodeFormatCodabar: return @"CODABAR"; case kBarcodeFormatCode39: return @"Code 39"; case kBarcodeFormatCode93: return @"Code 93"; case kBarcodeFormatCode128: return @"Code 128"; case kBarcodeFormatDataMatrix: return @"Data Matrix"; case kBarcodeFormatEan8: return @"EAN-8"; case kBarcodeFormatEan13: return @"EAN-13"; case kBarcodeFormatITF: return @"ITF"; case kBarcodeFormatPDF417: return @"PDF417"; case kBarcodeFormatQRCode: return @"QR Code"; case kBarcodeFormatRSS14: return @"RSS 14"; case kBarcodeFormatRSSExpanded: return @"RSS Expanded"; case kBarcodeFormatUPCA: return @"UPCA"; case kBarcodeFormatUPCE: return @"UPCE"; case kBarcodeFormatUPCEANExtension: return @"UPC/EAN extension"; default: return @"Unknown"; } } //- (BOOL)shouldAutorotate //{ // return YES; //} // //- (BOOL)shouldAutomaticallyForwardRotationMethods //{ // return YES; //} // //- (NSUInteger)supportedInterfaceOrientations //{ // return UIInterfaceOrientationMaskAll; //} - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)rotated { [self setViewFrame]; } - (void)setViewFrame{ } - (void)initViewFrame{ } #pragma mark - UIAlertViewDelegate - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex != alert.cancelButtonIndex) { } infoShowing = NO; } - (void)dealloc { [super dealloc]; } /* #pragma mark - Navigation @end