2008-06-19 01:52:37 -07:00
//
// DecoderViewController . m
// ZXing
//
// Created by Christian Brunschen on 22 / 05 / 2008.
/ *
2008-06-19 13:56:24 -07:00
* Copyright 2008 ZXing authors
2008-06-19 01:52:37 -07:00
*
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : // www . apache . org / licenses / LICENSE -2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an "AS IS" BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
* /
# import "DecoderViewController.h"
# import "Decoder.h"
# import "NSString+HTML.h"
2008-06-25 06:17:06 -07:00
# import "ResultParser.h"
2008-06-19 01:52:37 -07:00
# import "ParsedResult.h"
# import "ResultAction.h"
# import "Database.h"
# import "ArchiveController.h"
2008-08-14 04:00:48 -07:00
# import "MessageViewController.h"
2008-06-19 01:52:37 -07:00
# import "Scan.h"
# import "TwoDDecoderResult.h"
2010-05-07 10:10:06 -07:00
// Michael Jurewitz , Dec 16 , 2009 6 : 32 PM writes :
// https : // devforums . apple . com / message / 149553
// Notice Regarding UIGetScreenImage ( )
// After carefully considering the issue , Apple is now allowing applications to
// use the function UIGetScreenImage ( ) to programmatically capture the current
// screen contents .
// Note that a future release of iPhone OS may provide a public API equivalent
// of this functionality . At such time , all applications using
// UIGetScreenImage ( ) will be required to adopt the public API .
CGImageRef MyCGImageCopyScreenContents ( void ) {
extern CGImageRef UIGetScreenImage ( void ) ;
return UIGetScreenImage ( ) ; / * already retained * /
}
@ interface DecoderViewController ( )
- ( void ) takeScreenshot ;
@ end
2008-06-19 01:52:37 -07:00
@ implementation DecoderViewController
@ synthesize cameraBarItem ;
@ synthesize libraryBarItem ;
@ synthesize savedPhotosBarItem ;
@ synthesize archiveBarItem ;
@ synthesize actionBarItem ;
@ synthesize messageView ;
2008-08-01 06:04:23 -07:00
@ synthesize messageTextView ;
@ synthesize messageHelpButton ;
2008-06-19 01:52:37 -07:00
@ synthesize imageView ;
2010-05-07 10:10:06 -07:00
@ synthesize picker ;
2008-06-19 01:52:37 -07:00
@ synthesize toolbar ;
@ synthesize decoder ;
@ synthesize result ;
@ synthesize actions ;
2008-07-02 07:22:23 -07:00
@ synthesize resultPointViews ;
2008-06-19 01:52:37 -07:00
- ( id ) initWithNibName : ( NSString * ) nibNameOrNil bundle : ( NSBundle * ) nibBundleOrNil {
2010-05-07 10:10:06 -07:00
if ( ( self = [ super initWithNibName : nibNameOrNil bundle : nibBundleOrNil ] ) ) {
// Initialization code
self . title =
NSLocalizedString ( @ "DecoderViewController AppTitle" , @ "Barcode Scanner" ) ;
2008-06-19 01:52:37 -07:00
Decoder * d = [ [ Decoder alloc ] init ] ;
self . decoder = d ;
d . delegate = self ;
[ d release ] ;
2008-07-02 07:22:23 -07:00
resultPointViews = [ [ NSMutableArray alloc ] init ] ;
2010-05-07 10:10:06 -07:00
}
return self ;
2008-06-19 01:52:37 -07:00
}
2008-08-14 04:00:48 -07:00
- ( void ) messageReady : ( id ) sender {
MessageViewController * messageController = sender ;
[ [ self navigationController ] pushViewController : messageController animated : true ] ;
[ messageController release ] ;
2008-08-01 06:04:23 -07:00
}
2008-08-14 04:00:48 -07:00
- ( void ) messageFailed : ( id ) sender {
MessageViewController * messageController = sender ;
NSLog ( @ "Failed to load message!" ) ;
[ messageController release ] ;
2008-08-01 06:04:23 -07:00
}
- ( void ) showHints : ( id ) sender {
NSLog ( @ "Showing Hints!" ) ;
2010-05-07 10:10:06 -07:00
MessageViewController * hintsController =
2010-05-04 14:44:53 -07:00
[ [ MessageViewController alloc ] initWithMessageFilename : @ "Hints"
2010-05-07 10:10:06 -07:00
target : self
onSuccess : @ selector ( messageReady : )
2010-05-04 14:44:53 -07:00
onFailure : @ selector ( messageFailed : ) ] ;
2010-05-07 10:10:06 -07:00
hintsController . title =
NSLocalizedString ( @ "DecoderViewController Hints MessageViewController title" , @ "Hints" ) ;
2010-05-04 14:44:53 -07:00
[ hintsController view ] ;
2008-08-01 06:04:23 -07:00
}
2008-08-14 04:00:48 -07:00
- ( void ) showAbout : ( id ) sender {
NSLog ( @ "Showing About!" ) ;
2010-05-07 10:10:06 -07:00
MessageViewController * aboutController =
2010-05-04 14:44:53 -07:00
[ [ MessageViewController alloc ] initWithMessageFilename : @ "About"
2010-05-07 10:10:06 -07:00
target : self
onSuccess : @ selector ( messageReady : )
2010-05-04 14:44:53 -07:00
onFailure : @ selector ( messageFailed : ) ] ;
2010-05-07 10:10:06 -07:00
aboutController . title =
NSLocalizedString ( @ "DecoderViewController About MessageViewController title" , @ "About" ) ;
2010-05-04 14:44:53 -07:00
[ aboutController view ] ;
2008-08-14 04:00:48 -07:00
}
2010-05-07 10:10:06 -07:00
2008-08-01 06:04:23 -07:00
# define HELP_BUTTON _WIDTH ( 44.0 )
# define HELP_BUTTON _HEIGHT ( 55.0 )
2008-07-02 07:22:23 -07:00
# define FONT_NAME @ "TimesNewRomanPSMT"
# define FONT_SIZE 16.0
2008-06-19 01:52:37 -07:00
2008-08-01 06:04:23 -07:00
- ( void ) reset {
self . result = nil ;
[ self clearImageView ] ;
[ self updateToolbar ] ;
2010-05-07 10:10:06 -07:00
[ self showMessage : NSLocalizedString ( @ "DecoderViewController take or choose picture" ,
@ "Please take or choose a picture containing a barcode" ) helpButton : YES ] ;
2008-08-01 06:04:23 -07:00
}
2008-06-19 01:52:37 -07:00
// Implement loadView if you want to create a view hierarchy programmatically
- ( void ) loadView {
[ super loadView ] ;
2010-05-07 10:10:06 -07:00
2008-07-02 07:22:23 -07:00
CGRect messageViewFrame = imageView . frame ;
2008-08-01 06:04:23 -07:00
UIView * mView = [ [ UIView alloc ] initWithFrame : messageViewFrame ] ;
2008-07-02 07:22:23 -07:00
mView . backgroundColor = [ UIColor darkGrayColor ] ;
mView . alpha = 0.9 ;
2010-05-07 10:10:06 -07:00
mView . autoresizingMask = UIViewAutoresizingFlexibleHeight |
2008-08-01 06:04:23 -07:00
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleTopMargin ;
2010-05-07 10:10:06 -07:00
2008-08-01 06:04:23 -07:00
UITextView * mTextView = [ [ UITextView alloc ] initWithFrame : messageViewFrame ] ;
2010-05-07 10:10:06 -07:00
mTextView . autoresizingMask = UIViewAutoresizingFlexibleHeight |
2008-08-01 06:04:23 -07:00
UIViewAutoresizingFlexibleWidth ;
mTextView . editable = false ;
mTextView . scrollEnabled = true ;
mTextView . font = [ UIFont fontWithName : FONT_NAME size : FONT_SIZE ] ;
mTextView . textColor = [ UIColor whiteColor ] ;
mTextView . backgroundColor = [ [ UIColor lightGrayColor ] colorWithAlphaComponent : 0.0 ] ;
mTextView . textAlignment = UITextAlignmentLeft ;
mTextView . alpha = 1.0 ;
[ mView addSubview : mTextView ] ;
UIButton * mHelpButton = [ [ UIButton buttonWithType : UIButtonTypeInfoLight ] retain ] ;
mHelpButton . frame = CGRectMake ( messageViewFrame . size . width - HELP_BUTTON _WIDTH , 0.0 , HELP_BUTTON _WIDTH , HELP_BUTTON _HEIGHT ) ;
2010-05-07 10:10:06 -07:00
2008-08-01 06:04:23 -07:00
mHelpButton . backgroundColor = [ UIColor clearColor ] ;
[ mHelpButton setUserInteractionEnabled : YES ] ;
[ mHelpButton addTarget : self action : @ selector ( showHints : ) forControlEvents : UIControlEventTouchUpInside ] ;
self . messageHelpButton = mHelpButton ;
[ mHelpButton release ] ;
2010-05-07 10:10:06 -07:00
2008-08-01 06:04:23 -07:00
self . messageTextView = mTextView ;
[ mTextView release ] ;
2010-05-07 10:10:06 -07:00
2008-06-19 01:52:37 -07:00
self . messageView = mView ;
[ mView release ] ;
2010-05-07 10:10:06 -07:00
2008-07-02 07:22:23 -07:00
[ self . view addSubview : self . messageView ] ;
2010-05-07 10:10:06 -07:00
2008-08-14 04:00:48 -07:00
// add the ' About ' button at the top - right of the navigation bar
2010-05-07 10:10:06 -07:00
UIBarButtonItem * aboutButton =
[ [ UIBarButtonItem alloc ] initWithTitle : NSLocalizedString ( @ "DecoderViewController about button title" , @ "About" )
2008-08-14 04:00:48 -07:00
style : UIBarButtonItemStyleBordered
2010-05-07 10:10:06 -07:00
target : self
2008-08-14 04:00:48 -07:00
action : @ selector ( showAbout : ) ] ;
self . navigationItem . rightBarButtonItem = aboutButton ;
[ aboutButton release ] ;
2010-05-07 10:10:06 -07:00
2008-08-01 06:04:23 -07:00
[ self reset ] ;
2008-06-19 01:52:37 -07:00
}
- ( void ) updateToolbar {
self . cameraBarItem . enabled = [ UIImagePickerController isSourceTypeAvailable : UIImagePickerControllerSourceTypeCamera ] ;
self . savedPhotosBarItem . enabled = [ UIImagePickerController isSourceTypeAvailable : UIImagePickerControllerSourceTypeSavedPhotosAlbum ] ;
self . libraryBarItem . enabled = [ UIImagePickerController isSourceTypeAvailable : UIImagePickerControllerSourceTypePhotoLibrary ] ;
self . archiveBarItem . enabled = true ;
self . actionBarItem . enabled = ( self . result ! = nil ) && ( [ self . result actions ] ! = nil ) && ( [ self . result actions ] . count > 0 ) ;
}
// If you need to do additional setup after loading the view , override viewDidLoad .
- ( void ) viewDidLoad {
[ super viewDidLoad ] ;
}
2008-06-25 06:17:06 -07:00
- ( void ) clearImageView {
imageView . image = nil ;
2008-07-02 07:22:23 -07:00
for ( UIView * view in resultPointViews ) {
2008-06-25 06:17:06 -07:00
[ view removeFromSuperview ] ;
}
2008-07-02 07:22:23 -07:00
[ resultPointViews removeAllObjects ] ;
2008-06-25 06:17:06 -07:00
}
2008-06-19 01:52:37 -07:00
- ( void ) pickAndDecodeFromSource : ( UIImagePickerControllerSourceType ) sourceType {
2008-08-01 06:04:23 -07:00
[ self reset ] ;
2008-10-13 09:09:09 -07:00
2008-06-19 01:52:37 -07:00
// Create the Image Picker
if ( [ UIImagePickerController isSourceTypeAvailable : sourceType ] ) {
2010-05-07 10:10:06 -07:00
UIImagePickerController * aPicker =
[ [ [ UIImagePickerController alloc ] init ] autorelease ] ;
aPicker . sourceType = sourceType ;
aPicker . delegate = self ;
self . picker = aPicker ;
// [ [ NSUserDefaults standardUserDefaults ] boolForKey : @ "allowEditing" ] ;
BOOL isCamera = ( sourceType = = UIImagePickerControllerSourceTypeCamera ) ;
2010-05-10 10:44:49 -07:00
if ( [ picker respondsToSelector : @ selector ( setAllowsEditing : ) ] ) {
// not in 3.0
[ picker setAllowsEditing : ! isCamera ] ;
}
2010-05-07 10:10:06 -07:00
if ( isCamera ) {
2010-05-10 10:44:49 -07:00
if ( [ picker respondsToSelector : @ selector ( setShowsCameraControls : ) ] ) {
[ picker setShowsCameraControls : NO ] ;
UIButton * cancelButton =
[ UIButton buttonWithType : UIButtonTypeRoundedRect ] ;
NSString * cancelString =
2010-05-07 10:10:06 -07:00
NSLocalizedString ( @ "DecoderViewController cancel button title" , @ "" ) ;
2010-05-10 10:44:49 -07:00
CGFloat height = [ UIFont systemFontSize ] ;
CGSize size =
[ cancelString sizeWithFont : [ UIFont systemFontOfSize : height ] ] ;
[ cancelButton setTitle : cancelString forState : UIControlStateNormal ] ;
CGRect appFrame = [ [ UIScreen mainScreen ] bounds ] ;
static const int kMargin = 10 ;
static const int kInternalXMargin = 10 ;
static const int kInternalYMargin = 10 ;
CGRect frame = CGRectMake ( kMargin ,
appFrame . size . height - ( height + 2 * kInternalYMargin + kMargin ) ,
2 * kInternalXMargin + size . width ,
height + 2 * kInternalYMargin ) ;
[ cancelButton setFrame : frame ] ;
[ cancelButton addTarget : self
action : @ selector ( cancel : )
forControlEvents : UIControlEventTouchUpInside ] ;
picker . cameraOverlayView = cancelButton ;
// The camera takes quite a while to start up . Hence the 2 second delay .
[ self performSelector : @ selector ( takeScreenshot )
withObject : nil
afterDelay : 2.0 ] ;
}
2010-05-07 10:10:06 -07:00
}
2008-06-19 01:52:37 -07:00
// Picker is displayed asynchronously .
[ self presentModalViewController : picker animated : YES ] ;
} else {
NSLog ( @ "Attempted to pick an image with illegal source type '%d'" , sourceType ) ;
}
}
- ( IBAction ) pickAndDecode : ( id ) sender {
UIImagePickerControllerSourceType sourceType ;
int i = [ sender tag ] ;
2010-05-07 10:10:06 -07:00
2008-06-19 01:52:37 -07:00
switch ( i ) {
case 0 : sourceType = UIImagePickerControllerSourceTypeCamera ; break ;
case 1 : sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum ; break ;
case 2 : sourceType = UIImagePickerControllerSourceTypePhotoLibrary ; break ;
default : sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum ;
}
[ self pickAndDecodeFromSource : sourceType ] ;
}
- ( BOOL ) shouldAutorotateToInterfaceOrientation : ( UIInterfaceOrientation ) interfaceOrientation {
2008-10-01 13:19:55 -07:00
// Return YES for supported orientations
return ( interfaceOrientation = = UIInterfaceOrientationPortrait ) ;
2008-06-19 01:52:37 -07:00
}
- ( void ) didReceiveMemoryWarning {
2008-10-01 13:19:55 -07:00
[ super didReceiveMemoryWarning ] ; // Releases the view if it doesn ' t have a superview
// Release anything that ' s not essential , such as cached data
2008-06-19 01:52:37 -07:00
}
- ( void ) dealloc {
[ decoder release ] ;
2008-06-25 06:17:06 -07:00
[ self clearImageView ] ;
2008-06-19 01:52:37 -07:00
[ imageView release ] ;
[ actionBarItem release ] ;
[ cameraBarItem release ] ;
[ libraryBarItem release ] ;
[ savedPhotosBarItem release ] ;
[ archiveBarItem release ] ;
[ toolbar release ] ;
2010-05-07 10:10:06 -07:00
[ picker release ] ;
[ actions release ] ;
[ resultPointViews release ] ;
[ super dealloc ] ;
2008-06-19 01:52:37 -07:00
}
2008-08-01 06:04:23 -07:00
- ( void ) showMessage : ( NSString * ) message helpButton : ( BOOL ) showHelpButton {
2008-06-24 09:24:36 -07:00
# ifdef DEBUG
2008-08-01 06:04:23 -07:00
NSLog ( @ "Showing message '%@' %@ help Button" , message , showHelpButton ? @ "with" : @ "without" ) ;
2008-06-24 09:24:36 -07:00
# endif
2010-05-07 10:10:06 -07:00
2010-05-04 14:44:53 -07:00
CGSize imageMaxSize = imageView . bounds . size ;
2008-08-01 06:04:23 -07:00
if ( showHelpButton ) {
2010-05-04 14:44:53 -07:00
imageMaxSize . width - = messageHelpButton . frame . size . width ;
2008-08-01 06:04:23 -07:00
}
2010-05-04 14:44:53 -07:00
CGSize size = [ message sizeWithFont : messageTextView . font constrainedToSize : imageMaxSize lineBreakMode : UILineBreakModeWordWrap ] ;
2008-07-02 07:22:23 -07:00
float height = 20.0 + fmin ( 100.0 , size . height ) ;
2008-08-01 06:04:23 -07:00
if ( showHelpButton ) {
height = fmax ( HELP_BUTTON _HEIGHT , height ) ;
}
2008-07-02 07:22:23 -07:00
CGRect messageFrame = imageView . bounds ;
messageFrame . origin . y = CGRectGetMaxY ( messageFrame ) - height ;
messageFrame . size . height = height ;
[ self . messageView setFrame : messageFrame ] ;
2008-08-07 08:03:21 -07:00
messageView . autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth ;
2008-08-01 06:04:23 -07:00
CGRect messageViewBounds = [ messageView bounds ] ;
self . messageTextView . text = message ;
2008-08-07 08:03:21 -07:00
messageTextView . autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ;
2008-08-01 06:04:23 -07:00
if ( showHelpButton ) {
CGRect textViewFrame ;
CGRect helpButtonFrame ;
2010-05-07 10:10:06 -07:00
2008-08-01 06:04:23 -07:00
CGRectDivide ( messageViewBounds , & helpButtonFrame , & textViewFrame , HELP_BUTTON _WIDTH , CGRectMaxXEdge ) ;
[ self . messageTextView setFrame : textViewFrame ] ;
2010-05-07 10:10:06 -07:00
2008-08-01 06:04:23 -07:00
[ messageHelpButton setFrame : helpButtonFrame ] ;
messageHelpButton . alpha = 1.0 ;
messageHelpButton . enabled = YES ;
2010-05-07 10:10:06 -07:00
messageHelpButton . autoresizingMask =
UIViewAutoresizingFlexibleLeftMargin |
2008-08-07 08:03:21 -07:00
UIViewAutoresizingFlexibleTopMargin ;
2008-08-01 06:04:23 -07:00
[ messageView addSubview : messageHelpButton ] ;
} else {
[ messageHelpButton removeFromSuperview ] ;
messageHelpButton . alpha = 0.0 ;
messageHelpButton . enabled = NO ;
[ self . messageTextView setFrame : messageViewBounds ] ;
}
2008-06-19 01:52:37 -07:00
}
// DecoderDelegate methods
2008-10-13 09:09:09 -07:00
- ( void ) decoder : ( Decoder * ) decoder willDecodeImage : ( UIImage * ) image usingSubset : ( UIImage * ) subset {
2008-06-25 06:17:06 -07:00
[ self clearImageView ] ;
2008-10-13 09:09:09 -07:00
[ self . imageView setImage : subset ] ;
2008-08-01 06:04:23 -07:00
[ self showMessage : [ NSString stringWithFormat : NSLocalizedString ( @ "DecoderViewController MessageWhileDecodingWithDimensions" , @ "Decoding image (%.0fx%.0f) ..." ) , image . size . width , image . size . height ]
helpButton : NO ] ;
2008-06-19 01:52:37 -07:00
}
2010-05-07 10:10:06 -07:00
- ( void ) decoder : ( Decoder * ) decoder
decodingImage : ( UIImage * ) image
2008-06-19 01:52:37 -07:00
usingSubset : ( UIImage * ) subset
progress : ( NSString * ) message {
2008-06-25 06:17:06 -07:00
[ self clearImageView ] ;
2008-06-19 01:52:37 -07:00
[ self . imageView setImage : subset ] ;
2008-08-01 06:04:23 -07:00
[ self showMessage : message helpButton : NO ] ;
2008-06-19 01:52:37 -07:00
}
- ( void ) presentResultForString : ( NSString * ) resultString {
2008-06-25 06:17:06 -07:00
self . result = [ ResultParser parsedResultForString : resultString ] ;
2008-08-01 06:04:23 -07:00
[ self showMessage : [ self . result stringForDisplay ] helpButton : NO ] ;
2008-06-19 01:52:37 -07:00
self . actions = self . result . actions ;
2008-06-24 09:24:36 -07:00
# ifdef DEBUG
2008-06-19 01:52:37 -07:00
NSLog ( @ "result has %d actions" , actions ? 0 : actions . count ) ;
2008-06-24 09:24:36 -07:00
# endif
2008-06-19 01:52:37 -07:00
[ self updateToolbar ] ;
2010-05-07 10:10:06 -07:00
}
2008-06-25 06:17:06 -07:00
2010-05-07 10:10:06 -07:00
- ( void ) presentResultPoints : ( NSArray * ) resultPoints
2008-06-25 06:17:06 -07:00
forImage : ( UIImage * ) image
usingSubset : ( UIImage * ) subset {
2008-07-02 07:22:23 -07:00
// simply add the points to the image view
imageView . image = subset ;
2008-06-25 06:17:06 -07:00
for ( NSValue * pointValue in resultPoints ) {
2008-07-02 07:22:23 -07:00
[ imageView addResultPoint : [ pointValue CGPointValue ] ] ;
2008-06-25 06:17:06 -07:00
}
}
2008-06-19 01:52:37 -07:00
2008-06-25 06:17:06 -07:00
- ( void ) decoder : ( Decoder * ) decoder didDecodeImage : ( UIImage * ) image usingSubset : ( UIImage * ) subset withResult : ( TwoDDecoderResult * ) twoDResult {
2010-05-07 10:10:06 -07:00
self . picker = nil ;
2008-06-19 01:52:37 -07:00
[ self presentResultForString : twoDResult . text ] ;
2010-05-07 10:10:06 -07:00
2008-06-25 06:17:06 -07:00
[ self presentResultPoints : twoDResult . points forImage : image usingSubset : subset ] ;
2010-05-07 10:10:06 -07:00
2008-06-19 01:52:37 -07:00
// save the scan to the shared database
[ [ Database sharedDatabase ] addScanWithText : twoDResult . text ] ;
2010-05-07 10:10:06 -07:00
2008-06-19 01:52:37 -07:00
[ self performResultAction : self ] ;
}
2008-06-25 06:17:06 -07:00
- ( void ) decoder : ( Decoder * ) decoder failedToDecodeImage : ( UIImage * ) image usingSubset : ( UIImage * ) subset reason : ( NSString * ) reason {
2010-05-07 10:10:06 -07:00
if ( self . picker && UIImagePickerControllerSourceTypeCamera = = self . picker . sourceType ) {
// If we are using the camera , and the user hasn ' t manually cancelled ,
// take another snapshot and try to decode it .
[ self takeScreenshot ] ;
} else {
[ self showMessage : reason helpButton : YES ] ;
[ self updateToolbar ] ;
}
2008-06-19 01:52:37 -07:00
}
2008-07-02 07:22:23 -07:00
- ( void ) willAnimateFirstHalfOfRotationToInterfaceOrientation : ( UIInterfaceOrientation ) toInterfaceOrientation duration : ( NSTimeInterval ) duration {
[ super willAnimateFirstHalfOfRotationToInterfaceOrientation : toInterfaceOrientation duration : duration ] ;
2010-05-07 10:10:06 -07:00
2008-07-02 07:22:23 -07:00
if ( imageView . image ) {
/ *
CGRect viewBounds = imageView . bounds ;
CGSize imageSize = imageView . image . size ;
float scale = fmin ( viewBounds . size . width / imageSize . width ,
viewBounds . size . height / imageSize . height ) ;
float xOffset = ( viewBounds . size . width - scale * imageSize . width ) / 2.0 ;
float yOffset = ( viewBounds . size . height - scale * imageSize . height ) / 2.0 ;
* /
2010-05-07 10:10:06 -07:00
2008-07-02 07:22:23 -07:00
for ( UIView * view in resultPointViews ) {
view . alpha = 0.0 ;
}
2010-05-07 10:10:06 -07:00
}
2008-07-02 07:22:23 -07:00
}
- ( void ) willAnimateSecondHalfOfRotationFromInterfaceOrientation : ( UIInterfaceOrientation ) fromInterfaceOrientation duration : ( NSTimeInterval ) duration {
[ super willAnimateSecondHalfOfRotationFromInterfaceOrientation : fromInterfaceOrientation duration : duration ] ;
if ( imageView . image ) {
/ *
CGRect viewBounds = imageView . bounds ;
CGSize imageSize = imageView . image . size ;
float scale = fmin ( viewBounds . size . width / imageSize . width ,
viewBounds . size . height / imageSize . height ) ;
float xOffset = ( viewBounds . size . width - scale * imageSize . width ) / 2.0 ;
float yOffset = ( viewBounds . size . height - scale * imageSize . height ) / 2.0 ;
* /
2010-05-07 10:10:06 -07:00
2008-07-02 07:22:23 -07:00
for ( UIView * view in resultPointViews ) {
view . alpha = 1.0 ;
}
2010-05-07 10:10:06 -07:00
}
}
- ( void ) cancel : ( id ) sender {
self . picker = nil ;
}
- ( void ) takeScreenshot {
if ( picker ) {
CGImageRef cgScreen = MyCGImageCopyScreenContents ( ) ;
if ( cgScreen ) {
CGRect croppedFrame = CGRectMake ( 0 , 0 , CGImageGetWidth ( cgScreen ) ,
CGImageGetHeight ( cgScreen ) - ( 10 + toolbar . bounds . size . height ) ) ;
CGImageRef cgCropped = CGImageCreateWithImageInRect ( cgScreen , croppedFrame ) ;
if ( cgCropped ) {
UIImage * screenshot = [ UIImage imageWithCGImage : cgCropped ] ;
CGImageRelease ( cgCropped ) ;
[ self . decoder decodeImage : screenshot ] ;
}
CGImageRelease ( cgScreen ) ;
}
}
2008-07-02 07:22:23 -07:00
}
2008-06-19 01:52:37 -07:00
// UIImagePickerControllerDelegate methods
2010-05-07 10:10:06 -07:00
- ( void ) imagePickerController : ( UIImagePickerController * ) aPicker
didFinishPickingMediaWithInfo : ( NSDictionary * ) info {
UIImage * imageToDecode =
[ info objectForKey : UIImagePickerControllerEditedImage ] ;
if ( ! imageToDecode ) {
imageToDecode = [ info objectForKey : UIImagePickerControllerOriginalImage ] ;
}
CGSize size = [ imageToDecode size ] ;
CGRect cropRect = CGRectMake ( 0.0 , 0.0 , size . width , size . height ) ;
2008-06-24 09:24:36 -07:00
# ifdef DEBUG
2008-10-13 09:09:09 -07:00
NSLog ( @ "picked image size = (%f, %f)" , size . width , size . height ) ;
2008-06-25 06:17:06 -07:00
# endif
2008-10-13 09:09:09 -07:00
NSString * systemVersion = [ [ UIDevice currentDevice ] systemVersion ] ;
2010-05-07 10:10:06 -07:00
NSValue * cropRectValue = [ info objectForKey : UIImagePickerControllerCropRect ] ;
if ( cropRectValue ) {
UIImage * originalImage = [ info objectForKey : UIImagePickerControllerOriginalImage ] ;
2008-06-19 01:52:37 -07:00
if ( originalImage ) {
2008-06-25 06:17:06 -07:00
# ifdef DEBUG
2008-06-19 01:52:37 -07:00
NSLog ( @ "original image size = (%f, %f)" , originalImage . size . width , originalImage . size . height ) ;
2008-06-25 06:17:06 -07:00
# endif
2010-05-07 10:10:06 -07:00
cropRect = [ cropRectValue CGRectValue ] ;
2008-06-25 06:17:06 -07:00
# ifdef DEBUG
2010-05-07 10:10:06 -07:00
NSLog ( @ "crop rect = (%f, %f) x (%f, %f)" , CGRectGetMinX ( cropRect ) , CGRectGetMinY ( cropRect ) , CGRectGetWidth ( cropRect ) , CGRectGetHeight ( cropRect ) ) ;
2008-06-25 06:17:06 -07:00
# endif
2010-05-07 10:10:06 -07:00
if ( ( [ picker sourceType ] = = UIImagePickerControllerSourceTypeSavedPhotosAlbum ) &&
[ @ "2.1" isEqualToString : systemVersion ] ) {
// adjust crop rect to work around bug in iPhone OS 2.1 when selecting from the photo roll
cropRect . origin . x * = 2.5 ;
cropRect . origin . y * = 2.5 ;
cropRect . size . width * = 2.5 ;
cropRect . size . height * = 2.5 ;
2008-10-13 09:09:09 -07:00
# ifdef DEBUG
2010-05-07 10:10:06 -07:00
NSLog ( @ "2.1-adjusted crop rect = (%f, %f) x (%f, %f)" , CGRectGetMinX ( cropRect ) , CGRectGetMinY ( cropRect ) , CGRectGetWidth ( cropRect ) , CGRectGetHeight ( cropRect ) ) ;
2008-10-13 09:09:09 -07:00
# endif
2008-06-25 06:17:06 -07:00
}
2010-05-07 10:10:06 -07:00
imageToDecode = originalImage ;
2008-06-19 01:52:37 -07:00
}
}
2010-05-07 10:10:06 -07:00
2008-06-25 06:17:06 -07:00
[ imageToDecode retain ] ;
2010-05-07 10:10:06 -07:00
self . picker = nil ;
2008-10-13 09:09:09 -07:00
[ self . decoder decodeImage : imageToDecode cropRect : cropRect ] ;
2008-06-25 06:17:06 -07:00
[ imageToDecode release ] ;
2008-06-19 01:52:37 -07:00
}
2010-05-07 10:10:06 -07:00
- ( void ) imagePickerControllerDidCancel : ( UIImagePickerController * ) aPicker {
self . picker = nil ;
}
- ( void ) setPicker : ( UIImagePickerController * ) aPicker {
if ( picker ! = aPicker ) {
[ picker dismissModalViewControllerAnimated : YES ] ;
picker = [ aPicker retain ] ;
[ self updateToolbar ] ;
}
2008-06-19 01:52:37 -07:00
}
2010-05-07 10:10:06 -07:00
- ( void ) navigationController : ( UINavigationController * ) navigationController
didShowViewController : ( UIViewController * ) viewController
2008-06-19 01:52:37 -07:00
animated : ( BOOL ) animated {
// no - op
}
2010-05-07 10:10:06 -07:00
- ( void ) navigationController : ( UINavigationController * ) navigationController
willShowViewController : ( UIViewController * ) viewController
2008-06-19 01:52:37 -07:00
animated : ( BOOL ) animated {
// no - op
}
2008-06-24 07:18:11 -07:00
- ( void ) performAction : ( ResultAction * ) action {
[ action performActionWithController : self shouldConfirm : NO ] ;
}
- ( void ) confirmAndPerformAction : ( ResultAction * ) action {
[ action performActionWithController : self shouldConfirm : YES ] ;
}
2008-06-19 01:52:37 -07:00
- ( IBAction ) performResultAction : ( id ) sender {
if ( self . result = = nil ) {
NSLog ( @ "no result to perform an action on!" ) ;
return ;
}
2010-05-07 10:10:06 -07:00
2008-06-19 01:52:37 -07:00
if ( self . actions = = nil || self . actions . count = = 0 ) {
NSLog ( @ "result has no actions to perform!" ) ;
return ;
}
2010-05-07 10:10:06 -07:00
2008-06-19 01:52:37 -07:00
if ( self . actions . count = = 1 ) {
ResultAction * action = [ self . actions lastObject ] ;
2008-06-24 09:24:36 -07:00
# ifdef DEBUG
2008-06-19 01:52:37 -07:00
NSLog ( @ "Result has the single action, (%@) '%@', performing it" ,
NSStringFromClass ( [ action class ] ) , [ action title ] ) ;
2008-06-24 09:24:36 -07:00
# endif
2010-05-07 10:10:06 -07:00
[ self performSelector : @ selector ( confirmAndPerformAction : )
withObject : action
2008-06-19 01:52:37 -07:00
afterDelay : 0.0 ] ;
} else {
2008-06-24 09:24:36 -07:00
# ifdef DEBUG
2008-06-19 01:52:37 -07:00
NSLog ( @ "Result has multiple actions, popping up an action sheet" ) ;
2008-06-24 09:24:36 -07:00
# endif
2008-06-19 01:52:37 -07:00
UIActionSheet * actionSheet = [ [ UIActionSheet alloc ] initWithFrame : self . view . bounds ] ;
2010-05-07 10:10:06 -07:00
2008-06-19 01:52:37 -07:00
for ( ResultAction * action in self . actions ) {
[ actionSheet addButtonWithTitle : [ action title ] ] ;
}
2010-05-07 10:10:06 -07:00
2008-08-01 06:04:23 -07:00
int cancelIndex = [ actionSheet addButtonWithTitle : NSLocalizedString ( @ "DecoderViewController cancel button title" , @ "Cancel" ) ] ;
2008-06-19 01:52:37 -07:00
actionSheet . cancelButtonIndex = cancelIndex ;
2010-05-07 10:10:06 -07:00
2008-06-19 01:52:37 -07:00
actionSheet . delegate = self ;
2010-05-07 10:10:06 -07:00
2008-06-19 01:52:37 -07:00
[ actionSheet showFromToolbar : self . toolbar ] ;
}
}
- ( void ) actionSheet : ( UIActionSheet * ) actionSheet clickedButtonAtIndex : ( NSInteger ) buttonIndex {
if ( buttonIndex < self . actions . count ) {
int actionIndex = buttonIndex ;
ResultAction * action = [ self . actions objectAtIndex : actionIndex ] ;
2010-05-07 10:10:06 -07:00
[ self performSelector : @ selector ( performAction : )
withObject : action
2008-06-19 01:52:37 -07:00
afterDelay : 0.0 ] ;
}
}
- ( IBAction ) showArchive : ( id ) sender {
ArchiveController * archiveController = [ [ ArchiveController alloc ] initWithDecoderViewController : self ] ;
[ [ self navigationController ] pushViewController : archiveController animated : true ] ;
[ archiveController release ] ;
}
- ( void ) showScan : ( Scan * ) scan {
2008-06-25 06:17:06 -07:00
[ self clearImageView ] ;
2008-06-19 01:52:37 -07:00
[ self presentResultForString : scan . text ] ;
2008-06-24 03:04:08 -07:00
[ [ self navigationController ] popToViewController : self animated : YES ] ;
2008-06-19 01:52:37 -07:00
}
@ end