2016.1.18scan 二维码(仿照支付宝。微信)

2024-06-08 22:18

本文主要是介绍2016.1.18scan 二维码(仿照支付宝。微信),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1 生成二维码(原生方法生成二维码)

首先  要把二维码上的信息付给他 

 tempStr=self.textField.text;


其次 创建一个图片(在此之前需要导入#import "QRCodeGenerator.h"

 UIImage*tempImage=[QRCodeGenerator qrImageForString:tempStr imageSize:360 Topimg:image withColor:RandomColor];

    _outImageView.image=t 这是一个imageviewpImage;

2.扫描二维码

2.1添加手势长按识别

    UILongPressGestureRecognizer*longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(dealLongPress:)];

//把手势添加到图片上

    [_outImageView addGestureRecognizer:longPress];

    [self.view addSubview:_outImageView]


2.2长按手势方法并输出显示

-(void)dealLongPress:(UIGestureRecognizer*)gesture{

//

    if(gesture.state==UIGestureRecognizerStateBegan){

        _timer.fireDate=[NSDate distantFuture];

        UIImageView*tempImageView=(UIImageView*)gesture.view;

        if(tempImageView.image){

            //1. 初始化扫描仪,设置设别类型和识别质量

            CIDetector*detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{ CIDetectorAccuracy : CIDetectorAccuracyHigh }];

            //2. 扫描获取的特征组

            NSArray *features = [detector featuresInImage:[CIImage imageWithCGImage:tempImageView.image.CGImage]];

            //3. 获取扫描结果

            CIQRCodeFeature *feature = [features objectAtIndex:0];

            NSString *scannedResult = feature.messageString;

            UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"扫描结果" message:scannedResult delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

            [alertView show];

        }else {

            UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"扫描结果" message:@"您还没有生成二维码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

            [alertView show];

        }

        

        

    }else if (gesture.state==UIGestureRecognizerStateEnded){

        

        //定时器  可不要

        _timer.fireDate=[NSDate distantPast];

    }


}

3 正常用相机扫描(导入 #import <AVFoundation/AVFoundation.h>

            #import "UIView+SDExtension.h"

创建session对象

@property (nonatomic, strong) AVCaptureSession *session;


{

    //获取摄像设备

    AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    //创建输入流

    AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];

    if (!input) return;

    //创建输出流

    AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutput alloc]init];

    //设置代理 在主线程里刷新

    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];

    //设置有效扫描区域

    CGRect scanCrop=[self getScanCrop:_scanWindow.bounds readerViewBounds:self.view.frame];

     output.rectOfInterest = scanCrop;

    //初始化链接对象

    _session = [[AVCaptureSession alloc]init];

    //高质量采集率

    [_session setSessionPreset:AVCaptureSessionPresetHigh];

    

    [_session addInput:input];

    [_session addOutput:output];

    //设置扫码支持的编码格式(如下设置条形码和二维码兼容)

    output.metadataObjectTypes=@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];

    

    AVCaptureVideoPreviewLayer * layer = [AVCaptureVideoPreviewLayer layerWithSession:_session];

    layer.videoGravity=AVLayerVideoGravityResizeAspectFill;

    layer.frame=self.view.layer.bounds;

    [self.view.layer insertSublayer:layer atIndex:0];

    //开始捕获

    [_session startRunning];



4.选择一张图片然后 在扫描

#pragma mark-> 我的相册

-(void)myAlbum{

    NSLog(@"我的相册");

    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){

        //1.初始化相册拾取器

        UIImagePickerController *controller = [[UIImagePickerController alloc] init];

        //2.设置代理

        controller.delegate = self;

        //3.设置资源:

        /**

         UIImagePickerControllerSourceTypePhotoLibrary,相册

         UIImagePickerControllerSourceTypeCamera,相机

         UIImagePickerControllerSourceTypeSavedPhotosAlbum,照片库

         */

        controller.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

        //4.随便给他一个转场动画

        controller.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;

        [self presentViewController:controller animated:YES completion:NULL];

        

    }else{

        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"设备不支持访问相册,请在设置->隐私->照片中进行设置!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

        [alert show];

    }

    

}


#pragma mark-> imagePickerController delegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

    //1.获取选择的图片

    UIImage *image = info[UIImagePickerControllerOriginalImage];

    //2.初始化一个监测器

    CIDetector*detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{ CIDetectorAccuracy : CIDetectorAccuracyHigh }];

    [picker dismissViewControllerAnimated:YES completion:^{

        //监测到的结果数组

        NSArray *features = [detector featuresInImage:[CIImage imageWithCGImage:image.CGImage]];

        if (features.count >=1) {

            /**结果对象 */

            CIQRCodeFeature *feature = [features objectAtIndex:0];

            NSString *scannedResult = feature.messageString;

            UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"扫描结果" message:scannedResult delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

            [alertView show];

        }

        else{

            UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"该图片没有包含一个二维码!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

            [alertView show];   

        }

    }];

}


