Skip to content

Commit

Permalink
增加扫码 || 接下去写商品管理模块的人会用到
Browse files Browse the repository at this point in the history
  • Loading branch information
有马 committed Sep 3, 2015
1 parent f8bddc5 commit 0f2f1d9
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 0 deletions.
18 changes: 18 additions & 0 deletions YHB_Prj/YHB_Prj.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
5B54BA3F1B92F9C0002098A4 /* DJCheckCartSubmitRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = 5B54BA3E1B92F9C0002098A4 /* DJCheckCartSubmitRequest.m */; };
5B54BA421B9336C7002098A4 /* DJCheckCartCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 5B54BA411B9336C7002098A4 /* DJCheckCartCell.m */; };
5B54BA481B9348FD002098A4 /* DJProductCheckViewManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 5B54BA471B9348FD002098A4 /* DJProductCheckViewManager.m */; };
5BA5AA311B98297400329677 /* DJScanViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 5BA5AA301B98297400329677 /* DJScanViewController.m */; };
5BA5AA331B98299D00329677 /* AVFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5BA5AA321B98299D00329677 /* AVFoundation.framework */; };
AB2578DC1B8EFBDE0015039A /* GateDetailViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = AB2578DB1B8EFBDE0015039A /* GateDetailViewController.m */; };
AB2579341B8F02010015039A /* IQNSArray+Sort.m in Sources */ = {isa = PBXBuildFile; fileRef = AB2579121B8F02010015039A /* IQNSArray+Sort.m */; };
AB2579351B8F02010015039A /* IQUITextFieldView+Additions.m in Sources */ = {isa = PBXBuildFile; fileRef = AB2579141B8F02010015039A /* IQUITextFieldView+Additions.m */; };
Expand Down Expand Up @@ -257,6 +259,9 @@
5B54BA411B9336C7002098A4 /* DJCheckCartCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DJCheckCartCell.m; sourceTree = "<group>"; };
5B54BA461B9348FD002098A4 /* DJProductCheckViewManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DJProductCheckViewManager.h; sourceTree = "<group>"; };
5B54BA471B9348FD002098A4 /* DJProductCheckViewManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DJProductCheckViewManager.m; sourceTree = "<group>"; };
5BA5AA2F1B98297400329677 /* DJScanViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DJScanViewController.h; sourceTree = "<group>"; };
5BA5AA301B98297400329677 /* DJScanViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DJScanViewController.m; sourceTree = "<group>"; };
5BA5AA321B98299D00329677 /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = System/Library/Frameworks/AVFoundation.framework; sourceTree = SDKROOT; };
AB2578DA1B8EFBDE0015039A /* GateDetailViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GateDetailViewController.h; sourceTree = "<group>"; };
AB2578DB1B8EFBDE0015039A /* GateDetailViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GateDetailViewController.m; sourceTree = "<group>"; };
AB2579111B8F02010015039A /* IQNSArray+Sort.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "IQNSArray+Sort.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -600,6 +605,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
5BA5AA331B98299D00329677 /* AVFoundation.framework in Frameworks */,
FF5C87291B8318C500F79EC1 /* libsqlite3.dylib in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -726,6 +732,15 @@
name = Service;
sourceTree = "<group>";
};
5BA5AA2E1B98293600329677 /* Scan */ = {
isa = PBXGroup;
children = (
5BA5AA2F1B98297400329677 /* DJScanViewController.h */,
5BA5AA301B98297400329677 /* DJScanViewController.m */,
);
path = Scan;
sourceTree = "<group>";
};
AB2578D91B8EFBCA0015039A /* GateDetailVC */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -936,6 +951,7 @@
FF2A6BD11A0F3780001607AC = {
isa = PBXGroup;
children = (
5BA5AA321B98299D00329677 /* AVFoundation.framework */,
FF5C87281B8318C500F79EC1 /* libsqlite3.dylib */,
FF2A6BDC1A0F3780001607AC /* YHB_Prj */,
FF2A6BF01A0F3780001607AC /* YHB_PrjTests */,
Expand Down Expand Up @@ -1161,6 +1177,7 @@
FF2A6C8B1A0F3C58001607AC /* UIParty */ = {
isa = PBXGroup;
children = (
5BA5AA2E1B98293600329677 /* Scan */,
FF5CC7061B93F9B500F55713 /* Jicunchaxun */,
FF5CC6E31B93EE5300F55713 /* Rukushenpi */,
5B1F7F871B8F5D1100763059 /* ProductCheck */,
Expand Down Expand Up @@ -1792,6 +1809,7 @@
AB40EAC11B8C020700DE5ED7 /* SupplierViewController.m in Sources */,
FF2A6C731A0F3B7B001607AC /* SDWebImagePrefetcher.m in Sources */,
FFF3919A1B846F58007DC652 /* AFURLRequestSerialization.m in Sources */,
5BA5AA311B98297400329677 /* DJScanViewController.m in Sources */,
ABAACA0A1B95BE7600305080 /* SupplierMode.m in Sources */,
FF4DFD0B1B85CBFA00617660 /* StoreListView.m in Sources */,
5B54BA421B9336C7002098A4 /* DJCheckCartCell.m in Sources */,
Expand Down
8 changes: 8 additions & 0 deletions YHB_Prj/YHB_Prj/ProductCheck/DJCheckCartViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "DJCheckCartEngine.h"
#import "LoginManager.h"
#import "DJCheckCartCell.h"
#import "DJScanViewController.h"

#define StringValueWithNum(a) [NSString stringWithFormat:@"%ld",(NSInteger)a]

Expand All @@ -29,6 +30,13 @@ - (void)viewDidLoad {
[super viewDidLoad];
[self setExtraCellLineHidden:self.tableView];

[self.searchBar setNeedShowSearchVCHandler:^{
//TODO:跳转搜索框
} andShowScanVCHandler:^{
DJScanViewController *vc= [[DJScanViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
}];

}

- (void)viewWillAppear:(BOOL)animated{
Expand Down
34 changes: 34 additions & 0 deletions YHB_Prj/YHB_Prj/Scan/DJScanViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// DJScanViewController.h
// YHB_Prj
//
// Created by yato_kami on 15/9/3.
// Copyright (c) 2015年 striveliu. All rights reserved.
//

#import "BaseViewController.h"

@protocol DJScanDelegate<NSObject>

/**
* 扫描出信息的徽标
*
* @param message 扫描出的信息
*
*/
- (void)scanController:(UIViewController *)vc didScanedAndTransToMessage: (NSString *)message;

/**
* 用户点击了手工输入码
*/
- (void)needToInputNumberFromScanController:(UIViewController *)vc;

@end

@interface DJScanViewController : BaseViewController

@property (nonatomic, weak) id<DJScanDelegate> delegate;

- (BOOL)startReading;

@end
149 changes: 149 additions & 0 deletions YHB_Prj/YHB_Prj/Scan/DJScanViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
//
// DJScanViewController.m
// YHB_Prj
//
// Created by yato_kami on 15/9/3.
// Copyright (c) 2015年 striveliu. All rights reserved.
//

#import "DJScanViewController.h"
#import <AVFoundation/AVFoundation.h>

#define kButtonWidth 180
#define kButtonHeight 40
#define kScanWidth 250

@interface DJScanViewController()<AVCaptureMetadataOutputObjectsDelegate>

@property (nonatomic, assign) BOOL isReading;
@property (nonatomic, strong) AVCaptureSession * captureSession;
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *videoPreviewLayer;

@end;

@implementation DJScanViewController

- (void)viewDidLoad {
[super viewDidLoad];
if (![self startReading]) {
[self.navigationController popViewControllerAnimated:YES];
}
[self initailUI];
}


- (void)initailUI {
[self addShape];

self.title = @"商品扫码";
UILabel *Label = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, kMainScreenWidth, 20)];
Label.backgroundColor = [UIColor clearColor];
[Label setTextAlignment:NSTextAlignmentCenter];
Label.font = kFont16;
Label.text= @"请将您的摄像头对准条码";
Label.textColor = [UIColor whiteColor];
[self.view addSubview:Label];

UIButton *inputNumButton = [[UIButton alloc] initWithFrame:CGRectMake((kMainScreenWidth-kButtonWidth)/2., kMainScreenHeight-20-44-kButtonHeight-30, kButtonWidth, kButtonHeight)];
[inputNumButton setBackgroundColor:KColor];
[inputNumButton setTitle:@"手工输入条码" forState:UIControlStateNormal];
[inputNumButton addTarget:self action:@selector(inputNum:) forControlEvents:UIControlEventTouchUpInside];
inputNumButton.layer.cornerRadius = 4.;
[self.view addSubview:inputNumButton];
}

- (void)inputNum: (UIButton *)sender {
if ([self.delegate respondsToSelector:@selector(scanController:didScanedAndTransToMessage:)]) {
[self.navigationController popViewControllerAnimated:YES];
[self.delegate needToInputNumberFromScanController:self];
}
}

- (BOOL)startReading {
if (_isReading) {
return YES;
}
_isReading = YES;
NSError *error;
AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];

if (!input) {
NSLog(@"%@", [error localizedDescription]);
return NO;
}

_captureSession = [[AVCaptureSession alloc] init];
// Set the input device on the capture session.
[_captureSession addInput:input];
[_captureSession setSessionPreset:AVCaptureSessionPresetHigh];

AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];
[_captureSession addOutput:captureMetadataOutput];


// [captureMetadataOutput setRectOfInterest:CGRectMake(0.5, 0.5, 0.5, 0.5)];


[captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];

[captureMetadataOutput setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code]];

_videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
[_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[_videoPreviewLayer setFrame:self.view.layer.bounds];
[self.view.layer insertSublayer:_videoPreviewLayer atIndex:0];//:_videoPreviewLayer];

[_captureSession startRunning];

return YES;
}

- (void)addShape {
CGFloat x = (kMainScreenWidth - kScanWidth) / 2.;
CGFloat y = 40 + 40;

CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, nil, self.view.bounds);
CGPathAddRect(path, nil, CGRectMake(x, y, kScanWidth, kScanWidth));

CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.strokeColor = [UIColor whiteColor].CGColor;
shapeLayer.fillColor = RGBACOLOR(0, 0, 0, 0.7).CGColor;
[shapeLayer setFillRule:@"even-odd"];
// shapeLayer.lineWidth = 1;
shapeLayer.lineJoin = kCALineJoinRound;
shapeLayer.lineCap = kCALineCapRound;
shapeLayer.path = path;

[self.view.layer addSublayer:shapeLayer];

CGPathRelease(path);
}


-(void)stopReading{
[_captureSession stopRunning];
_captureSession = nil;
[_videoPreviewLayer removeFromSuperlayer];
_isReading = NO;
}

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects
fromConnection:(AVCaptureConnection *)connection
{
if (!_isReading) return;

if (metadataObjects != nil && [metadataObjects count] > 0) {
AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];
NSLog(@"扫描到的信息%@",metadataObj.stringValue);
if ([self.delegate respondsToSelector:@selector(scanController:didScanedAndTransToMessage:)]) {
[self stopReading];
[self.delegate scanController:self didScanedAndTransToMessage:metadataObj.stringValue];
[self.navigationController popViewControllerAnimated:YES];
}
}
}

@end
1 change: 1 addition & 0 deletions YHB_Prj/YHB_Prj/main.m
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

//
// main.m
// YHB_Prj
Expand Down

0 comments on commit 0f2f1d9

Please sign in to comment.