From 7b02207537d35bfa1714bf8beafc921f717d100a Mon Sep 17 00:00:00 2001 From: 单军华 Date: Wed, 11 Jul 2018 10:47:42 +0800 Subject: [PATCH] 首次上传 --- screendisplay/Pods/HMQRCodeScanner/HMQRCodeScanner/QRCode/HMScannerViewController.m | 221 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 221 insertions(+), 0 deletions(-) diff --git a/screendisplay/Pods/HMQRCodeScanner/HMQRCodeScanner/QRCode/HMScannerViewController.m b/screendisplay/Pods/HMQRCodeScanner/HMQRCodeScanner/QRCode/HMScannerViewController.m new file mode 100755 index 0000000..ca74c70 --- /dev/null +++ b/screendisplay/Pods/HMQRCodeScanner/HMQRCodeScanner/QRCode/HMScannerViewController.m @@ -0,0 +1,221 @@ +// +// HMScannerViewController.m +// HMQRCodeScanner +// +// Created by ������ on 16/1/2. +// Copyright �� 2016��� itheima. All rights reserved. +// + +#import "HMScannerViewController.h" +#import "HMScanerCardViewController.h" +#import "HMScannerBorder.h" +#import "HMScannerMaskView.h" +#import "HMScanner.h" + +/// ������������ +#define kControlMargin 32.0 +/// ������������������������ +#define kImageMaxSize CGSizeMake(1000, 1000) + +@interface HMScannerViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate> +/// ��������������� +@property (nonatomic) NSString *cardName; +/// ������������ +@property (nonatomic) UIImage *avatar; +/// ������������ +@property (nonatomic, copy) void (^completionCallBack)(NSString *); +@end + +@implementation HMScannerViewController { + /// ��������� + HMScannerBorder *scannerBorder; + /// ��������� + HMScanner *scanner; + /// ������������ + UILabel *tipLabel; +} + +- (instancetype)initWithCardName:(NSString *)cardName avatar:(UIImage *)avatar completion:(void (^)(NSString *))completion { + self = [super init]; + if (self) { + self.cardName = cardName; + self.avatar = avatar; + self.completionCallBack = completion; + } + return self; +} + +- (void)viewDidLoad { + [super viewDidLoad]; + + [self prepareUI]; + + // ������������������ + __weak typeof(self) weakSelf = self; + scanner = [HMScanner scanerWithView:self.view scanFrame:scannerBorder.frame completion:^(NSString *stringValue) { + // ������������ + weakSelf.completionCallBack(stringValue); + + // ������ + [weakSelf clickCloseButton]; + }]; +} + +- (void)viewWillAppear:(BOOL)animated { + [super viewWillAppear:animated]; + + [scannerBorder startScannerAnimating]; + [scanner startScan]; +} + +- (void)viewWillDisappear:(BOOL)animated { + [super viewWillDisappear:animated]; + + [scannerBorder stopScannerAnimating]; + [scanner stopScan]; +} + +#pragma mark - ������������ +/// ������������������ +- (void)clickCloseButton { + [self dismissViewControllerAnimated:YES completion:nil]; +} + +/// ������������������ +- (void)clickAlbumButton { + + if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { + tipLabel.text = @"������������������"; + + return; + } + + UIImagePickerController *picker = [[UIImagePickerController alloc] init]; + + picker.view.backgroundColor = [UIColor whiteColor]; + picker.delegate = self; + + [self showDetailViewController:picker sender:nil]; +} + +/// ������������������ +- (void)clickCardButton { + HMScanerCardViewController *vc = [[HMScanerCardViewController alloc] initWithCardName:self.cardName avatar:self.avatar]; + + [self showViewController:vc sender:nil]; +} + +#pragma mark - UIImagePickerControllerDelegate +- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info { + + UIImage *image = [self resizeImage:info[UIImagePickerControllerOriginalImage]]; + + // ������������ + [HMScanner scaneImage:image completion:^(NSArray *values) { + + if (values.count > 0) { + self.completionCallBack(values.firstObject); + [self dismissViewControllerAnimated:NO completion:^{ + [self clickCloseButton]; + }]; + } else { + tipLabel.text = @"������������������������������������������������"; + + [self dismissViewControllerAnimated:YES completion:nil]; + } + }]; +} + +- (UIImage *)resizeImage:(UIImage *)image { + + if (image.size.width < kImageMaxSize.width && image.size.height < kImageMaxSize.height) { + return image; + } + + CGFloat xScale = kImageMaxSize.width / image.size.width; + CGFloat yScale = kImageMaxSize.height / image.size.height; + CGFloat scale = MIN(xScale, yScale); + CGSize size = CGSizeMake(image.size.width * scale, image.size.height * scale); + + UIGraphicsBeginImageContext(size); + + [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; + + UIImage *result = UIGraphicsGetImageFromCurrentImageContext(); + + UIGraphicsEndImageContext(); + + return result; +} + +#pragma mark - ������������ +- (void)prepareUI { + self.view.backgroundColor = [UIColor darkGrayColor]; + + [self prepareNavigationBar]; + [self prepareScanerBorder]; + [self prepareOtherControls]; +} + +/// ��������������������������������� +- (void)prepareOtherControls { + + // 1> ������������ + tipLabel = [[UILabel alloc] init]; + + tipLabel.text = @"������������/���������������������������������������"; + tipLabel.font = [UIFont systemFontOfSize:12]; + tipLabel.textColor = [UIColor whiteColor]; + tipLabel.textAlignment = NSTextAlignmentCenter; + + [tipLabel sizeToFit]; + tipLabel.center = CGPointMake(scannerBorder.center.x, CGRectGetMaxY(scannerBorder.frame) + kControlMargin); + + [self.view addSubview:tipLabel]; + + // 2> ������������ + UIButton *cardButton = [[UIButton alloc] init]; + + [cardButton setTitle:@"������������" forState:UIControlStateNormal]; + cardButton.titleLabel.font = [UIFont systemFontOfSize:15]; + [cardButton setTitleColor:self.navigationController.navigationBar.tintColor forState:UIControlStateNormal]; + + [cardButton sizeToFit]; + cardButton.center = CGPointMake(tipLabel.center.x, CGRectGetMaxY(tipLabel.frame) + kControlMargin); + + [self.view addSubview:cardButton]; + + [cardButton addTarget:self action:@selector(clickCardButton) forControlEvents:UIControlEventTouchUpInside]; +} + +/// ��������������� +- (void)prepareScanerBorder { + + CGFloat width = self.view.bounds.size.width - 80; + scannerBorder = [[HMScannerBorder alloc] initWithFrame:CGRectMake(0, 0, width, width)]; + + scannerBorder.center = self.view.center; + scannerBorder.tintColor = self.navigationController.navigationBar.tintColor; + + [self.view addSubview:scannerBorder]; + + HMScannerMaskView *maskView = [HMScannerMaskView maskViewWithFrame:self.view.bounds cropRect:scannerBorder.frame]; + [self.view insertSubview:maskView atIndex:0]; +} + +/// ��������������� +- (void)prepareNavigationBar { + // 1> ������������ + [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithWhite:0.1 alpha:1.0]]; + self.navigationController.navigationBar.translucent = YES; + self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init]; + + // 2> ������ + self.title = @"���������"; + + // 3> ������������ + self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"������" style:UIBarButtonItemStylePlain target:self action:@selector(clickCloseButton)]; + self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"������" style:UIBarButtonItemStylePlain target:self action:@selector(clickAlbumButton)]; +} + +@end -- Gitblit v1.8.0