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 @implementation OverlayView
23 @synthesize delegate, oneDMode;
24 @synthesize points = _points;
27 ////////////////////////////////////////////////////////////////////////////////////////////////////
28 - (id) initWithFrame:(CGRect)theFrame cancelEnabled:(BOOL)isCancelEnabled oneDMode:(BOOL)isOneDModeEnabled {
29 if( self = [super initWithFrame:theFrame] ) {
30 self.backgroundColor = [UIColor clearColor];
31 self.oneDMode = isOneDModeEnabled;
32 if (isCancelEnabled) {
33 cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
34 [cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
36 [cancelButton setTransform:CGAffineTransformMakeRotation(M_PI/2)];
37 [cancelButton setFrame:CGRectMake(20, 175, 45, 130)];
40 [cancelButton setFrame:CGRectMake(95, 420, 130, 45)];
43 [cancelButton addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
44 [self addSubview:cancelButton];
50 - (void)cancel:(id)sender {
51 // call delegate to cancel this scanner
52 if (delegate != nil) {
57 ////////////////////////////////////////////////////////////////////////////////////////////////////
63 [cancelButton release];
68 - (void)drawRect:(CGRect)rect inContext:(CGContextRef)context {
69 CGContextBeginPath(context);
70 CGContextMoveToPoint(context, rect.origin.x, rect.origin.y);
71 CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y);
72 CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y + rect.size.height);
73 CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y + rect.size.height);
74 CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y);
75 CGContextStrokePath(context);
79 ////////////////////////////////////////////////////////////////////////////////////////////////////
80 - (void)drawRect:(CGRect)rect {
81 [super drawRect:rect];
82 CGContextRef c = UIGraphicsGetCurrentContext();
84 CGRect cropRect = [self cropRect];
87 // [imageView.image drawAtPoint:cropRect.origin];
90 CGFloat white[4] = {1.0f, 1.0f, 1.0f, 1.0f};
91 CGContextSetStrokeColor(c, white);
92 CGContextSetFillColor(c, white);
93 [self drawRect:cropRect inContext:c];
95 // CGContextSetStrokeColor(c, white);
96 // CGContextSetStrokeColor(c, white);
97 CGContextSaveGState(c);
99 char *text = "Place a red line over the bar code to be scanned.";
100 CGContextSelectFont(c, "Helvetica", 15, kCGEncodingMacRoman);
101 CGContextScaleCTM(c, -1.0, 1.0);
102 CGContextRotateCTM(c, M_PI/2);
103 CGContextShowTextAtPoint(c, 74.0, 285.0, text, 49);
106 char *text = "Place a barcode inside the";
107 char *text2 = "viewfinder rectangle to scan it.";
108 CGContextSelectFont(c, "Helvetica", 18, kCGEncodingMacRoman);
109 CGContextScaleCTM(c, -1.0, 1.0);
110 CGContextRotateCTM(c, M_PI);
111 CGContextShowTextAtPoint(c, 48.0, -45.0, text, 26);
112 CGContextShowTextAtPoint(c, 33.0, -70.0, text2, 32);
114 CGContextRestoreGState(c);
115 int offset = rect.size.width / 2;
117 CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f};
118 CGContextSetStrokeColor(c, red);
119 CGContextSetFillColor(c, red);
120 CGContextBeginPath(c);
121 // CGContextMoveToPoint(c, rect.origin.x + kPadding, rect.origin.y + offset);
122 // CGContextAddLineToPoint(c, rect.origin.x + rect.size.width - kPadding, rect.origin.y + offset);
123 CGContextMoveToPoint(c, rect.origin.x + offset, rect.origin.y + kPadding);
124 CGContextAddLineToPoint(c, rect.origin.x + offset, rect.origin.y + rect.size.height - kPadding);
125 CGContextStrokePath(c);
127 if( nil != _points ) {
128 CGFloat blue[4] = {0.0f, 1.0f, 0.0f, 1.0f};
129 CGContextSetStrokeColor(c, blue);
130 CGContextSetFillColor(c, blue);
132 CGPoint val1 = [[_points objectAtIndex:0] CGPointValue];
133 CGPoint val2 = [[_points objectAtIndex:1] CGPointValue];
134 CGContextMoveToPoint(c, offset, val1.x);
135 CGContextAddLineToPoint(c, offset, val2.x);
136 CGContextStrokePath(c);
139 CGRect smallSquare = CGRectMake(0, 0, 10, 10);
140 for( NSValue* value in _points ) {
141 CGPoint point = [value CGPointValue];
142 smallSquare.origin = CGPointMake(
143 cropRect.origin.x + point.x - smallSquare.size.width / 2,
144 cropRect.origin.y + point.y - smallSquare.size.height / 2);
145 [self drawRect:smallSquare inContext:c];
152 ////////////////////////////////////////////////////////////////////////////////////////////////////
154 - (void) setImage:(UIImage*)image {
155 if( nil == _imageView ) {
156 _imageView = [[UIImageView alloc] initWithImage:image];
157 _imageView.alpha = 0.5;
159 _imageView.image = image;
162 CGRect frame = _imageView.frame;
163 frame.origin.x = self.cropRect.origin.x;
164 frame.origin.y = self.cropRect.origin.y;
165 _imageView.frame = frame;
169 self.backgroundColor = [UIColor clearColor];
171 [self setNeedsDisplay];
175 ////////////////////////////////////////////////////////////////////////////////////////////////////
177 return imageView.image;
181 ////////////////////////////////////////////////////////////////////////////////////////////////////
182 - (CGRect) cropRect {
183 CGFloat rectSize = self.frame.size.width - kPadding * 2;
185 return CGRectMake(kPadding, (self.frame.size.height - rectSize) / 2, rectSize, rectSize);
188 CGFloat rectSize2 = self.frame.size.height - kPadding * 2;
189 return CGRectMake(kPadding, kPadding, rectSize, rectSize2);
194 ////////////////////////////////////////////////////////////////////////////////////////////////////
195 - (void) setPoints:(NSArray*)pnts {
201 self.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.25];
203 [self setNeedsDisplay];