5 打开闪光灯

#pragma mark-> 闪光灯

-(void)openFlash:(UIButton*)button{

    NSLog(@"闪光灯");

    button.selected = !button.selected;

    if (button.selected) {

        [self turnTorchOn:YES];

    }

    else{

        [self turnTorchOn:NO];

    }

    

}


#pragma mark-> 开关闪光灯

- (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];

                

            } else {

                [device setTorchMode:AVCaptureTorchModeOff];

                [device setFlashMode:AVCaptureFlashModeOff];

            }

            [device unlockForConfiguration];

        }

    }

}




这篇关于2016.1.18scan 二维码(仿照支付宝。微信)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1043461

相关文章

java向微信服务号发送消息的完整步骤实例

《java向微信服务号发送消息的完整步骤实例》:本文主要介绍java向微信服务号发送消息的相关资料,包括申请测试号获取appID/appsecret、关注公众号获取openID、配置消息模板及代码... 目录步骤1. 申请测试系统2. 公众号账号信息3. 关注测试号二维码4. 消息模板接口5. Java测试

Python基于微信OCR引擎实现高效图片文字识别

《Python基于微信OCR引擎实现高效图片文字识别》这篇文章主要为大家详细介绍了一款基于微信OCR引擎的图片文字识别桌面应用开发全过程,可以实现从图片拖拽识别到文字提取,感兴趣的小伙伴可以跟随小编一... 目录一、项目概述1.1 开发背景1.2 技术选型1.3 核心优势二、功能详解2.1 核心功能模块2.

如何基于Python开发一个微信自动化工具

《如何基于Python开发一个微信自动化工具》在当今数字化办公场景中,自动化工具已成为提升工作效率的利器,本文将深入剖析一个基于Python的微信自动化工具开发全过程,有需要的小伙伴可以了解下... 目录概述功能全景1. 核心功能模块2. 特色功能效果展示1. 主界面概览2. 定时任务配置3. 操作日志演示

Redis迷你版微信抢红包实战

《Redis迷你版微信抢红包实战》本文主要介绍了Redis迷你版微信抢红包实战... 目录1 思路分析1.1hCckRX 流程1.2 注意点①拆红包:二倍均值算法②发红包:list③抢红包&记录:hset2 代码实现2.1 拆红包splitRedPacket2.2 发红包sendRedPacket2.3 抢

SpringBoot后端实现小程序微信登录功能实现

《SpringBoot后端实现小程序微信登录功能实现》微信小程序登录是开发者通过微信提供的身份验证机制,获取用户唯一标识(openid)和会话密钥(session_key)的过程,这篇文章给大家介绍S... 目录SpringBoot实现微信小程序登录简介SpringBoot后端实现微信登录SpringBoo

SpringBoot实现二维码生成的详细步骤与完整代码

《SpringBoot实现二维码生成的详细步骤与完整代码》如今,二维码的应用场景非常广泛,从支付到信息分享,二维码都扮演着重要角色,SpringBoot是一个非常流行的Java基于Spring框架的微... 目录一、环境搭建二、创建 Spring Boot 项目三、引入二维码生成依赖四、编写二维码生成代码五

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

SpringBoot实现微信小程序支付功能

《SpringBoot实现微信小程序支付功能》小程序支付功能已成为众多应用的核心需求之一,本文主要介绍了SpringBoot实现微信小程序支付功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录一、引言二、准备工作(一)微信支付商户平台配置(二)Spring Boot项目搭建(三)配置文件

微信公众号脚本-获取热搜自动新建草稿并发布文章

《微信公众号脚本-获取热搜自动新建草稿并发布文章》本来想写一个自动化发布微信公众号的小绿书的脚本,但是微信公众号官网没有小绿书的接口,那就写一个获取热搜微信普通文章的脚本吧,:本文主要介绍微信公众... 目录介绍思路前期准备环境要求获取接口token获取热搜获取热搜数据下载热搜图片给图片加上标题文字上传图片

如何用java对接微信小程序下单后的发货接口

《如何用java对接微信小程序下单后的发货接口》:本文主要介绍在微信小程序后台实现发货通知的步骤,包括获取Access_token、使用RestTemplate调用发货接口、处理AccessTok... 目录配置参数 调用代码获取Access_token调用发货的接口类注意点总结配置参数 首先需要获取Ac