Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions HMQRCodeScanner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
TargetAttributes = {
C2D6BCF61C377219000EC35D = {
CreatedOnToolsVersion = 7.2;
DevelopmentTeam = J9XHDK444E;
DevelopmentTeam = 59B2B36X98;
};
};
};
Expand All @@ -161,6 +161,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
Base,
);
Expand Down Expand Up @@ -317,7 +318,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = J9XHDK444E;
DEVELOPMENT_TEAM = 59B2B36X98;
INFOPLIST_FILE = HMQRCodeScanner/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand All @@ -330,7 +331,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = J9XHDK444E;
DEVELOPMENT_TEAM = 59B2B36X98;
INFOPLIST_FILE = HMQRCodeScanner/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
27 changes: 18 additions & 9 deletions HMQRCodeScanner/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9531" systemVersion="15D9c" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="gfc-eJ-nmv">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="gfc-eJ-nmv">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9529"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Title-->
Expand All @@ -14,24 +18,24 @@
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="2yz-MN-05V">
<rect key="frame" x="165" y="165" width="270" height="270"/>
<rect key="frame" x="52.5" y="198.5" width="270" height="270"/>
<constraints>
<constraint firstAttribute="height" constant="270" id="7nM-FP-HcW"/>
<constraint firstAttribute="width" constant="270" id="s3H-rC-2OV"/>
</constraints>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="扫描结果" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" preferredMaxLayoutWidth="300" translatesAutoresizingMaskIntoConstraints="NO" id="41T-fL-A4o">
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" misplaced="YES" text="扫描结果" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" preferredMaxLayoutWidth="300" translatesAutoresizingMaskIntoConstraints="NO" id="41T-fL-A4o">
<rect key="frame" x="276" y="450" width="48" height="14.5"/>
<fontDescription key="fontDescription" type="system" pointSize="12"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="41T-fL-A4o" firstAttribute="top" secondItem="2yz-MN-05V" secondAttribute="bottom" constant="15" id="3Lg-wB-mu8"/>
<constraint firstItem="41T-fL-A4o" firstAttribute="centerX" secondItem="2yz-MN-05V" secondAttribute="centerX" id="9Eo-dD-p3q"/>
Expand All @@ -40,6 +44,11 @@
</constraints>
</view>
<navigationItem key="navigationItem" title="Title" id="t2s-qQ-mEH">
<barButtonItem key="leftBarButtonItem" title="我的二维码" id="lq9-Qu-YwM">
<connections>
<action selector="myQRCodeAction:" destination="BYZ-38-t0r" id="zu0-nE-N5z"/>
</connections>
</barButtonItem>
<barButtonItem key="rightBarButtonItem" title="扫一扫" id="xlh-b1-wH2">
<connections>
<action selector="clickScanButton:" destination="BYZ-38-t0r" id="nfp-Sm-aAf"/>
Expand All @@ -61,7 +70,7 @@
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="gfc-eJ-nmv" sceneMemberID="viewController">
<toolbarItems/>
<navigationBar key="navigationBar" contentMode="scaleToFill" id="3Ny-bf-yiy">
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
<rect key="frame" x="0.0" y="20" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<nil name="viewControllers"/>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions HMQRCodeScanner/QRCode/HMScannerController.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import <UIKit/UIKit.h>
#import "HMScannerViewController.h"

/*!
扫描控制器
Expand Down Expand Up @@ -39,6 +40,7 @@
*/
@interface HMScannerController : UINavigationController

@property (nonatomic, strong) HMScannerViewController *scannerViewController;
/// 使用 `名片字符串` 实例化扫描导航控制器
///
/// @param cardName 名片字符串
Expand Down
5 changes: 2 additions & 3 deletions HMQRCodeScanner/QRCode/HMScannerController.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

#import "HMScannerController.h"
#import "HMScannerViewController.h"
#import "HMScanner.h"

