1、iOS 扫一扫功能实现iOS 扫一扫功能实现下面贴上代码:AppDelegate.m:objc view plain copy 在CODE上查看代码片派生到我的代码片- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions self.window = UIWindow alloc initWithFrame:UIScreen mainScreen bounds; self.window.rootViewController = UINa
2、vigationController alloc initWithRootViewController:ViewController alloc init; self.window.backgroundColor = UIColor whiteColor; self.window makeKeyAndVisible; return YES; ViewController:objc view plain copy 在CODE上查看代码片派生到我的代码片#import interface ViewController : UIViewController end /* -分割线- */ #impo
3、rt ViewController.h #import HWScanViewController.h implementation ViewController - (void)viewDidLoad super viewDidLoad; self.view.backgroundColor = UIColor whiteColor; /创建控件 self creatControl; - (void)creatControl /扫一扫按钮 UIButton *scanBtn = UIButton alloc initWithFrame:CGRectMake(100, 100, 100, 44);
4、 scanBtn.backgroundColor = UIColor orangeColor; scanBtn setTitle:扫一扫 forState:UIControlStateNormal; scanBtn addTarget:self action:selector(scanBtnOnClick) forControlEvents:UIControlEventTouchUpInside; self.view addSubview:scanBtn; - (void)scanBtnOnClick HWScanViewController *vc = HWScanViewControlle
5、r alloc init; self.navigationController pushViewController:vc animated:YES; end HWScanViewController:objc view plain copy 在CODE上查看代码片派生到我的代码片#import interface HWScanViewController : UIViewController end /* -分割线- */ #import HWScanViewController.h #import #define KMainW UIScreen mainScreen.bounds.size
6、.width #define KMainH UIScreen mainScreen.bounds.size.height interface HWScanViewController () property (nonatomic, strong) NSTimer *timer; property (nonatomic, strong) AVCaptureDevice *device; property (nonatomic, strong) AVCaptureSession *session; property (nonatomic, strong) AVCaptureVideoPreview
7、Layer *preview; property (nonatomic, weak) UIImageView *line; property (nonatomic, assign) NSInteger distance; end implementation HWScanViewController - (void)viewDidLoad super viewDidLoad; /初始化信息 self initInfo; /创建控件 self creatControl; /设置参数 self setupCamera; /添加定时器 self addTimer; - (void)viewWillD
8、isappear:(BOOL)animated super viewWillDisappear:animated; self stopScanning; - (void)initInfo /背景色 self.view.backgroundColor = UIColor blackColor; /导航标题 self.navigationItem.title = 二维码/条形码; /导航右侧相册按钮 self.navigationItem.rightBarButtonItem = UIBarButtonItem alloc initWithTitle:相册 style:UIBarButtonIte
9、mStylePlain target:self action:selector(photoBtnOnClick); - (void)creatControl CGFloat scanW = KMainW * 0.65; CGFloat padding = 10.0f; CGFloat labelH = 20.0f; CGFloat tabBarH = 64.0f; CGFloat cornerW = 26.0f; CGFloat marginX = (KMainW - scanW) * 0.5; CGFloat marginY = (KMainH - scanW - padding - lab
10、elH) * 0.5; /遮盖视图 for (int i = 0; i 4; i+) UIView *cover = UIView alloc initWithFrame:CGRectMake(0, (marginY + scanW) * i, KMainW, marginY + (padding + labelH) * i); if (i = 2 | i = 3) cover.frame = CGRectMake(marginX + scanW) * (i - 2), marginY, marginX, scanW); cover.backgroundColor = UIColor blac
11、kColor colorWithAlphaComponent:0.5f; self.view addSubview:cover; /扫描视图 UIView *scanView = UIView alloc initWithFrame:CGRectMake(marginX, marginY, scanW, scanW); self.view addSubview:scanView; /扫描线 UIImageView *line = UIImageView alloc initWithFrame:CGRectMake(0, 0, scanW, 2); self drawLineForImageVi
12、ew:line; scanView addSubview:line; self.line = line; /边框 UIView *borderView = UIView alloc initWithFrame:CGRectMake(0, 0, scanW, scanW); borderView.layer.borderColor = UIColor whiteColor CGColor; borderView.layer.borderWidth = 1.0f; scanView addSubview:borderView; /扫描视图四个角 for (int i = 0; i KMainW *
13、 0.65) _distance = 0; _line.frame = CGRectMake(0, _distance, KMainW * 0.65, 2); - (void)removeTimer _timer invalidate; _timer = nil; /照明按钮点击事件 - (void)lightBtnOnClick:(UIButton *)btn /判断是否有闪光灯 if (!_device hasTorch) self showAlertWithTitle:当前设备没有闪光灯,无法开启照明功能 message:nil sureHandler:nil cancelHandler
14、:nil; return; btn.selected = !btn.selected; _device lockForConfiguration:nil; if (btn.selected) _device setTorchMode:AVCaptureTorchModeOn; else _device setTorchMode:AVCaptureTorchModeOff; _device unlockForConfiguration; /进入相册 - (void)photoBtnOnClick if (UIImagePickerController isSourceTypeAvailable:
15、UIImagePickerControllerSourceTypePhotoLibrary) UIImagePickerController *controller = UIImagePickerController alloc init; controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; controller.delegate = lf; self presentViewController:controller animated:YES completion:nil; else self showA
16、lertWithTitle:当前设备不支持访问相册 message:nil sureHandler:nil cancelHandler:nil; #pragma mark - AVCaptureMetadataOutputObjectsDelegate - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection /扫描完成 if (metadat
17、aObjects count 0) /停止扫描 self stopScanning; /显示结果 self showAlertWithTitle:扫描结果 message:metadataObjects firstObject stringValue sureHandler:nil cancelHandler:nil; - (void)stopScanning _session stopRunning; _session = nil; _preview removeFromSuperlayer; self removeTimer; #pragma mark - UIImagePickerCon
18、trollrDelegate - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info picker dismissViewControllerAnimated:YES completion: /获取相册图片 UIImage *image = infoUIImagePickerControllerOriginalImage; /识别图片 CIDetector *detector = CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:CIDetectorAccuracy:CIDetectorAccuracyHigh; NSArray *features = detector featuresInImage:CIImage imageWithCGImage:image.CGImage; /识别结果 if (features.cou
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1