标签:
#import "ViewController.h"
#import <CoreImage/CoreImage.h>
#import <AVFoundation/AVFoundation.h>
#import "CustomView.h"
#define width self.view.frame.size.width
//#define height self.view.frame.size.height
@interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate>
{
BOOL torchIsOn;
}
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property(nonatomic,strong)AVCaptureSession * session;
@property(nonatomic,strong) AVCaptureVideoPreviewLayer * preview;
@end
@implementation ViewController
- (IBAction)button:(id)sender {
[self creamt];
}
- (void)viewDidLoad {
[super viewDidLoad];
// 1 实例化二维码滤镜
CIFilter * filter=[CIFilter filterWithName:@"CIQRCodeGenerator"];
// 2,恢复滤镜的默认属性
[filter setDefaults];
// 3将字符串转换为NSDate
NSData * data=[@"http://www.baidu.com" dataUsingEncoding:NSUTF8StringEncoding];
// 4通过kvo 设置滤镜传入data 将来滤镜就知道要通知传入的数据生成二维码
[filter setValue:data forKey:@"inputMessage"];
// 5生成二维码
CIImage * outputimage=[filter outputImage];
self.imageView.image=[UIImage imageWithCIImage:outputimage];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)creamt
{
// 1.实例话拍摄设备
AVCaptureDevice * device=[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// 2.设置输入设备
AVCaptureDeviceInput *input=[AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
// 3 设置元数输出
// 3.1实力话拍摄元数据输出
AVCaptureMetadataOutput* output=[[AVCaptureMetadataOutput alloc]init];
// 设置输出数据代理
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
// 4添加拍摄会话
// 4.1实力话拍摄会话
AVCaptureSession * session=[[AVCaptureSession alloc]init];
// 4.2添加会话输入
[session addInput:input];
// 4.3 添加会话输出
[session addOutput:output];
self.session=session;
// 4.4设置输出数据类型,需要将原数据输出添加到会话后,才能指定元数据类型,否则会报错
[output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
// 5
AVCaptureVideoPreviewLayer * preview=[[AVCaptureVideoPreviewLayer alloc]initWithSession:_session];
preview.videoGravity =AVLayerVideoGravityResizeAspectFill;
preview.frame=self.view.bounds;
// 5.2将图层插入当前视图
[self.view.layer insertSublayer:preview atIndex:100];
self.preview=preview;
// 6.启动会话
[_session startRunning];
UIView * line =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 280, 1)];
line.backgroundColor=[UIColor orangeColor];
CustomView * l=[[CustomView alloc]initWithFrame:CGRectMake(width/2-140, 100, 280, 280)];
[l addSubview:line];
[self runloop:line];
l.backgroundColor=[UIColor clearColor];
[self.view addSubview:l];
}
- (IBAction)open:(id)sender {
if (torchIsOn) {
[self turnTorchOn:NO];
}else{
[self turnTorchOn:YES];
}
}
-(void)runloop:(UIView*)line
{
[UIView animateWithDuration:3 animations:^{
line.frame=CGRectMake(0, 280, 280, 1);
}completion:^(BOOL finished) {
line.frame=CGRectMake(0, 0, 280, 1);
[self runloop:line];
}];
}
- (void) turnTorchOn:(bool) on {
Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
if (captureDeviceClass != nil) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch] && [device hasFlash]){
[device lockForConfiguration:nil];
if (on) {
[device setTorchMode:AVCaptureTorchModeOn];
[device setFlashMode:AVCaptureFlashModeOn];
torchIsOn = YES;
} else {
[device setTorchMode:AVCaptureTorchModeOff];
[device setFlashMode:AVCaptureFlashModeOff];
torchIsOn = NO;
}
[device unlockForConfiguration];
}
}
}
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
//会频繁的扫描,调用代理方法
//1 如果扫描完成,停止会话
[self.session stopRunning];
// 2 删除预览图层
[self.preview removeFromSuperlayer];
// 3设置界面显示扫描结果
AVMetadataMachineReadableCodeObject * obj =metadataObjects[0];
NSLog(@"%@",obj.stringValue);
}
@end
#import "CustomView.h"
#define width self.frame.size.width
#define height self.frame.size.height
@implementation CustomView
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
//获得上下文
CGContextRef context =UIGraphicsGetCurrentContext();
//设置线条样式
CGContextSetLineCap(context, kCGLineCapRound);
//设置线条粗细宽度
CGContextSetLineWidth(context, 1.0);
CGContextDrawPath(context, kCGPathStroke);
self.backgroundColor=[UIColor clearColor];
//设置颜色
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
//设置一个开始路径
CGContextBeginPath(context);
//起始点设置为(0,0):注意这是上下文对应区域中的相对坐标
CGContextMoveToPoint(context, 0, 0);
//设置下一个坐标点
CGContextAddLineToPoint(context, width, 0);
//设置下一个坐标点
CGContextAddLineToPoint(context, width, height);
//设置下一个坐标点
CGContextAddLineToPoint(context, 0, height);
//设置下一个坐标点
CGContextAddLineToPoint(context, 0, 0);
//连接上面定义的坐标点
CGContextStrokePath(context);
//获得上下文
CGContextRef con =UIGraphicsGetCurrentContext();
//设置线条样式
CGContextSetLineCap(con, kCGLineCapRound);
//设置线条粗细宽度
CGContextSetLineWidth(context, 3.0);
CGContextDrawPath(con, kCGPathStroke);
//设置颜色
CGContextSetRGBStrokeColor(con, 1.0, 1.0, 0.0, 1.0);
//设置一个开始路径
CGContextBeginPath(con);
//起始点设置为(0,0):注意这是上下文对应区域中的相对坐标
CGContextMoveToPoint(con, 0, 20);
//设置下一个坐标点
CGContextAddLineToPoint(con, 0, 0);
//设置下一个坐标点
CGContextAddLineToPoint(con, 20, 0);
CGContextStrokePath(con);
//获得上下文
CGContextRef con2 =UIGraphicsGetCurrentContext();
//设置线条样式
CGContextSetLineCap(con2, kCGLineCapRound);
//设置线条粗细宽度
CGContextSetLineWidth(context, 3.0);
CGContextDrawPath(con2, kCGPathStroke);
//设置颜色
CGContextSetRGBStrokeColor(con2, 1.0, 1.0, 0.0, 1.0);
//设置一个开始路径
CGContextBeginPath(con);
//起始点设置为(0,0):注意这是上下文对应区域中的相对坐标
CGContextMoveToPoint(con2, width-20, 0);
//设置下一个坐标点
CGContextAddLineToPoint(con2, width, 0);
//设置下一个坐标点
CGContextAddLineToPoint(con2, width, 20);
CGContextStrokePath(con2);
// 左下角
//获得上下文
CGContextRef con1 =UIGraphicsGetCurrentContext();
//设置线条样式
CGContextSetLineCap(con1, kCGLineCapRound);
//设置线条粗细宽度
CGContextSetLineWidth(con1, 3.0);
CGContextDrawPath(con1, kCGPathStroke);
//设置颜色
CGContextSetRGBStrokeColor(con1, 1.0, 0, 1.0, 1.0);
//设置一个开始路径
CGContextBeginPath(con1);
//起始点设置为(0,0):注意这是上下文对应区域中的相对坐标
CGContextMoveToPoint(con1, 0, height- 20);
//设置下一个坐标点
CGContextAddLineToPoint(con1, 0, height);
//设置下一个坐标点
CGContextAddLineToPoint(con1, 20, height);
CGContextStrokePath(con1);
// 右下角
//获得上下文
CGContextRef con3 =UIGraphicsGetCurrentContext();
//设置线条样式
CGContextSetLineCap(con3, kCGLineCapRound);
//设置线条粗细宽度
CGContextSetLineWidth(con3, 3.0);
CGContextDrawPath(con3, kCGPathStroke);
//设置颜色
CGContextSetRGBStrokeColor(con3, 1.0, 1.0, 0.0, 1.0);
//设置一个开始路径
CGContextBeginPath(con3);
//起始点设置为(0,0):注意这是上下文对应区域中的相对坐标
CGContextMoveToPoint(con3, width-20, height);
//设置下一个坐标点
CGContextAddLineToPoint(con3, width, height);
//设置下一个坐标点
CGContextAddLineToPoint(con3, width, height-20);
CGContextStrokePath(con3);
}
标签:
原文地址:http://www.cnblogs.com/xiezefeng/p/4630446.html