[iphone] added known issues in README when including ZXingWidget into your project...
[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 @implementation OverlayView
22
23 @synthesize delegate, oneDMode;
24 @synthesize points = _points;
25
26
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];
35       if (oneDMode) {
36         [cancelButton setTransform:CGAffineTransformMakeRotation(M_PI/2)];
37         [cancelButton setFrame:CGRectMake(20, 175, 45, 130)];
38       }
39       else {
40         [cancelButton setFrame:CGRectMake(95, 420, 130, 45)];                   
41       }
42       
43       [cancelButton addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
44       [self addSubview:cancelButton];
45     }
46   }
47         return self;
48 }
49
50 - (void)cancel:(id)sender {
51         // call delegate to cancel this scanner
52         if (delegate != nil) {
53                 [delegate cancelled];
54         }
55 }
56
57 ////////////////////////////////////////////////////////////////////////////////////////////////////
58 - (void) dealloc {
59         [imageView release];
60         imageView = nil;
61         [_points release];
62         _points = nil;
63         [cancelButton release];
64         [super dealloc];
65 }
66
67
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);
76 }
77
78
79 ////////////////////////////////////////////////////////////////////////////////////////////////////
80 - (void)drawRect:(CGRect)rect {
81         [super drawRect:rect];
82         CGContextRef c = UIGraphicsGetCurrentContext();
83   
84         CGRect cropRect = [self cropRect];
85         
86         if (nil != _points) {
87     //          [imageView.image drawAtPoint:cropRect.origin];
88         }
89         
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];
94         
95   //    CGContextSetStrokeColor(c, white);
96         //      CGContextSetStrokeColor(c, white);
97         CGContextSaveGState(c);
98         if (oneDMode) {
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);
104         }
105         else {
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);
113         }
114         CGContextRestoreGState(c);
115         int offset = rect.size.width / 2;
116         if (oneDMode) {
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);
126         }
127         if( nil != _points ) {
128                 CGFloat blue[4] = {0.0f, 1.0f, 0.0f, 1.0f};
129                 CGContextSetStrokeColor(c, blue);
130                 CGContextSetFillColor(c, blue);
131                 if (oneDMode) {
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);
137                 }
138                 else {
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];
146                         }
147                 }
148         }
149 }
150
151
152 ////////////////////////////////////////////////////////////////////////////////////////////////////
153 /*
154  - (void) setImage:(UIImage*)image {
155  if( nil == _imageView ) {
156  _imageView = [[UIImageView alloc] initWithImage:image];
157  _imageView.alpha = 0.5;
158  } else {
159  _imageView.image = image;
160  }
161  
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;
166  
167  [_points release];
168  _points = nil;
169  self.backgroundColor = [UIColor clearColor];
170  
171  [self setNeedsDisplay];
172  }
173  */
174
175 ////////////////////////////////////////////////////////////////////////////////////////////////////
176 - (UIImage*) image {
177         return imageView.image;
178 }
179
180
181 ////////////////////////////////////////////////////////////////////////////////////////////////////
182 - (CGRect) cropRect {
183         CGFloat rectSize = self.frame.size.width - kPadding * 2;
184         if (!oneDMode) {
185                 return CGRectMake(kPadding, (self.frame.size.height - rectSize) / 2, rectSize, rectSize);
186         }
187         else {
188                 CGFloat rectSize2 = self.frame.size.height - kPadding * 2;
189                 return CGRectMake(kPadding, kPadding, rectSize, rectSize2);             
190         }
191 }
192
193
194 ////////////////////////////////////////////////////////////////////////////////////////////////////
195 - (void) setPoints:(NSArray*)pnts {
196         [pnts retain];
197         [_points release];
198         _points = pnts;
199         
200         if (pnts != nil) {
201                 self.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.25];
202         }
203         [self setNeedsDisplay];
204 }
205
206
207 @end