@implementation HMScannerController
Expand All @@ -25,11 +24,11 @@ + (instancetype)scannerWithCardName:(NSString *)cardName avatar:(UIImage *)avata
- (instancetype)initWithCardName:(NSString *)cardName avatar:(UIImage *)avatar completion:(void (^)(NSString *))completion {
self = [super init];
if (self) {
HMScannerViewController *scanner = [[HMScannerViewController alloc] initWithCardName:cardName avatar:avatar completion:completion];
self.scannerViewController = [[HMScannerViewController alloc] initWithCardName:cardName avatar:avatar completion:completion];

[self setTitleColor:[UIColor whiteColor] tintColor:[UIColor greenColor]];

[self pushViewController:scanner animated:NO];
[self pushViewController:self.scannerViewController animated:NO];
}
return self;
}
Expand Down
8 changes: 8 additions & 0 deletions HMQRCodeScanner/QRCode/HMScannerViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@

#import <UIKit/UIKit.h>



/// 扫描控制器
@interface HMScannerViewController : UIViewController

/** 名片按钮是否隐藏 */
@property(nonatomic,assign) BOOL hideCardButton;

/** 手电按钮是否隐藏 */
@property(nonatomic,assign) BOOL hideLightButton;

/// 实例化扫描控制器
///
/// @param cardName 名片字符串
Expand Down
82 changes: 69 additions & 13 deletions HMQRCodeScanner/QRCode/HMScannerViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "HMScannerBorder.h"
#import "HMScannerMaskView.h"
#import "HMScanner.h"
#import <AVFoundation/AVFoundation.h>

/// 控件间距
#define kControlMargin 32.0
Expand All @@ -22,6 +23,9 @@ @interface HMScannerViewController () <UIImagePickerControllerDelegate, UINaviga
@property (nonatomic) NSString *cardName;
/// 头像图片
@property (nonatomic) UIImage *avatar;
@property (nonatomic, strong) UIButton *cardButton;
@property (nonatomic, strong) UIButton *flashLightButton;

/// 完成回调
@property (nonatomic, copy) void (^completionCallBack)(NSString *);
@end
Expand Down Expand Up @@ -75,6 +79,7 @@ - (void)viewWillDisappear:(BOOL)animated {
[scanner stopScan];
}


#pragma mark - 监听方法
/// 点击关闭按钮
- (void)clickCloseButton {
Expand Down Expand Up @@ -105,6 +110,11 @@ - (void)clickCardButton {
[self showViewController:vc sender:nil];
}

- (void)modifyFlashLight:(UIButton *)sender {
[sender setSelected:[self modifyFlashLight]];
}


#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {

Expand All @@ -126,6 +136,10 @@ - (void)imagePickerController:(UIImagePickerController *)picker didFinishPicking
}];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:nil];
}

- (UIImage *)resizeImage:(UIImage *)image {

if (image.size.width < kImageMaxSize.width && image.size.height < kImageMaxSize.height) {
Expand Down Expand Up @@ -174,18 +188,27 @@ - (void)prepareOtherControls {
[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];
self.cardButton = [[UIButton alloc] init];

[self.cardButton setTitle:@"我的名片" forState:UIControlStateNormal];
self.cardButton.titleLabel.font = [UIFont systemFontOfSize:15];
[self.cardButton setTitleColor:self.navigationController.navigationBar.tintColor forState:UIControlStateNormal];

[self.cardButton sizeToFit];
self.cardButton.center = CGPointMake(tipLabel.center.x, CGRectGetMaxY(tipLabel.frame) + kControlMargin);

[self.view addSubview:self.cardButton];
[self.cardButton addTarget:self action:@selector(clickCardButton) forControlEvents:UIControlEventTouchUpInside];

self.flashLightButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.flashLightButton setFrame:CGRectMake((scannerBorder.frame.size.width-40)/2, scannerBorder.frame.size.height-60, 40, 40)];
[self.flashLightButton setImage:[self imageWithName:@"flashlight-off" bundle:@"HMScanner"]
forState:UIControlStateNormal];
[self.flashLightButton setImage:[self imageWithName:@"flashlight-on" bundle:@"HMScanner"] forState:UIControlStateSelected];
[scannerBorder addSubview:self.flashLightButton];
[self.flashLightButton addTarget:self action:@selector(modifyFlashLight:) forControlEvents:UIControlEventTouchUpInside];
[self.cardButton setHidden:self.hideCardButton];
[self.flashLightButton setHidden:self.hideLightButton];
}

/// 准备扫描框
Expand All @@ -209,7 +232,6 @@ - (void)prepareNavigationBar {
[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 = @"扫一扫";

Expand All @@ -218,4 +240,38 @@ - (void)prepareNavigationBar {
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"相册" style:UIBarButtonItemStylePlain target:self action:@selector(clickAlbumButton)];
}

#pragma mark - 封装处理

- (UIImage *)imageWithName:(NSString *)imageName bundle:(NSString *)bundleName {
///https://github.com/20120608/QM_HMQRCodeScanner
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSURL *url = [bundle URLForResource:bundleName withExtension:@"bundle"];
NSBundle *imageBundle = [NSBundle bundleWithURL:url];
NSString *fileName = [NSString stringWithFormat:@"%@@2x", imageName];
NSString *path = [imageBundle pathForResource:fileName ofType:@"png"];
return [[UIImage imageWithContentsOfFile:path] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}

- (BOOL)modifyFlashLight {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//修改前必须先锁定
[device lockForConfiguration:nil];
//必须判定是否有闪光灯,否则如果没有闪光灯会崩溃
BOOL isopen = false;
if ([device hasFlash]) {
if (device.flashMode == AVCaptureFlashModeOff) {
device.flashMode = AVCaptureFlashModeOn;
device.torchMode = AVCaptureTorchModeOn;
isopen = true;
} else if (device.flashMode == AVCaptureFlashModeOn) {
device.flashMode = AVCaptureFlashModeOff;
device.torchMode = AVCaptureTorchModeOff;
isopen = false;
}
}
[device unlockForConfiguration];
return isopen;
}


@end
12 changes: 8 additions & 4 deletions HMQRCodeScanner/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ @interface ViewController ()
@end

@implementation ViewController
- (IBAction)myQRCodeAction:(UIBarButtonItem *)sender {
[self show:NO];
}

- (IBAction)clickScanButton:(id)sender {

[self show:YES];
}

- (void)show:(BOOL)show{
NSString *cardName = @"天涯刀哥 - 傅红雪";
UIImage *avatar = [UIImage imageNamed:@"avatar"];

HMScannerController *scanner = [HMScannerController scannerWithCardName:cardName avatar:avatar completion:^(NSString *stringValue) {

self.scanResultLabel.text = stringValue;
}];

scanner.scannerViewController.hideCardButton = show;
[scanner setTitleColor:[UIColor whiteColor] tintColor:[UIColor greenColor]];

[self showDetailViewController:scanner sender:nil];
}

Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

包含 UI 界面的轻量级二维码扫描及生成框架



![扫描二维码](https://raw.githubusercontent.com/sauteam/HMQRCodeScanner/master/intro.jpeg)

## 功能

* 提供一个导航控制器,扫描 `二维码 / 条形码`
* 能够生成指定 `字符串` + `avatar(可选)` 的二维码名片
* 能够识别相册图片中的二维码(iOS 64 位设备)
* 支持闪光灯

## 系统支持

Expand Down Expand Up @@ -84,6 +89,8 @@ UIImage *avatar = [UIImage imageNamed:@"avatar"];
}];
```



### Swift

* 导入框架
Expand Down Expand Up @@ -116,3 +123,8 @@ HMScannerController.cardImageWithCardName(cardName, avatar: avatar, scale: 0.2)
}
```



### Special Thanks

<https://github.com/20120608/QM_HMQRCodeScanner>
Binary file added intro.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.