Thursday, 17 May 2012

Z Bar SDK ( QR Code / bar code reader )

-- Note that Zbar sdk works only for presentmodel or navigation controller push view. if u want to direct open Zbar sdk it wont work.
this is demo for present model view see the red highlighted code


// write it to viewDidLoad
[self ZBarInit]; 

// write to viewWillAppear or where you want to start Zbarsdk
[self readBarCode];
-------------------------------------------------
// write these methods to .m class
- (void)ZBarInit{
    reader = [ZBarReaderViewController new];
   

    reader.readerDelegate =  self;
    reader.supportedOrientationsMask = ZBarOrientationMask(UIInterfaceOrientationPortrait);
   
    #if !(TARGET_IPHONE_SIMULATOR)
// this scanView is custom view.
        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];
}

- (void)readBarCode{
// I added this code because need navigation controller slide animation
    // I write thid code [self presentModalViewController:reader animated:NO]; direc then animation goes down
    double delayInSeconds = 0.1;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [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)
        break;
    // EXAMPLE: do something useful with the barcode data

    NSLog(@"SYsmbol data %@",symbol.data);
    resultText = [CommonFunctions decodeBase64WithStringToString:symbol.data];

    NSLog(@"result text %@",resultText);

    cardType = symbol.typeName;
   
    // EXAMPLE: do something useful with the barcode image
    resultImage.image =[info objectForKey: UIImagePickerControllerOriginalImage];
    if ([cardType isEqualToString:@"QR-Code"]){
        // QR - Code
        if ([resultText hasPrefix:@"Stamp"] || [resultText hasPrefix:@"Reward"]){
            //[self showSaveButton:YES];
            [self btnSavePressed:nil];
        }
        else{
            [self showSaveButton:NO];
           
            cardType = nil;
            resultText = nil;
            resultImage.image = nil;
           
            [[[UIAlertView alloc] initWithTitle:@"Error"
                                        message:@"Invalid Card, Please rescan."
                                       delegate:self cancelButtonTitle:@"Ok"
                              otherButtonTitles:nil] show];
        }
    }
    else{
        //[self showSaveButton:YES];
        resultText = symbol.data;
        [self btnSavePressed:nil];
    }       
   
    // ADD: dismiss the controller (NB dismiss from the *reader*!)
    [reader1 dismissModalViewControllerAnimated:NO];
   
}

No comments:

Post a Comment