2 * Copyright 2009 Jeff Verkoeyen
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #import "OverlayView.h"
19 static const CGFloat kPadding = 10;
21 @interface OverlayView()
22 @property (nonatomic,assign) UIButton *cancelButton;
26 @implementation OverlayView
28 @synthesize delegate, oneDMode;
29 @synthesize points = _points;
30 @synthesize cancelButton;
32 ////////////////////////////////////////////////////////////////////////////////////////////////////
33 - (id) initWithFrame:(CGRect)theFrame cancelEnabled:(BOOL)isCancelEnabled oneDMode:(BOOL)isOneDModeEnabled {
34 if( self = [super initWithFrame:theFrame] ) {
35 self.backgroundColor = [UIColor clearColor];
36 self.oneDMode = isOneDModeEnabled;
37 if (isCancelEnabled) {
38 UIButton *butt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
39 self.cancelButton = butt;
40 [cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
42 [cancelButton setTransform:CGAffineTransformMakeRotation(M_PI/2)];
43 [cancelButton setFrame:CGRectMake(20, 175, 45, 130)];
46 [cancelButton setFrame:CGRectMake(95, 420, 130, 45)];
49 [cancelButton addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
50 [self addSubview:cancelButton];
51 [self addSubview:imageView];
57 - (void)cancel:(id)sender {
58 // call delegate to cancel this scanner
59 if (delegate != nil) {
64 ////////////////////////////////////////////////////////////////////////////////////////////////////
74 - (void)drawRect:(CGRect)rect inContext:(CGContextRef)context {
75 CGContextBeginPath(context);
76 CGContextMoveToPoint(context, rect.origin.x, rect.origin.y);
77 CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y);
78 CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y + rect.size.height);
79 CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y + rect.size.height);
80 CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y);
81 CGContextStrokePath(context);
85 ////////////////////////////////////////////////////////////////////////////////////////////////////
86 - (void)drawRect:(CGRect)rect {
87 [super drawRect:rect];
88 CGContextRef c = UIGraphicsGetCurrentContext();
90 CGRect cropRect = [self cropRect];
93 // [imageView.image drawAtPoint:cropRect.origin];
96 CGFloat white[4] = {1.0f, 1.0f, 1.0f, 1.0f};
97 CGContextSetStrokeColor(c, white);
98 CGContextSetFillColor(c, white);
99 [self drawRect:cropRect inContext:c];
101 // CGContextSetStrokeColor(c, white);
102 // CGContextSetStrokeColor(c, white);
103 CGContextSaveGState(c);
105 char *text = "Place a red line over the bar code to be scanned.";
106 CGContextSelectFont(c, "Helvetica", 15, kCGEncodingMacRoman);
107 CGContextScaleCTM(c, -1.0, 1.0);
108 CGContextRotateCTM(c, M_PI/2);
109 CGContextShowTextAtPoint(c, 74.0, 285.0, text, 49);
112 char *text = "Place a barcode inside the";
113 char *text2 = "viewfinder rectangle to scan it.";
114 CGContextSelectFont(c, "Helvetica", 18, kCGEncodingMacRoman);
115 CGContextScaleCTM(c, -1.0, 1.0);
116 CGContextRotateCTM(c, M_PI);
117 CGContextShowTextAtPoint(c, 48.0, -45.0, text, 26);
118 CGContextShowTextAtPoint(c, 33.0, -70.0, text2, 32);
120 CGContextRestoreGState(c);
121 int offset = rect.size.width / 2;
123 CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f};
124 CGContextSetStrokeColor(c, red);
125 CGContextSetFillColor(c, red);
126 CGContextBeginPath(c);
127 // CGContextMoveToPoint(c, rect.origin.x + kPadding, rect.origin.y + offset);
128 // CGContextAddLineToPoint(c, rect.origin.x + rect.size.width - kPadding, rect.origin.y + offset);
129 CGContextMoveToPoint(c, rect.origin.x + offset, rect.origin.y + kPadding);
130 CGContextAddLineToPoint(c, rect.origin.x + offset, rect.origin.y + rect.size.height - kPadding);
131 CGContextStrokePath(c);
133 if( nil != _points ) {
134 CGFloat blue[4] = {0.0f, 1.0f, 0.0f, 1.0f};
135 CGContextSetStrokeColor(c, blue);
136 CGContextSetFillColor(c, blue);
138 CGPoint val1 = [[_points objectAtIndex:0] CGPointValue];
139 CGPoint val2 = [[_points objectAtIndex:1] CGPointValue];
140 CGContextMoveToPoint(c, offset, val1.x);
141 CGContextAddLineToPoint(c, offset, val2.x);
142 CGContextStrokePath(c);
145 CGRect smallSquare = CGRectMake(0, 0, 10, 10);
146 for( NSValue* value in _points ) {
147 CGPoint point = [value CGPointValue];
148 smallSquare.origin = CGPointMake(
149 cropRect.origin.x + point.x - smallSquare.size.width / 2,
150 cropRect.origin.y + point.y - smallSquare.size.height / 2);
151 [self drawRect:smallSquare inContext:c];
158 ////////////////////////////////////////////////////////////////////////////////////////////////////
160 - (void) setImage:(UIImage*)image {
161 //if( nil == imageView ) {
162 // imageView = [[UIImageView alloc] initWithImage:image];
163 // imageView.alpha = 0.5;
165 imageView.image = image;
168 //CGRect frame = imageView.frame;
169 //frame.origin.x = self.cropRect.origin.x;
170 //frame.origin.y = self.cropRect.origin.y;
171 //imageView.frame = CGRectMake(0,0, 30, 50);
175 //self.backgroundColor = [UIColor clearColor];
177 //[self setNeedsDisplay];
181 ////////////////////////////////////////////////////////////////////////////////////////////////////
183 return imageView.image;
187 ////////////////////////////////////////////////////////////////////////////////////////////////////
188 - (CGRect) cropRect {
189 CGFloat rectSize = self.frame.size.width - kPadding * 2;
191 return CGRectMake(kPadding, (self.frame.size.height - rectSize) / 2, rectSize, rectSize);
194 CGFloat rectSize2 = self.frame.size.height - kPadding * 2;
195 return CGRectMake(kPadding, kPadding, rectSize, rectSize2);
200 - (void)viewDidLoad {
201 self.imageView = [[UIImageView alloc] init];
202 self.imageView.frame = CGRectMake(0, 0, 100, 100);
203 [self addSubview:self.imageView];
208 ////////////////////////////////////////////////////////////////////////////////////////////////////
209 - (void) setPoints:(NSArray*)pnts {
215 self.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.25];
217 [self setNeedsDisplay];