本来用的ZBar开源库实现的扫描二维码,可是貌似不支持arm64了,也没有在更新。
如今不用适配ios7下面。而iOS新增系统API已支持扫码,參考老外的一篇博客做了个demo。须要的能够參考下
參考博客:http://www.appcoda.com/qr-code-ios-programming-tutorial/
#import <AVFoundation/AVFoundation.h> @interface QRCodeReadController : BaseViewController<AVCaptureMetadataOutputObjectsDelegate> @property (weak, nonatomic) IBOutlet UIView *viewPreview; @end在xib上加一个viewPreview,用来扫码时动态显示获取到的摄像头的内容
@interface QRCodeReadController () { NSInteger maxY; NSInteger minY; NSTimer *timer; UIImageView *line; } @property (nonatomic) BOOL isReading; @property (nonatomic, strong) AVCaptureSession *captureSession; @property (nonatomic, strong) AVCaptureVideoPreviewLayer *videoPreviewLayer; -(BOOL)startReading; -(void)stopReading; @end @implementation QRCodeReadController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. _isReading = NO; if ([self startReading]) { maxY = 280; minY = 2; line =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 280, 10)]; // 0 -200 [line setImage:[UIImage imageNamed:@"e0"]]; [_viewPreview addSubview:line]; timer = [NSTimer scheduledTimerWithTimeInterval:1.0/40 target:self selector:@selector(move) userInfo:nil repeats:YES]; }; } /* * * AVCaptureMetadataOutput object. This class in combination with the AVCaptureMetadataOutputObjectsDelegate protocol will manage to intercept any metadata found in the input device (meaning data in a QR code captured by our camera) and translate it to a human readable format. */ - (BOOL)startReading { NSError *error; AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error]; if (!input) { NSLog(@"%@", [error localizedDescription]); return NO; } _captureSession = [[AVCaptureSession alloc] init]; [_captureSession addInput:input]; AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init]; [_captureSession addOutput:captureMetadataOutput]; dispatch_queue_t dispatchQueue; dispatchQueue = dispatch_queue_create("myQueue", NULL); [captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue]; [captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeQRCode]]; //show to user what the camera of the device sees using a AVCaptureVideoPreviewLayer _videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession]; [_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill]; [_videoPreviewLayer setFrame:_viewPreview.layer.bounds]; [_viewPreview.layer addSublayer:_videoPreviewLayer]; [_captureSession startRunning]; return YES; } -(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{ if (metadataObjects != nil && [metadataObjects count] > 0) { AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0]; if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) { [self performSelectorOnMainThread:@selector(stopReading) withObject:nil waitUntilDone:NO]; NSLog(@"metadataObj string = %@",[metadataObj stringValue]); _isReading = NO; } } } -(void)stopReading{ [_captureSession stopRunning]; _captureSession = nil; [_videoPreviewLayer removeFromSuperlayer]; } // 扫描时,移动扫描线 -(void)move { NSLog(@"+++"); static BOOL flag = TRUE; // true down and false up if (flag) { if (line.frame.origin.y <maxY) { line.frame = CGRectMake( line.frame.origin.x, line.frame.origin.y +5, line.frame.size.width, line.frame.size.height ); }else{ flag = !flag; if (_isReading&&[timer isValid]) { [timer invalidate]; } } }else { if (line.frame.origin.y >minY) { line.frame = CGRectMake( line.frame.origin.x, line.frame.origin.y -5, line.frame.size.width, line.frame.size.height ); }else { flag = !flag; } } }
/*****************************************************************************************/
识别图片二维码
如今有从含二维码的图片直接出二维码中信息的需求,查相关资料发现。原生api在iOS8才支持(CIQRCodeFeature
)
见 http://stackoverflow.com/questions/27505420/is-it-possible-to-decode-qrcode-image-to-value
解决方式用的zxing的ZXQRcodeDecoder
http://stackoverflow.com/questions/15575554/zxingobjc-cant-decode-image-taken-from-uiimagepickercontroller
NSString *path = [[NSBundle mainBundle] pathForResource:@"code" ofType:@"png"]; UIImage *image = [UIImage imageWithContentsOfFile:path]; NSError *error = nil; ZXQRCodeReader *reader = [[ZXQRCodeReader alloc]init]; ZXLuminanceSource *source = [[ZXCGImageLuminanceSource alloc] initWithCGImage:[image CGImage]]; ZXHybridBinarizer *binazer = [ZXHybridBinarizer binarizerWithSource:source]; ZXBinaryBitmap *bitmap = [[ZXBinaryBitmap alloc]initWithBinarizer:binazer]; ZXResult *result = [reader decode:bitmap hints:nil error:&error]; if(result){ NSLog(@"%@",result); [[[UIAlertView alloc] initWithTitle:@"Success" message:result.text delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show]; } else { // Use error to determine why we didn't get a result, such as a barcode // not being found, an invalid checksum, or a format inconsistency. [[[UIAlertView alloc] initWithTitle:@"ERROR" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show]; }