OriginalScaner.mm 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // OriginalScaner.m
  3. // 未来家庭
  4. //
  5. // Created by Megatron on 11/6/15.
  6. // Copyright © 2015 Megatron. All rights reserved.
  7. //
  8. #import "OriginalScaner.h"
  9. #import <CoreMedia/CMSync.h>
  10. #import <AVFoundation/AVFoundation.h>
  11. #import "ScanAnimationView.h"
  12. #import "AppDelegate.h"
  13. #import "Header.h"
  14. @interface OriginalScaner ()<AVCaptureMetadataOutputObjectsDelegate>{
  15. ScanAnimationView *_animatoinView;
  16. }
  17. @property (nonatomic, strong) AVCaptureSession *session;
  18. @property (nonatomic, strong) AVCaptureVideoPreviewLayer *preview;
  19. @end
  20. #define TopHeight 124
  21. #define ScanWidth 220
  22. @implementation OriginalScaner
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. int cccolor = 20;
  26. float alpha = 0.7;
  27. UIView *viewTop = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, TopHeight)];
  28. viewTop.backgroundColor = [UIColor colorWithRed:cccolor/255.0 green:cccolor/255.0 blue:cccolor/255.0 alpha:alpha];
  29. [self.view addSubview:viewTop];
  30. UIView *viewLeft = [[UIView alloc] initWithFrame:CGRectMake(0, TopHeight, (ScreenWidth - ScanWidth) / 2.0, ScreenHeight - TopHeight)];
  31. viewLeft.backgroundColor = [UIColor colorWithRed:cccolor/255.0 green:cccolor/255.0 blue:cccolor/255.0 alpha:alpha];
  32. [self.view addSubview:viewLeft];
  33. UIView *viewRight = [[UIView alloc] initWithFrame:CGRectMake(ScanWidth +(ScreenWidth - ScanWidth) / 2.0, TopHeight, (ScreenWidth - ScanWidth) / 2.0, ScreenHeight - TopHeight)];
  34. viewRight.backgroundColor = [UIColor colorWithRed:cccolor/255.0 green:cccolor/255.0 blue:cccolor/255.0 alpha:alpha];
  35. [self.view addSubview:viewRight];
  36. UIView *viewBottom = [[UIView alloc] initWithFrame:CGRectMake((ScreenWidth - ScanWidth) / 2.0, TopHeight + ScanWidth, ScanWidth, ScreenHeight - TopHeight + ScanWidth)];
  37. viewBottom.backgroundColor = [UIColor colorWithRed:cccolor/255.0 green:cccolor/255.0 blue:cccolor/255.0 alpha:alpha];
  38. [self.view addSubview:viewBottom];
  39. UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, ScanWidth, 50)];
  40. lab.textAlignment = NSTextAlignmentCenter;
  41. lab.textColor = [UIColor whiteColor];
  42. lab.font = [UIFont systemFontOfSize:15];
  43. lab.text = TS("qr_scan_tips");
  44. lab.numberOfLines = 0;
  45. [lab sizeToFit];
  46. [viewBottom addSubview:lab];
  47. [self setNaviStyle];
  48. _animatoinView = [[ScanAnimationView alloc] initWithFrame:CGRectMake((ScreenWidth - ScanWidth) / 2.0, TopHeight, ScanWidth, ScanWidth)];
  49. [self.view addSubview:_animatoinView];
  50. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  51. [_animatoinView startScanAnimation];
  52. });
  53. self.view.backgroundColor=[UIColor colorWithRed:230.0/255 green:230.0/255 blue:230.0/255 alpha:1];
  54. self.edgesForExtendedLayout = UIRectEdgeNone;
  55. [self startReadingMachineReadableCodeObjects:@[AVMetadataObjectTypeQRCode] inView:self.view];
  56. // 注册通知
  57. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActive) name:@"MyAppWillBecomeInactive" object:nil];
  58. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidBecomeActive) name:@"MyAppWillBecomeActive" object:nil];
  59. }
  60. #pragma mark - 设置导航栏
  61. - (void)setNaviStyle {
  62. self.navigationItem.title = TS("scan_code");
  63. UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"new_back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(popViewController)];
  64. self.navigationItem.leftBarButtonItem = leftBtn;
  65. }
  66. #pragma mark - button event
  67. -(void)popViewController
  68. {
  69. [self stopReading];
  70. if (self.myScanerQuitBlock) {
  71. self.myScanerQuitBlock();
  72. }
  73. [self.navigationController popViewControllerAnimated:YES];
  74. }
  75. -(void)appWillResignActive
  76. {
  77. [self stopReading];
  78. }
  79. -(void)appDidBecomeActive
  80. {
  81. [_animatoinView startScanAnimation];
  82. [self startReadingMachineReadableCodeObjects:@[AVMetadataObjectTypeQRCode] inView:self.view];
  83. }
  84. // 开始扫描
  85. - (void)startReadingMachineReadableCodeObjects:(NSArray *)codeObjects inView:(UIView *)preview {
  86. // 摄像头设备
  87. AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  88. NSError *error = nil;
  89. // 设置输入口
  90. AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
  91. if (error || !input) {
  92. NSLog(@"error: %@", [error description]);
  93. return;
  94. }
  95. // 会话session, 把输入口加入会话
  96. self.session = [[AVCaptureSession alloc] init];
  97. [self.session addInput:input];// 将输入添加到session
  98. // 设置输出口,加入session, 设置输出口参数
  99. AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
  100. [output setRectOfInterest:CGRectMake(( TopHeight )/ ScreenHeight ,(( ScreenWidth - ScanWidth )/ 2 )/ ScreenWidth , 220 / ScreenHeight , 220 / ScreenWidth )];
  101. [self.session addOutput:output];
  102. [output setMetadataObjectTypes:codeObjects];
  103. [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; // 使用主线程队列,相应比较同步,使用其他队列,相应不同步,容易让用户产生不好的体验
  104. // 设置展示层(预览层)
  105. self.preview = [AVCaptureVideoPreviewLayer layerWithSession:self.session];// 设置预览层信息
  106. [self.preview setVideoGravity:AVLayerVideoGravityResizeAspectFill];
  107. self.preview.frame = [UIScreen mainScreen].bounds;
  108. [preview.layer insertSublayer:self.preview atIndex:0]; //添加至视图
  109. [self beginReading];
  110. }
  111. // 启动session
  112. - (void)beginReading
  113. {
  114. [self.session startRunning];
  115. }
  116. // 关闭session
  117. - (void)stopReading
  118. {
  119. [self.session stopRunning];
  120. [self.preview removeFromSuperlayer];
  121. }
  122. // 识别到二维码 并解析转换为字符串
  123. #pragma mark -
  124. - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
  125. {
  126. [self stopReading];
  127. AVMetadataObject *metadata = [metadataObjects objectAtIndex:0];
  128. NSString *codeStr= nil;
  129. if ([metadata respondsToSelector:@selector(stringValue)]) {
  130. codeStr = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
  131. }
  132. if (self.myScanerFinishedBlock) {
  133. self.myScanerFinishedBlock(codeStr);
  134. }
  135. [self.navigationController popViewControllerAnimated:YES];
  136. }
  137. @end