Friday, 30 March 2012

Scan Bar Code / QR Code ZBarSDk in iPhone

Zxing used in Android for this purpose and in iPhone use ZBarSDK

Custom ZBarSDK
 1.  Make a UIView object (called scanView) design it as u want to customize.

 2.  make sure that where you want camera region to scan code need to be transparent.
 3. Make object of ZBarReaderViewController  (called reader)
 3. set reader.cameraOverlayView to this view. (reader.cameraOverlayView = scanView;)
 4. If need Orientation specfic use
 reader.supportedOrientationsMask = ZBarOrientationMask(UIInterfaceOrientationPortrait);

Detail Code :>

Whole Code
// Name : AddCardViewController.h

#import <UIKit/UIKit.h>
#import "ZBarSDK.h"

@interface AddCardViewController : UIViewController<ZBarReaderDelegate, UITextFieldDelegate, UIScrollViewDelegate>
{
ZBarReaderViewController *reader;
}
@property (nonatomic, strong) IBOutlet UIView *scanView;
@property (nonatomic, strong) IBOutlet UIImageView *resultImage;
@property (nonatomic, strong) IBOutlet UITextField *resultText;
@property (nonatomic, strong) IBOutlet UIButton *wontScanButton;
@property (nonatomic, strong) NSString *cardType;

@property (nonatomic, strong) IBOutlet UIToolbar *toolBarScanPage;

- (void) readBarCode;
- (void) showSaveButton;

- (IBAction)btnSavePressed:(id)sender;
- (IBAction)scanBackBarButtonPressed:(id)sender;
- (IBAction)wontScanBtnPressed:(id)sender;
- (IBAction)enterBarCodeManually:(id)sender;
- (IBAction)enterBarCodeManuallyViaModelView:(id)sender;
@end
----------------------

// Name : AddCardViewController.m
 #import "AddCardViewController.h"
#import "NewRewardCardViewController.h"

@implementation AddCardViewController

@synthesize resultImage,resultText,cardType;
@synthesize  scanView;
@synthesize wontScanButton;
@synthesize toolBarScanPage;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
  
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
  
  
    //self.resultText.text = @"";
  
    // Navigation bar right side save
    if (![Validate isNull:cardType]) {
        [self showSaveButton];
    }
}

- (void)viewDidLoad{
    [super viewDidLoad];
    [dicGlobal setObject:self.navigationController forKey:@"navCont"];
    self.navigationItem.leftBarButtonItem= [CommonFunctions getBackBtn];
    self.navigationController.navigationBarHidden = NO;
    self.title=@"Scan Card";
  
  
    //[toolBarScanPage drawRect:CGRectMake(0, 0, 320, 40)];
    [self readBarCode];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - IBAction Methods
- (IBAction)btnSavePressed:(id)sender{
    if (![Validate isNull:cardType]) {
        if ([cardType isEqualToString:@"QR-Code"]){
            // QR - Code for  stamp cards
            [CommonFunctions AlertTitle:@"QR_CODE" withMsg:@"Stamp Card"];
        }
        else{
            //Bar code for reward cards (cardType == EAN-13)
            NewRewardCardViewController *nrcvc= [NewRewardCardViewController new];
            nrcvc.barCodeValue = resultText.text;
            nrcvc.barCodeImageDataValue = [[NSData alloc] initWithData:UIImageJPEGRepresentation(resultImage.image, 90)];
            [nrcvc isBarCodeImageAvailable:YES];
            [self.navigationController pushViewController:nrcvc animated:YES];
        } 
    }
}
-(IBAction)scanBackBarButtonPressed:(id)sender{
    if ([Validate isNull:cardType]) {
        [self.navigationController dismissModalViewControllerAnimated:NO];
        [self.navigationController popViewControllerAnimated:YES];
    }
    else{
        [self.navigationController dismissModalViewControllerAnimated:NO];
    }
  
}

- (IBAction)wontScanBtnPressed:(id)sender{
    [self readBarCode];
}

- (IBAction)enterBarCodeManually:(id)sender{
    NewRewardCardViewController *nrcvc= [NewRewardCardViewController new];
    [nrcvc isBarCodeImageAvailable:NO];
    [self.navigationController pushViewController:nrcvc animated:YES];
}

- (IBAction)enterBarCodeManuallyViaModelView:(id)sender{
    [reader dismissModalViewControllerAnimated:NO];
  
    NewRewardCardViewController *nrcvc= [NewRewardCardViewController new];
    [nrcvc isBarCodeImageAvailable:NO];
    [self.navigationController pushViewController:nrcvc animated:YES];
}

#pragma mark - Util Methods

- (void) showSaveButton{
    // save button on navigation bar right side
    UIButton *btnNext=[UIButton buttonWithType:UIButtonTypeCustom];
    [btnNext setFrame:CGRectMake(280, 0, 64, 32)];
    [btnNext setImage:[UIImage imageNamed:@"SaveBtn_17"] forState:UIControlStateNormal];
    [btnNext addTarget:self action:@selector(btnSavePressed:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *btnRight=[[UIBarButtonItem alloc] initWithCustomView:btnNext];
    self.navigationItem.rightBarButtonItem=btnRight;
  
}

- (void) readBarCode{
    reader = [ZBarReaderViewController new];
  
    reader.readerDelegate =  self;
    reader.supportedOrientationsMask = ZBarOrientationMask(UIInterfaceOrientationPortrait);
  
    #if !(TARGET_IPHONE_SIMULATOR)  
        reader.cameraOverlayView = scanView;
        reader.showsZBarControls = NO;
    #endif
    //[[UIApplication sharedApplication] setStatusBarHidden:NO];
    reader.wantsFullScreenLayout = NO;
    ZBarImageScanner *scanner = reader.scanner;
  
    [scanner setSymbology:ZBAR_I25 config: ZBAR_CFG_ENABLE to: 0];
    [NSTimer scheduledTimerWithTimeInterval:0.2f target:self selector:@selector(presentModal) userInfo:nil repeats:NO];
  
}

-(void) presentModal
{
    [self presentModalViewController:reader animated:NO];
}

#pragma mark - ZBarReaderDelegate
- (void) imagePickerController:(UIImagePickerController*)reader1 didFinishPickingMediaWithInfo:(NSDictionary*)info{
    // ADD: get the decode results
    id<NSFastEnumeration> results =
    [info objectForKey: ZBarReaderControllerResults];
    ZBarSymbol *symbol = nil;
    for(symbol in results)
        // EXAMPLE: just grab the first barcode
        break;
  
    // EXAMPLE: do something useful with the barcode data
    resultText.text = symbol.data;
    //resultText.hidden = NO;
    NSLog(@"%@",resultText.text);
    cardType = symbol.typeName;
  
    // EXAMPLE: do something useful with the barcode image
    resultImage.image =[info objectForKey: UIImagePickerControllerOriginalImage];
    [self showSaveButton];
  
    // ADD: dismiss the controller (NB dismiss from the *reader*!)
    [reader1 dismissModalViewControllerAnimated:NO];
    //[self.navigationController popViewControllerAnimated:NO];
}
@end


No comments:

Post a Comment