二维码扫描:
#import "AVFViewController.h"
#import
staticconst char *kScanQRCodeQueueName ="ScanQRCodeQueue";
@interface AVFViewController () <AVCaptureMetadataOutputObjectsDelegate>
//扫描视图
@property (weak, nonatomic) IBOutletUIView *sanFrameView;
//开始扫描按钮
@property (weak, nonatomic) IBOutletUIButton *button;
//扫描结果显示
@property (weak, nonatomic) IBOutletUILabel *resultL;
@property (nonatomic)AVCaptureSession *captureSession;
@property (nonatomic)AVCaptureVideoPreviewLayer *videoPreviewLayer;
@property (nonatomic)BOOL lastResut;
@end
@implementation AVFViewController
- (void)viewDidLoad
{
[superviewDidLoad];
[_buttonsetTitle:@"开始"forState:UIControlStateNormal];
_lastResut = YES;
}
- (void)dealloc
{
[selfstopReading];
}
- (BOOL)startReading
{
[_buttonsetTitle:@"停止"forState:UIControlStateNormal];
// 获取 AVCaptureDevice 实例
NSError * error;
AVCaptureDevice *captureDevice = [AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo];
// 初始化输入流
AVCaptureDeviceInput *input = [AVCaptureDeviceInputdeviceInputWithDevice:captureDevice error:&error];
if (!input) {
NSLog(@"%@", [errorlocalizedDescription]);
return NO;
}
// 创建会话
_captureSession = [[AVCaptureSessionalloc] init];
// 添加输入流
[_captureSessionaddInput:input];
// 初始化输出流
AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutputalloc] init];
// 添加输出流
[_captureSessionaddOutput:captureMetadataOutput];
// 创建dispatch queue.
dispatch_queue_t dispatchQueue;
dispatchQueue =dispatch_queue_create(kScanQRCodeQueueName,NULL);
[captureMetadataOutputsetMetadataObjectsDelegate:selfqueue:dispatchQueue];
// 设置元数据类型 AVMetadataObjectTypeQRCode
[captureMetadataOutput setMetadataObjectTypes:[NSArrayarrayWithObject:AVMetadataObjectTypeQRCode]];
// 创建输出对象
_videoPreviewLayer = [[AVCaptureVideoPreviewLayeralloc] initWithSession:_captureSession];
[_videoPreviewLayersetVideoGravity:AVLayerVideoGravityResizeAspectFill];
[_videoPreviewLayersetFrame:_sanFrameView.layer.bounds];
[_sanFrameView.layeraddSublayer:_videoPreviewLayer];
// 开始会话
[_captureSessionstartRunning];
return YES;
}
- (void)stopReading
{
[_buttonsetTitle:@"开始"forState:UIControlStateNormal];
// 停止会话
[_captureSessionstopRunning];
_captureSession =nil;
}
- (void)reportScanResult:(NSString *)result
{
[selfstopReading];
if (!_lastResut) {
return;
}
_lastResut = NO;
self.resultL.text=result;
//以及处理了结果,下次扫描
_lastResut = YES;
}
#pragma AVCaptureMetadataOutputObjectsDelegate
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects
fromConnection:(AVCaptureConnection *)connection
{
if (metadataObjects != nil && [metadataObjects count] > 0) {
AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjectsobjectAtIndex:0];
NSString *result;
if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {
result = metadataObj.stringValue;
}else {
NSLog(@"不是二维码");
}
[selfperformSelectorOnMainThread:@selector(reportScanResult:)withObject:result waitUntilDone:NO];
}
}
- (IBAction)startScanner:(id)sender
{
UIButton *button = (UIButton *)sender;
if ([button.titleLabel.textisEqualToString:@"开始"]) {
[selfstartReading];
}else {
[selfstopReading];
}
}
#import "CreateViewController.h"
#import "QRCodeGenerator.h"
@interface CreateViewController ()
@property (weak, nonatomic) IBOutletUIImageView *outImageV;
@property (weak, nonatomic) IBOutletUITextField *contentTF;
@end
@implementation CreateViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
}
- (IBAction)createQRCImg:(id)sender {
UIImage * image=[QRCodeGeneratorqrImageForString:self.contentTF.textimageSize:200];
NSLog(@"尺寸:%@",NSStringFromCGSize(image.size));
self.outImageV.image=image;
}
生成:
#import "CreateViewController.h"
#import "QRCodeGenerator.h"
@interface CreateViewController ()
@property (weak, nonatomic) IBOutletUIImageView *outImageV;
@property (weak, nonatomic) IBOutletUITextField *contentTF;
@end
@implementation CreateViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
}
- (IBAction)createQRCImg:(id)sender {
UIImage * image=[QRCodeGeneratorqrImageForString:self.contentTF.textimageSize:200];
NSLog(@"尺寸:%@",NSStringFromCGSize(image.size));
self.outImageV.image=image;
}