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
24 @synthesize points = _points;
28 ////////////////////////////////////////////////////////////////////////////////////////////////////
29 - (id)initWithCancelEnabled:(BOOL)cancelEnabled frame:(CGRect)frame {
30 if( self = [super initWithFrame:frame] ) {
31 self.backgroundColor = [UIColor clearColor];
34 cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
35 [cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
36 [cancelButton setFrame:CGRectMake(95, 420, 130, 45)];
37 [cancelButton addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
38 [self addSubview:cancelButton];
43 - (void)cancel:(id)sender {
44 // call delegate to cancel this scanner
45 if (delegate != nil) {
50 ////////////////////////////////////////////////////////////////////////////////////////////////////
61 - (void)drawRect:(CGRect)rect inContext:(CGContextRef)context {
62 CGContextBeginPath(context);
63 CGContextMoveToPoint(context, rect.origin.x, rect.origin.y);
64 CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y);
65 CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y + rect.size.height);
66 CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y + rect.size.height);
67 CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y);
68 CGContextStrokePath(context);
72 ////////////////////////////////////////////////////////////////////////////////////////////////////
73 - (void)drawRect:(CGRect)rect {
74 [super drawRect:rect];
75 CGContextRef c = UIGraphicsGetCurrentContext();
77 CGRect cropRect = [self cropRect];
80 // [imageView.image drawAtPoint:cropRect.origin];
83 CGFloat white[4] = {1.0f, 1.0f, 1.0f, 1.0f};
84 CGContextSetStrokeColor(c, white);
85 CGContextSetFillColor(c, white);
86 [self drawRect:cropRect inContext:c];
88 // CGContextSetStrokeColor(c, white);
89 char *text = "Place a barcode inside the";
90 char *text2 = "viewfinder rectangle to scan it.";
91 // CGContextSetStrokeColor(c, white);
92 CGContextSaveGState(c);
93 CGContextSelectFont(c, "Helvetica", 18, kCGEncodingMacRoman);
94 CGContextScaleCTM(c, -1.0, 1.0);
95 CGContextRotateCTM(c, 3.1415);
96 CGContextShowTextAtPoint(c, 48.0, -45.0, text, 26);
97 CGContextShowTextAtPoint(c, 33.0, -70.0, text2, 32);
98 CGContextRestoreGState(c);
99 if( nil != _points ) {
100 CGFloat blue[4] = {0.0f, 1.0f, 0.0f, 1.0f};
101 CGContextSetStrokeColor(c, blue);
102 CGContextSetFillColor(c, blue);
103 CGRect smallSquare = CGRectMake(0, 0, 10, 10);
104 for( NSValue* value in _points ) {
105 CGPoint point = [value CGPointValue];
106 NSLog(@"drawing point at %f, %f", point.x, point.y);
107 smallSquare.origin = CGPointMake(
108 cropRect.origin.x + point.x - smallSquare.size.width / 2,
109 cropRect.origin.y + point.y - smallSquare.size.height / 2);
110 [self drawRect:smallSquare inContext:c];
116 ////////////////////////////////////////////////////////////////////////////////////////////////////
118 - (void) setImage:(UIImage*)image {
119 if( nil == _imageView ) {
120 _imageView = [[UIImageView alloc] initWithImage:image];
121 _imageView.alpha = 0.5;
123 _imageView.image = image;
126 CGRect frame = _imageView.frame;
127 frame.origin.x = self.cropRect.origin.x;
128 frame.origin.y = self.cropRect.origin.y;
129 _imageView.frame = frame;
133 self.backgroundColor = [UIColor clearColor];
135 [self setNeedsDisplay];
139 ////////////////////////////////////////////////////////////////////////////////////////////////////
141 return imageView.image;
145 ////////////////////////////////////////////////////////////////////////////////////////////////////
146 - (CGRect) cropRect {
147 CGFloat rectSize = self.frame.size.width - kPadding * 2;
149 return CGRectMake(kPadding, (self.frame.size.height - rectSize) / 2, rectSize, rectSize);
153 ////////////////////////////////////////////////////////////////////////////////////////////////////
154 - (void) setPoints:(NSArray*)pnts {
160 self.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.25];
162 [self setNeedsDisplay];