eb6a6e93f639c3bff277fd5d1326b375a84c56a5
[zxing.git] / iphone / ZXingWidget / Classes / 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 @interface OverlayView()
22 @property (nonatomic,assign) UIButton *cancelButton;
23 @end
24
25
26 @implementation OverlayView
27
28 @synthesize delegate, oneDMode;
29 @synthesize points = _points;
30 @synthesize cancelButton;
31
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];
41       if (oneDMode) {
42         [cancelButton setTransform:CGAffineTransformMakeRotation(M_PI/2)];
43         [cancelButton setFrame:CGRectMake(20, 175, 45, 130)];
44       }
45       else {
46         [cancelButton setFrame:CGRectMake(95, 420, 130, 45)];                   
47       }
48       
49       [cancelButton addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
50       [self addSubview:cancelButton];
51       [self addSubview:imageView];
52     }
53   }
54         return self;
55 }
56
57 - (void)cancel:(id)sender {
58         // call delegate to cancel this scanner
59         if (delegate != nil) {
60                 [delegate cancelled];
61         }
62 }
63
64 ////////////////////////////////////////////////////////////////////////////////////////////////////
65 - (void) dealloc {
66         [imageView release];
67         imageView = nil;
68         [_points release];
69         _points = nil;
70         [super dealloc];
71 }
72
73
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);
82 }
83
84
85 ////////////////////////////////////////////////////////////////////////////////////////////////////
86 - (void)drawRect:(CGRect)rect {
87         [super drawRect:rect];
88         CGContextRef c = UIGraphicsGetCurrentContext();
89   
90         CGRect cropRect = [self cropRect];
91         
92         if (nil != _points) {
93     //          [imageView.image drawAtPoint:cropRect.origin];
94         }
95         
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];
100         
101   //    CGContextSetStrokeColor(c, white);
102         //      CGContextSetStrokeColor(c, white);
103         CGContextSaveGState(c);
104         if (oneDMode) {
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);
110         }
111         else {
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);
119         }
120         CGContextRestoreGState(c);
121         int offset = rect.size.width / 2;
122         if (oneDMode) {
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);
132         }
133         if( nil != _points ) {
134                 CGFloat blue[4] = {0.0f, 1.0f, 0.0f, 1.0f};
135                 CGContextSetStrokeColor(c, blue);
136                 CGContextSetFillColor(c, blue);
137                 if (oneDMode) {
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);
143                 }
144                 else {
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];
152                         }
153                 }
154         }
155 }
156
157
158 ////////////////////////////////////////////////////////////////////////////////////////////////////
159 /*
160  - (void) setImage:(UIImage*)image {
161  //if( nil == imageView ) {
162 // imageView = [[UIImageView alloc] initWithImage:image];
163 // imageView.alpha = 0.5;
164 // } else {
165  imageView.image = image;
166  //}
167  
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);
172  
173  //[_points release];
174  //_points = nil;
175  //self.backgroundColor = [UIColor clearColor];
176  
177  //[self setNeedsDisplay];
178  }
179  */
180
181 ////////////////////////////////////////////////////////////////////////////////////////////////////
182 - (UIImage*) image {
183         return imageView.image;
184 }
185
186
187 ////////////////////////////////////////////////////////////////////////////////////////////////////
188 - (CGRect) cropRect {
189         CGFloat rectSize = self.frame.size.width - kPadding * 2;
190         if (!oneDMode) {
191                 return CGRectMake(kPadding, (self.frame.size.height - rectSize) / 2, rectSize, rectSize);
192         }
193         else {
194                 CGFloat rectSize2 = self.frame.size.height - kPadding * 2;
195                 return CGRectMake(kPadding, kPadding, rectSize, rectSize2);             
196         }
197 }
198
199 /*
200 - (void)viewDidLoad {
201   self.imageView = [[UIImageView alloc] init];
202   self.imageView.frame = CGRectMake(0, 0, 100, 100);
203   [self addSubview:self.imageView];
204         
205 }
206 */
207
208 ////////////////////////////////////////////////////////////////////////////////////////////////////
209 - (void) setPoints:(NSArray*)pnts {
210         [pnts retain];
211         [_points release];
212         _points = pnts;
213         
214         if (pnts != nil) {
215                 self.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.25];
216         }
217         [self setNeedsDisplay];
218 }
219
220
221 @end