1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// ZXingWrapper.m
//
//
// Created by lbxia on 15/1/6.
// Copyright (c) 2015年 lbxia. All rights reserved.
//
#import "ZXingWrapper.h"
#import "ZXingObjC.h"
#import "LBXZXCaptureDelegate.h"
#import "LBXZXCapture.h"
typedef void(^blockScan)(ZXBarcodeFormat barcodeFormat,NSString *str,UIImage *scanImg);
@interface ZXingWrapper() <LBXZXCaptureDelegate>
@property (nonatomic, strong) LBXZXCapture *capture;
@property (nonatomic,copy)blockScan block;
@property (nonatomic, assign) BOOL bNeedScanResult;
@end
@implementation ZXingWrapper
- (id)init
{
if ( self = [super init] )
{
self.capture = [[LBXZXCapture alloc] init];
self.capture.camera = self.capture.back;
self.capture.focusMode = AVCaptureFocusModeContinuousAutoFocus;
self.capture.rotation = 90.0f;
self.capture.delegate = self;
}
return self;
}
- (id)initWithPreView:(UIView*)preView block:(void(^)(ZXBarcodeFormat barcodeFormat,NSString *str,UIImage *scanImg))block
{
if (self = [super init]) {
self.capture = [[LBXZXCapture alloc] init];
self.capture.camera = self.capture.back;
self.capture.focusMode = AVCaptureFocusModeContinuousAutoFocus;
self.capture.rotation = 90.0f;
self.capture.delegate = self;
self.block = block;
CGRect rect = preView.frame;
rect.origin = CGPointZero;
self.capture.layer.frame = rect;
//[preView.layer addSublayer:self.capture.layer];
[preView.layer insertSublayer:self.capture.layer atIndex:0];
}
return self;
}
- (void)setScanRect:(CGRect)scanRect
{
self.capture.scanRect = scanRect;
}
- (void)start
{
[self.capture start];
self.bNeedScanResult = YES;
}
- (void)stop
{
self.bNeedScanResult = NO;
[self.capture stop];
}
- (void)openTorch:(BOOL)on_off
{
[self.capture setTorch:on_off];
}
- (void)openOrCloseTorch
{
[self.capture changeTorch];
}
#pragma mark - ZXCaptureDelegate Methods
- (void)captureResult:(ZXCapture *)capture result:(ZXResult *)result scanImage:(UIImage *)img {
if (!result) return;
if (!_bNeedScanResult) {
return;
}
if ( _block )
{
[self stop];
_block(result.barcodeFormat,result.text,img);
}
}
+ (UIImage*)createCodeWithString:(NSString*)str size:(CGSize)size CodeFomart:(ZXBarcodeFormat)format
{
ZXMultiFormatWriter *writer = [[ZXMultiFormatWriter alloc] init];
ZXBitMatrix *result = [writer encode:str
format:format
width:size.width
height:size.width
error:nil];
if (result) {
ZXImage *image = [ZXImage imageWithMatrix:result];
return [UIImage imageWithCGImage:image.cgimage];
} else {
return nil;
}
}
+ (void)recognizeImage:(UIImage*)image block:(void(^)(ZXBarcodeFormat barcodeFormat,NSString *str))block;
{
ZXCGImageLuminanceSource *source = [[ZXCGImageLuminanceSource alloc] initWithCGImage:image.CGImage];
ZXHybridBinarizer *binarizer = [[ZXHybridBinarizer alloc] initWithSource: source];
ZXBinaryBitmap *bitmap = [[ZXBinaryBitmap alloc] initWithBinarizer:binarizer];
NSError *error;
id<ZXReader> reader;
if (NSClassFromString(@"ZXMultiFormatReader")) {
reader = [NSClassFromString(@"ZXMultiFormatReader") performSelector:@selector(reader)];
}
ZXDecodeHints *_hints = [ZXDecodeHints hints];
ZXResult *result = [reader decode:bitmap hints:_hints error:&error];
if (result == nil) {
block(kBarcodeFormatQRCode,nil);
return;
}
block(result.barcodeFormat,result.text);
}
@end