ios二维码

二维码的一些基本的用法

一.首先是通过输入字符串显示二维码(如果输入网址扫描后可直接进入网页)


上面是简单页面

.m文件中写的是

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textfield;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIButton *Button;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 15px; font-family: FangSong; color: rgb(76, 191, 87);">/*</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 15px; font-family: FangSong; color: rgb(76, 191, 87);"> 返回参数字符串对应的二维码图像,是通过将字符串传递给滤镜,滤镜直接转换生成二维码图片</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 15px; font-family: FangSong; color: rgb(76, 191, 87);"> *</p>- (IBAction)click_Btn:(UIButton *)sender {
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 15px; font-family: FangSong; color: rgb(76, 191, 87);"><span style="font-variant-ligatures: no-common-ligatures; color: #ffffff"> </span>//实例化一个滤镜</p>CIFilter * filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 15px; font-family: FangSong; color: rgb(76, 191, 87);"><span style="font-variant-ligatures: no-common-ligatures; color: #ffffff"> </span>//设置filter为默认值,因为之前如果使用过滤镜,输入就有可能保留,因此,在使用滤镜之前,最好设置恢复默认值</p>[filter setDefaults];
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 15px; font-family: FangSong; color: rgb(76, 191, 87);">//将传入的str转化为NSData</p>NSData * data = [_textfield.text dataUsingEncoding:NSUTF8StringEncoding];
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 15px; font-family: FangSong; color: rgb(76, 191, 87);">//将NSData传递给滤镜</p>[filter setValue:data forKey:@"inputMessage"];
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 15px; font-family: FangSong; color: rgb(76, 191, 87);">//由filter输出的图像</p>CIImage * outputImage = [filter outputImage];
_imageView.image = [UIImage imageWithCIImage:outputImage];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
程序使用的storyboard

二.通过扫描二维码返回字符串

.m文件

程序使用的是纯代码

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate>
{
    UITextView * _textView;
    UIView * _previewView;
    UIButton * _queryBtn;
    AVCaptureSession * _session;
    AVCaptureVideoPreviewLayer * _preview;
}

@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addQueryView];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)addQueryView{
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 20, 320, 100)];
    self.textView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.textView];
    
    self.previewView = [[UIView alloc] initWithFrame:CGRectMake(45, 145, 230, 260)];
    self.previewView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:self.previewView];
    
    
    self.queryBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    self.queryBtn.frame = CGRectMake(80, 450, 160, 30);
    [self.queryBtn setTitle:@"扫描二维码" forState:UIControlStateNormal];
    [self.queryBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.queryBtn];
}

- (void)clickBtn:(id)sender{
    //摄像头设备
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    //设置输入,因为模拟器没有摄像头,所以最好做一个判断
    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    if (error) {
        NSLog(@"没有摄像头!!!");
        return;
    }
    
    //设置输出
    AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
    //使用输出为主线程
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    //拍摄会话
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    //添加会话的输入输出
    [session addInput:input];
    [session addOutput:output];
    
    //设置输出格式
    [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
    //设置预览图层
    AVCaptureVideoPreviewLayer *preview = [AVCaptureVideoPreviewLayer layerWithSession:session];
    //设置预览图层的属性
    [preview setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    //设置预览图层的大小
    preview.frame = self.previewView.bounds;
    [self.previewView.layer insertSublayer:preview atIndex:0];

    self.preview = preview;
    //启动会话
    [session startRunning];
    self.session = session;
}

//代理方法
//此方法在识别到QRCode,并且完成转换后调用
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
    //让设备震动
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    
    [self.session stopRunning];
    [self.preview removeFromSuperlayer];
    
    //设置界面显示扫描结果
    if (metadataObjects.count > 0) {
        AVMetadataMachineReadableCodeObject *obj = metadataObjects[0];
        //如果是Url或是其他信息,就在这里进行对应的处理即可
        self.textView.text = obj.stringValue;
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

必须真机运行

可通过扫描显示字符串



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值