国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

iOS模仿微信長(zhǎng)按識(shí)別二維碼的多種方式

2020-07-26 02:44:47
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

 參考:https://github.com/nglszs/BCQRcode

方式一:

#import <UIKit/UIKit.h>@interface ViewController : UIViewController@end**************#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {  [super viewDidLoad];  self.title = @"二維碼";  UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc]                initWithTitle:@"生成"                style:UIBarButtonItemStylePlain                target:self                action:@selector(backView)];  self.navigationItem.leftBarButtonItem = leftBtn;  UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]                 initWithTitle:@"掃描"                 style:UIBarButtonItemStylePlain                 target:self                 action:@selector(ScanView)];  self.navigationItem.rightBarButtonItem = rightBtn;  //長(zhǎng)按識(shí)別圖中的二維碼,類(lèi)似于微信里面的功能,前提是當(dāng)前頁(yè)面必須有二維碼  UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(readCode:)];  [self.view addGestureRecognizer:longPress];}- (void)readCode:(UILongPressGestureRecognizer *)pressSender {  if (pressSender.state == UIGestureRecognizerStateBegan) {    //截圖 再讀取    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, YES, 0);    CGContextRef context = UIGraphicsGetCurrentContext();    [self.view.layer renderInContext:context];    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();   //識(shí)別二維碼     CIImage *ciImage = [[CIImage alloc] initWithCGImage:image.CGImage options:nil];    CIContext *ciContext = [CIContext contextWithOptions:@{kCIContextUseSoftwareRenderer : @(YES)}]; // 軟件渲染    CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:ciContext options:@{CIDetectorAccuracy : CIDetectorAccuracyHigh}];// 二維碼識(shí)別    NSArray *features = [detector featuresInImage:ciImage];    for (CIQRCodeFeature *feature in features) {      NSLog(@"msg = %@",feature.messageString); // 打印二維碼中的信息      //對(duì)結(jié)果進(jìn)行處理      ResultViewController *resultVC = [[ResultViewController alloc] init];      resultVC.contentString = feature.messageString;      [self.navigationController pushViewController:resultVC animated:NO];    }  }}- (void)backView {    UIImageView *codeImageView = [[UIImageView alloc] initWithFrame:CGRectMake((BCWidth - 200)/2, 100, 200, 200)];    codeImageView.layer.borderColor = [UIColor orangeColor].CGColor;    codeImageView.layer.borderWidth = 1;    [self.view addSubview:codeImageView];  //有圖片的時(shí)候,也可以不設(shè)置圓角  [codeImageView creatCode:@"https://www.baidu.com" Image:[UIImage imageNamed:@"bg"] andImageCorner:4];  //沒(méi)有圖片的時(shí)候  // [codeImageView creatCode:@"這波可以" Image:nil andImageCorner:4];}- (void)ScanView {  [self.navigationController pushViewController:[ScanCodeViewController new] animated:YES];}- (void)didReceiveMemoryWarning {  [super didReceiveMemoryWarning];  // Dispose of any resources that can be recreated.}@end************生成二維碼#import <UIKit/UIKit.h>@interface UIImageView (CreatCode)/** 這里傳入二維碼的信息,image是加載二維碼上方的圖片,如果不要圖片直接codeImage為nil即可,后面是圖片的圓角 */- (void)creatCode:(NSString *)codeContent Image:(UIImage *)codeImage andImageCorner:(CGFloat)imageCorner;@end**************#import "UIImageView+CreatCode.h"#define ImageSize self.bounds.size.width@implementation UIImageView (CreatCode)- (void)creatCode:(NSString *)codeContent Image:(UIImage *)codeImage andImageCorner:(CGFloat)imageCorner {  //用CoreImage框架實(shí)現(xiàn)二維碼的生成,下面方法最好異步調(diào)用  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{    CIFilter *codeFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];    //每次調(diào)用都恢復(fù)其默認(rèn)屬性    [codeFilter setDefaults];    NSData *codeData = [codeContent dataUsingEncoding:NSUTF8StringEncoding];    //設(shè)置濾鏡數(shù)據(jù)    [codeFilter setValue:codeData forKey:@"inputMessage"];    //獲得濾鏡輸出的圖片    CIImage *outputImage = [codeFilter outputImage];    //這里的圖像必須經(jīng)過(guò)位圖轉(zhuǎn)換,不然會(huì)很模糊    UIImage *translateImage = [self creatUIImageFromCIImage:outputImage andSize:ImageSize];    //這里如果不想設(shè)置圓角,直接傳一個(gè)image就好了    UIImage *resultImage = [self setSuperImage:translateImage andSubImage:[self imageCornerRadius:imageCorner andImage:codeImage]];    dispatch_async(dispatch_get_main_queue(), ^{      self.image = resultImage;    });});}//這里的size我是用imageview的寬度來(lái)算的,你可以改為自己想要的size- (UIImage *)creatUIImageFromCIImage:(CIImage *)image andSize:(CGFloat)size {  //下面是創(chuàng)建bitmao沒(méi)什么好解釋的,不懂得自行百度或者參考官方文檔  CGRect extent = CGRectIntegral(image.extent);  CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));  size_t width = CGRectGetWidth(extent) * scale;  size_t height = CGRectGetHeight(extent) * scale;  CGColorSpaceRef colorRef = CGColorSpaceCreateDeviceGray();  CGContextRef contextRef = CGBitmapContextCreate(nil, width, height, 8, 0, colorRef, (CGBitmapInfo)kCGImageAlphaNone);  CIContext *context = [CIContext contextWithOptions:nil];  CGImageRef imageRef = [context createCGImage:image fromRect:extent];  CGContextSetInterpolationQuality(contextRef, kCGInterpolationNone);  CGContextScaleCTM(contextRef, scale, scale);  CGContextDrawImage(contextRef, extent, imageRef);  CGImageRef newImage = CGBitmapContextCreateImage(contextRef);  CGContextRelease(contextRef);  CGImageRelease(imageRef);  return [UIImage imageWithCGImage:newImage];}//這里將二維碼上方的圖片設(shè)置圓角并縮放- (UIImage *)imageCornerRadius:(CGFloat)cornerRadius andImage:(UIImage *)image {  //這里是將圖片進(jìn)行處理,frame不能太大,否則會(huì)擋住二維碼  CGRect frame = CGRectMake(0, 0, ImageSize/5, ImageSize/5);  UIGraphicsBeginImageContextWithOptions(frame.size, NO, 0);  [[UIBezierPath bezierPathWithRoundedRect:frame cornerRadius:cornerRadius] addClip];  [image drawInRect:frame];  UIImage *clipImage = UIGraphicsGetImageFromCurrentImageContext();  UIGraphicsEndImageContext();  return clipImage;}- (UIImage *)setSuperImage:(UIImage *)superImage andSubImage:(UIImage *)subImage {  //將兩張圖片繪制在一起  UIGraphicsBeginImageContextWithOptions(superImage.size, YES, 0);  [superImage drawInRect:CGRectMake(0, 0, superImage.size.width, superImage.size.height)];  [subImage drawInRect:CGRectMake((ImageSize - ImageSize/5)/2, (ImageSize - ImageSize/5)/2, subImage.size.width, subImage.size.height)];  UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();  UIGraphicsEndImageContext();  return resultImage;}@end***************掃描二維碼#import <UIKit/UIKit.h>#import <AVFoundation/AVFoundation.h>@interface ScanCodeViewController : UIViewController<AVCaptureMetadataOutputObjectsDelegate>{  AVCaptureSession * session;  AVCaptureMetadataOutput * output;  NSInteger lineNum;  BOOL upOrDown;  NSTimer *lineTimer;}@property (nonatomic, strong) UIImageView *lineImageView;@property (nonatomic, strong) UIImageView *backImageView;@end******************#import "ScanCodeViewController.h"@implementation ScanCodeViewController- (void)viewDidLoad {  [super viewDidLoad];  if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {        AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];        if (authStatus == AVAuthorizationStatusDenied || authStatus == AVAuthorizationStatusRestricted)        {          [[[UIAlertView alloc] initWithTitle:nil message:@"本應(yīng)用無(wú)訪問(wèn)相機(jī)的權(quán)限,如需訪問(wèn),可在設(shè)置中修改" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil] show];          return;        } else {          //打開(kāi)相機(jī)          AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];          //創(chuàng)建輸入流          AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];          //創(chuàng)建輸出流          output = [[AVCaptureMetadataOutput alloc]init];          //設(shè)置代理 在主線(xiàn)程里刷新          [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];          //設(shè)置掃描區(qū)域,這個(gè)需要仔細(xì)調(diào)整          [output setRectOfInterest:CGRectMake(64/BCHeight, (BCWidth - 320)/2/BCWidth, 320/BCHeight, 320/BCWidth)];          //初始化鏈接對(duì)象          session = [[AVCaptureSession alloc]init];          //高質(zhì)量采集率          [session setSessionPreset:AVCaptureSessionPresetHigh];          [session addInput:input];          [session addOutput:output];          //設(shè)置掃碼支持的編碼格式          output.metadataObjectTypes=@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];          AVCaptureVideoPreviewLayer * layer = [AVCaptureVideoPreviewLayer layerWithSession:session];          layer.videoGravity=AVLayerVideoGravityResizeAspectFill;          layer.frame=self.view.layer.bounds;          [self.view.layer addSublayer:layer];      }  }        [self _initView];}//里面所有的控件可以自己定制,這里只是簡(jiǎn)單的例子- (void)_initView {  //掃碼框  _backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, BCWidth, BCHeight - 64)];  _backImageView.image = [UIImage imageNamed:@"camera_bg"];  [self.view addSubview:_backImageView];  _lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(16, 15, BCWidth - 32, 1)];  _lineImageView.backgroundColor = [UIColor orangeColor];  [_backImageView addSubview:_lineImageView];  //各種參數(shù)設(shè)置  lineNum = 0;  upOrDown = NO;  lineTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(lineAnimation) userInfo:nil repeats:YES];}-(void)lineAnimation {  if (upOrDown == NO) {    lineNum ++;    _lineImageView.frame = CGRectMake(CGRectGetMinX(_lineImageView.frame), 15 + lineNum, BCWidth - 32, 1);    CGFloat tempHeight = CGRectGetHeight(_backImageView.frame) * 321/542;    NSInteger height = (NSInteger)tempHeight + 20;    if (lineNum == height) {      upOrDown = YES;    }  }  else {    lineNum --;    _lineImageView.frame = CGRectMake(CGRectGetMinX(_lineImageView.frame), 15 + lineNum, BCWidth - 32, 1);    if (lineNum == 0) {      upOrDown = NO;    }  }}#pragma mark AVCaptureMetadataOutputObjectsDelegate- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {  if ([metadataObjects count] > 0) {    [session stopRunning]; //停止掃碼    AVMetadataMachineReadableCodeObject *metadataObject = [metadataObjects firstObject];    ResultViewController *resultVC = [[ResultViewController alloc] init];    resultVC.contentString = metadataObject.stringValue;    [self.navigationController pushViewController:resultVC animated:NO];  }}- (void)viewWillAppear:(BOOL)animated {  [super viewWillAppear:animated];  [session startRunning];  [lineTimer setFireDate:[NSDate distantPast]];}- (void)viewWillDisappear:(BOOL)animated {  [super viewWillDisappear:animated];  [session stopRunning];  [lineTimer setFireDate:[NSDate distantFuture]];  if (![self.navigationController.viewControllers containsObject:self]) {//釋放timer    [lineTimer invalidate];    lineTimer = nil;  }}- (void)dealloc {  NSLog(@"已釋放");}@end*******吧識(shí)別的二維碼信息傳過(guò)來(lái)加載網(wǎng)頁(yè)#import <UIKit/UIKit.h>@interface ResultViewController : UIViewController@property(nonatomic, retain)NSString *contentString;@end********#import "ResultViewController.h"#import <WebKit/WebKit.h>@implementation ResultViewController- (void)viewDidLoad {  [super viewDidLoad];  //這個(gè)界面我只是簡(jiǎn)單的處理一下,可以自己定制,實(shí)際應(yīng)用中掃碼跳轉(zhuǎn)不可能就這兩種邏輯  if ([_contentString hasPrefix:@"http"]) {    WKWebView *showView = [[WKWebView alloc] initWithFrame:BCScreen];    NSURLRequest *codeRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:_contentString]];    [showView loadRequest:codeRequest];    [self.view addSubview:showView];  } else {    UILabel *showLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 80, 200, 50)];    showLabel.center = self.view.center;    showLabel.font = [UIFont boldSystemFontOfSize:16];    showLabel.text = [NSString stringWithFormat:@"掃描結(jié)果是---%@",_contentString];    showLabel.numberOfLines = 0;    [self.view addSubview:showLabel];  }}@end

