![iOS 掃一掃功能實(shí)現(xiàn)_第1頁(yè)](http://file2.renrendoc.com/fileroot_temp3/2021-11/1/b4cfbe51-dc4f-4b2d-b5b3-569f4a25e644/b4cfbe51-dc4f-4b2d-b5b3-569f4a25e6441.gif)
![iOS 掃一掃功能實(shí)現(xiàn)_第2頁(yè)](http://file2.renrendoc.com/fileroot_temp3/2021-11/1/b4cfbe51-dc4f-4b2d-b5b3-569f4a25e644/b4cfbe51-dc4f-4b2d-b5b3-569f4a25e6442.gif)
![iOS 掃一掃功能實(shí)現(xiàn)_第3頁(yè)](http://file2.renrendoc.com/fileroot_temp3/2021-11/1/b4cfbe51-dc4f-4b2d-b5b3-569f4a25e644/b4cfbe51-dc4f-4b2d-b5b3-569f4a25e6443.gif)
![iOS 掃一掃功能實(shí)現(xiàn)_第4頁(yè)](http://file2.renrendoc.com/fileroot_temp3/2021-11/1/b4cfbe51-dc4f-4b2d-b5b3-569f4a25e644/b4cfbe51-dc4f-4b2d-b5b3-569f4a25e6444.gif)
![iOS 掃一掃功能實(shí)現(xiàn)_第5頁(yè)](http://file2.renrendoc.com/fileroot_temp3/2021-11/1/b4cfbe51-dc4f-4b2d-b5b3-569f4a25e644/b4cfbe51-dc4f-4b2d-b5b3-569f4a25e6445.gif)
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、iOS 掃一掃功能實(shí)現(xiàn)下面貼上代碼: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 = UINavigationCon
2、troller alloc initWithRootViewController:ViewController alloc init; self.window.backgroundColor = UIColor whiteColor; self.window makeKeyAndVisible; return YES; ViewController:objc view plain copy 在CODE上查看代碼片派生到我的代碼片#import <UIKit/UIKit.h> interface ViewController : UIViewController end /* -分割
3、線- */ #import "ViewController.h" #import "HWScanViewController.h" implementation ViewController - (void)viewDidLoad super viewDidLoad; self.view.backgroundColor = UIColor whiteColor; /創(chuàng)建控件 self creatControl; - (void)creatControl /掃一掃按鈕 UIButton *scanBtn = UIButton alloc initWithF
4、rame:CGRectMake(100, 100, 100, 44); scanBtn.backgroundColor = UIColor orangeColor; scanBtn setTitle:"掃一掃" forState:UIControlStateNormal; scanBtn addTarget:self action:selector(scanBtnOnClick) forControlEvents:UIControlEventTouchUpInside; self.view addSubview:scanBtn; - (void)scanBtnOnClick
5、 HWScanViewController *vc = HWScanViewController alloc init; self.navigationController pushViewController:vc animated:YES; end HWScanViewController:objc view plain copy 在CODE上查看代碼片派生到我的代碼片#import <UIKit/UIKit.h> interface HWScanViewController : UIViewController end /* -分割線- */ #import "HW
6、ScanViewController.h" #import <AVFoundation/AVFoundation.h> #define KMainW UIScreen mainScreen.bounds.size.width #define KMainH UIScreen mainScreen.bounds.size.height interface HWScanViewController ()<AVCaptureMetadataOutputObjectsDelegate, UINavigationControllerDelegate, UIImagePicker
7、ControllerDelegate> property (nonatomic, strong) NSTimer *timer; property (nonatomic, strong) AVCaptureDevice *device; property (nonatomic, strong) AVCaptureSession *session; property (nonatomic, strong) AVCaptureVideoPreviewLayer *preview; property (nonatomic, weak) UIImageView *line; property (
8、nonatomic, assign) NSInteger distance; end implementation HWScanViewController - (void)viewDidLoad super viewDidLoad; /初始化信息 self initInfo; /創(chuàng)建控件 self creatControl; /設(shè)置參數(shù) self setupCamera; /添加定時(shí)器 self addTimer; - (void)viewWillDisappear:(BOOL)animated super viewWillDisappear:animated; self stopScann
9、ing; - (void)initInfo /背景色 self.view.backgroundColor = UIColor blackColor; /導(dǎo)航標(biāo)題 self.navigationItem.title = "二維碼/條形碼" /導(dǎo)航右側(cè)相冊(cè)按鈕 self.navigationItem.rightBarButtonItem = UIBarButtonItem alloc initWithTitle:"相冊(cè)" style:UIBarButtonItemStylePlain target:self action:selector(photoBtnO
10、nClick); - (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 - labelH) * 0.5; /遮蓋視圖 for (int i = 0; i < 4; i+) U
11、IView *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 blackColor colorWithAlphaComponent:0.5f; self.vi
12、ew 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 drawLineForImageView:line; scanView addSubview:line; self.line
13、 = 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; /掃描視圖四個(gè)角 for (int i = 0; i < 4; i+) CGFloat imgViewX = (scanW - cornerW) * (
14、i % 2); CGFloat imgViewY = (scanW - cornerW) * (i / 2); UIImageView *imgView = UIImageView alloc initWithFrame:CGRectMake(imgViewX, imgViewY, cornerW, cornerW); if (i = 0 | i = 1) imgView.transform = CGAffineTransformRotate(imgView.transform, M_PI_2 * i); else imgView.transform = CGAffineTransformRo
15、tate(imgView.transform, - M_PI_2 * (i - 1); self drawImageForImageView:imgView; scanView addSubview:imgView; /提示標(biāo)簽 UILabel *label = UILabel alloc initWithFrame:CGRectMake(0, CGRectGetMaxY(scanView.frame) + padding, KMainW, labelH); label.text = "將二維碼/條形碼放入框內(nèi),即可自動(dòng)掃描" label.font = UIFont sys
16、temFontOfSize:16.0f; label.textAlignment = NSTextAlignmentCenter; label.textColor = UIColor whiteColor; self.view addSubview:label; /選項(xiàng)欄 UIView *tabBarView = UIView alloc initWithFrame:CGRectMake(0, KMainH - tabBarH, KMainW, tabBarH); tabBarView.backgroundColor = UIColor blackColor colorWithAlphaCom
17、ponent:0.8f; self.view addSubview:tabBarView; /開啟照明按鈕 UIButton *lightBtn = UIButton alloc initWithFrame:CGRectMake(KMainW - 100, 0, 100, tabBarH); lightBtn.titleLabel.font = UIFont systemFontOfSize:16.0f; lightBtn setTitle:"開啟照明" forState:UIControlStateNormal; lightBtn setTitle:"關(guān)閉照明&
18、quot; forState:UIControlStateSelected; lightBtn addTarget:self action:selector(lightBtnOnClick:) forControlEvents:UIControlEventTouchUpInside; tabBarView addSubview:lightBtn; - (void)setupCamera dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), /初始化相機(jī)設(shè)備 _device = AVCaptur
19、eDevice defaultDeviceWithMediaType:AVMediaTypeVideo; /初始化輸入流 AVCaptureDeviceInput *input = AVCaptureDeviceInput deviceInputWithDevice:_device error:nil; /初始化輸出流 AVCaptureMetadataOutput *output = AVCaptureMetadataOutput alloc init; /設(shè)置代理,主線程刷新 output setMetadataObjectsDelegate:self queue:dispatch_get
20、_main_queue(); /初始化鏈接對(duì)象 _session = AVCaptureSession alloc init; /高質(zhì)量采集率 _session setSessionPreset:AVCaptureSessionPresetHigh; if (_session canAddInput:input) _session addInput:input; if (_session canAddOutput:output) _session addOutput:output; /條碼類型(二維碼/條形碼) output.metadataObjectTypes = NSArray arra
21、yWithObjects:AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code, AVMetadataObjectTypeQRCode, nil nil; /更新界面 dispatch_async(dispatch_get_main_queue(), _preview = AVCaptureVideoPreviewLayer layerWithSession:_session; _preview.videoGravity = AVLayerVideoGravity
22、ResizeAspectFill; _preview.frame = CGRectMake(0, 0, KMainW, KMainH); self.view.layer insertSublayer:_preview atIndex:0; _session startRunning; ); ); - (void)addTimer _distance = 0; self.timer = NSTimer scheduledTimerWithTimeInterval:0.01f target:self selector:selector(timerAction) userInfo:nil repea
23、ts:YES; NSRunLoop mainRunLoop addTimer:self.timer forMode:NSRunLoopCommonModes; - (void)timerAction if (_distance+ > KMainW * 0.65) _distance = 0; _line.frame = CGRectMake(0, _distance, KMainW * 0.65, 2); - (void)removeTimer _timer invalidate; _timer = nil; /照明按鈕點(diǎn)擊事件 - (void)lightBtnOnClick:(UIBu
24、tton *)btn /判斷是否有閃光燈 if (!_device hasTorch) self showAlertWithTitle:"當(dāng)前設(shè)備沒(méi)有閃光燈,無(wú)法開啟照明功能" message:nil sureHandler:nil cancelHandler:nil; return; btn.selected = !btn.selected; _device lockForConfiguration:nil; if (btn.selected) _device setTorchMode:AVCaptureTorchModeOn; else _device setTorch
25、Mode:AVCaptureTorchModeOff; _device unlockForConfiguration; /進(jìn)入相冊(cè) - (void)photoBtnOnClick if (UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary) UIImagePickerController *controller = UIImagePickerController alloc init; controller.sourceType = UIImagePickerCo
26、ntrollerSourceTypePhotoLibrary; controller.delegate = lf; self presentViewController:controller animated:YES completion:nil; else self showAlertWithTitle:"當(dāng)前設(shè)備不支持訪問(wèn)相冊(cè)" message:nil sureHandler:nil cancelHandler:nil; #pragma mark - AVCaptureMetadataOutputObjectsDelegate - (void)captureOutput
27、:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection /掃描完成 if (metadataObjects count > 0) /停止掃描 self stopScanning; /顯示結(jié)果 self showAlertWithTitle:"掃描結(jié)果" message:metadataObjects firstObject stringValue sureH
28、andler:nil cancelHandler:nil; - (void)stopScanning _session stopRunning; _session = nil; _preview removeFromSuperlayer; self removeTimer; #pragma mark - UIImagePickerControllrDelegate - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSStr
29、ing *,id> *)info picker dismissViewControllerAnimated:YES completion: /獲取相冊(cè)圖片 UIImage *image = infoUIImagePickerControllerOriginalImage; /識(shí)別圖片 CIDetector *detector = CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:CIDetectorAccuracy:CIDetectorAccuracyHigh; NSArray *features = d
30、etector featuresInImage:CIImage imageWithCGImage:image.CGImage; /識(shí)別結(jié)果 if (features.count > 0) self showAlertWithTitle:"掃描結(jié)果" message:features firstObject messageString sureHandler:nil cancelHandler:nil; else self showAlertWithTitle:"沒(méi)有識(shí)別到二維碼或條形碼" message:nil sureHandler:nil ca
31、ncelHandler:nil; ; /提示彈窗 - (void)showAlertWithTitle:(NSString *)title message:(NSString *)message sureHandler:(void ()()sureHandler cancelHandler:(void ()()cancelHandler UIAlertController *alertController = UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertContro
32、llerStyleAlert; UIAlertAction *sureAction = UIAlertAction actionWithTitle:"確認(rèn)" style:UIAlertActionStyleDefault handler:sureHandler; UIAlertAction *cancelAction = UIAlertAction actionWithTitle:"取消" style:UIAlertActionStyleCancel handler:cancelHandler; alertController addAction:sur
33、eAction; alertController addAction:cancelAction; self presentViewController:alertController animated:YES completion:nil; /繪制角圖片 - (void)drawImageForImageView:(UIImageView *)imageView UIGraphicsBeginImageContext(imageView.bounds.size); /獲取上下文 CGContextRef context = UIGraphicsGetCurrentContext(); /設(shè)置線
34、條寬度 CGContextSetLineWidth(context, 6.0f); /設(shè)置顏色 CGContextSetStrokeColorWithColor(context, UIColor greenColor CGColor); /路徑 CGContextBeginPath(context); /設(shè)置起點(diǎn)坐標(biāo) CGContextMoveToPoint(context, 0, imageView.bounds.size.height); /設(shè)置下一個(gè)點(diǎn)坐標(biāo) CGContextAddLineToPoint(context, 0, 0); CGContextAddLineToPoint(context, imageView.bounds.size.width, 0); /渲染,連接起點(diǎn)和下一個(gè)坐標(biāo)點(diǎn) CGContextStrokePath(context); imageView.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); /繪制線圖片 - (void)drawLineForI
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年中子、電子及Γ輻照裝置合作協(xié)議書
- 2025年機(jī)載設(shè)備綜合測(cè)試臺(tái)合作協(xié)議書
- 2025年石材翻新護(hù)理用品合作協(xié)議書
- 建筑力學(xué)期末考試B卷試題及答案
- 2025年個(gè)人貨物運(yùn)輸協(xié)議模板(2篇)
- 2025年個(gè)人房屋設(shè)計(jì)裝修合同(4篇)
- 2025年五年級(jí)體育教師工作總結(jié)(5篇)
- 2025年儀器銷售合同標(biāo)準(zhǔn)版本(4篇)
- 2025年五年級(jí)語(yǔ)文備課組長(zhǎng)工作總結(jié)范文(二篇)
- 2025年二手車車輛轉(zhuǎn)讓合同簡(jiǎn)單版(2篇)
- DB43-T 2142-2021學(xué)校食堂建設(shè)與食品安全管理規(guī)范
- 宏觀利率篇:債券市場(chǎng)研究分析框架
- 橋梁頂升移位改造技術(shù)規(guī)范
- 六年級(jí)語(yǔ)文(上冊(cè))選擇題集錦
- 介紹人提成方案
- 天津在津居住情況承諾書
- PHOTOSHOP教案 學(xué)習(xí)資料
- 初中數(shù)學(xué)教學(xué)“教-學(xué)-評(píng)”一體化研究
- 2012年安徽高考理綜試卷及答案-文檔
- 《游戲界面設(shè)計(jì)專題實(shí)踐》課件-知識(shí)點(diǎn)5:圖標(biāo)繪制準(zhǔn)備與繪制步驟
- 自動(dòng)扶梯安裝過(guò)程記錄
評(píng)論
0/150
提交評(píng)論