the very simple test app that calls the ZXingWidget
[zxing.git] / iphone / ZXingWidget / OverlayView.m
1 /**
2  * Copyright 2009 Jeff Verkoeyen
3  *
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
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #import "OverlayView.h"
18
19 static const CGFloat kPadding = 10;
20
21 @implementation OverlayView
22
23 @synthesize delegate;
24 @synthesize points = _points;
25 //@synthesize image;
26
27
28 ////////////////////////////////////////////////////////////////////////////////////////////////////
29 - (id)initWithCancelEnabled:(BOOL)cancelEnabled frame:(CGRect)frame {
30         if( self = [super initWithFrame:frame] ) {
31                 self.backgroundColor = [UIColor clearColor];
32         }
33         if (cancelEnabled) {
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];
39         }
40         return self;
41 }
42
43 - (void)cancel:(id)sender {
44         // call delegate to cancel this scanner
45         if (delegate != nil) {
46                 [delegate cancelled];
47         }
48 }
49
50 ////////////////////////////////////////////////////////////////////////////////////////////////////
51 - (void) dealloc {
52         [imageView release];
53         imageView = nil;
54         [_points release];
55         _points = nil;
56         
57         [super dealloc];
58 }
59
60
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);
69 }
70
71
72 ////////////////////////////////////////////////////////////////////////////////////////////////////
73 - (void)drawRect:(CGRect)rect {
74         [super drawRect:rect];
75         CGContextRef c = UIGraphicsGetCurrentContext();
76
77         CGRect cropRect = [self cropRect];
78         
79         if (nil != _points) {
80 //              [imageView.image drawAtPoint:cropRect.origin];
81         }
82         
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];
87         
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];
111                 }
112         }
113 }
114
115
116 ////////////////////////////////////////////////////////////////////////////////////////////////////
117 /*
118 - (void) setImage:(UIImage*)image {
119         if( nil == _imageView ) {
120                 _imageView = [[UIImageView alloc] initWithImage:image];
121                 _imageView.alpha = 0.5;
122         } else {
123                 _imageView.image = image;
124         }
125         
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;
130         
131         [_points release];
132         _points = nil;
133         self.backgroundColor = [UIColor clearColor];
134         
135         [self setNeedsDisplay];
136 }
137 */
138
139 ////////////////////////////////////////////////////////////////////////////////////////////////////
140 - (UIImage*) image {
141         return imageView.image;
142 }
143
144
145 ////////////////////////////////////////////////////////////////////////////////////////////////////
146 - (CGRect) cropRect {
147         CGFloat rectSize = self.frame.size.width - kPadding * 2;
148         
149         return CGRectMake(kPadding, (self.frame.size.height - rectSize) / 2, rectSize, rectSize);
150 }
151
152
153 ////////////////////////////////////////////////////////////////////////////////////////////////////
154 - (void) setPoints:(NSArray*)pnts {
155         [pnts retain];
156         [_points release];
157         _points = pnts;
158         
159         if (pnts != nil) {
160                 self.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.25];
161         }
162         [self setNeedsDisplay];
163 }
164
165
166 @end