方式二:識(shí)別網(wǎng)頁(yè)中的二維碼

iOS WebView中 長(zhǎng)按二維碼的識(shí)別

思路:

長(zhǎng)按webView 的過(guò)程中 截屏,再去解析是否有二維碼,但是有個(gè)缺點(diǎn) 就是 萬(wàn)一截了一個(gè) 一半的二維碼 那就無(wú)解了。

在webview中 注入獲取點(diǎn)擊圖片的JS 獲取圖片,再解析。缺點(diǎn):萬(wàn)一圖片過(guò)大 需要下載,勢(shì)必會(huì)影響用戶(hù)體驗(yàn)。

@interface CVWebViewController ()<UIGestureRecognizerDelegate>@property (weak, nonatomic) IBOutlet UIWebView *webView;@end@implementation CVWebViewController- (void)viewDidLoad{  [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://mp.weixin.qq.com/s?__biz=MzI2ODAzODAzMw==&mid=2650057120&idx=2&sn=c875f7d03ea3823e8dcb3dc4d0cff51d&scene=0#wechat_redirect"]]];  UILongPressGestureRecognizer *longPressed = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressed:)];  longPressed.delegate = self;  [self.webView addGestureRecognizer:longPressed];}- (void)longPressed:(UITapGestureRecognizer*)recognizer{  if (recognizer.state != UIGestureRecognizerStateBegan) {    return;  }  CGPoint touchPoint = [recognizer locationInView:self.webView];  NSString *js = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", touchPoint.x, touchPoint.y];  NSString *imageUrl = [self.webView stringByEvaluatingJavaScriptFromString:js];  if (imageUrl.length == 0) {    return;  }  NSLog(@"image url:%@",imageUrl);  NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];  UIImage *image = [UIImage imageWithData:data];  if (image) {    //......    //save image or Extract QR code  }}-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{  return YES;}

以上所述是小編給大家介紹的iOS模仿微信長(zhǎng)按識(shí)別二維碼的多種方式,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 永靖县| 黎川县| 新建县| 即墨市| 会东县| 黄石市| 景洪市| 章丘市| 温泉县| 福州市| 青田县| 黄浦区| 本溪市| 麦盖提县| 调兵山市| 迁西县| 怀安县| 屏山县| 东宁县| 雷山县| 洪湖市| 思南县| 邯郸县| 博兴县| 思茅市| 永和县| 楚雄市| 英山县| 永济市| 阿拉善左旗| 连江县| 唐海县| 柯坪县| 宁阳县| 上杭县| 专栏| 巩义市| 利川市| 阳信县| 内黄县| 高雄市|