Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Convert string into barcode image

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 761
    Comment on it

    Hi Readers,

    This blog includes a very simple example to convert a string into barcode image. As in one of my previous blogs I have explained how to read barcode with the help of iPhone camera and now in this blog I will explain how those barcodes will be created.  Before moving towards the coding part we will design the user interface for the same as given in the below screenshot:-

     

     

     

     

    As given in the above screenshot view controller includes one textfield to take input string from the user, a label is there to display a message if the user will not enter any string and click the button, click button is there which will create the barcode image according to the string entered by the user. 

    Now we will move towards the coding part of the application. First of all, we should declare all the strings which we are going to use further in the application. The separate file for string declaration and definition is required to make an application easier to understand and only by importing the file we can use all strings.

     

    StringDeclaration.h
    
    #import <Foundation/Foundation.h>
    
    @interface StringDeclaration : NSObject
    extern NSString *const blankString,*const messageString,*const inputMessage,*const filterName;
    @end
    
    StringDeclaration.m
    
    #import "StringDeclaration.h"
    
    @implementation StringDeclaration
    NSString *const blankString=@"",*const messageString=@"Please enter string to check bar code image",*const inputMessage=@"inputMessage",*const filterName=@"CIQRCodeGenerator";
    @end

     

     

    Now the view controller coding where the whole concept of converting string into barcode is implemented is given below:-

    ViewController.h
    
    #import <UIKit/UIKit.h>
    #import "StringDeclaration.h"
    @interface ViewController : UIViewController
    @property (weak, nonatomic) IBOutlet UITextField *txtEnterString; // textfeild outlet
    @property (weak, nonatomic) IBOutlet UILabel *lblMessage;  // label outlet
    
    @property (weak, nonatomic) IBOutlet UIImageView *imgView; // imageview outlet
    - (IBAction)btnBarCode:(id)sender;    // button click action
    
    @end
    
    ViewController.m
    
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    
    - (IBAction)btnBarCode:(id)sender {
          [self createBarCodeForString:_txtEnterString.text]; // calling
        _txtEnterString.text=blankString;
    }
    
    
    - (CIImage *)createBarCodeForString:(NSString *)barcodeString {
        NSData *stringData = [barcodeString dataUsingEncoding: NSISOLatin1StringEncoding];
        
        CIFilter *qrFilter = [CIFilter filterWithName:filterName];
        [qrFilter setValue:stringData forKey:inputMessage];
       
        CIImage *displayImage = qrFilter.outputImage;
    
        CIContext *context = [CIContext contextWithOptions:nil];
        if ([_txtEnterString.text isEqualToString:blankString]) {
    
            _lblMessage.text=messageString;
            _imgView.hidden=YES;
            
        }else {
            // We got output image. Display it.
               _imgView.hidden=NO;
            _imgView.image = [UIImage imageWithCGImage:[context createCGImage:displayImage fromRect:displayImage.extent]];
            _lblMessage.text=blankString;
            
        }
        return qrFilter.outputImage;
    }
    @end

     

     

    Output:-

    Output of the application will be shown in this way as given in screenshots below:-

    First, the screen will be visible in this way as given below. It includes one textfield for taking input from the user and one button to generate barcode image from user entered string.

     

     

     

    Suppose user not enter any string and directly click on the button then this message will be displayed with the help of label taken between the button and textfield.

     

     

     

    Now user entered a string as "Helo World" in the textfield and after button click data entered by user will get removed and imageview will display the barcode image of string entered by the user.

     

     

     

    This screenshot includes the barcode image of "Helo World" entered by the user.

     

     

     

    If the user enters another string as "Hi this is a new day" so the barcode image for this string will be different from the previous string entered by the user.

     

     

     

    This is the output of the string "Hi this is a new day" entered by the user.

     

     

    You can also download the project with the help of link given below:-

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: