Advertisement
triclops200

Barcodescanner V1.1

Jul 2nd, 2011
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.21 KB | None | 0 0
  1. /*  This program reads a bar-code in .pgm image format and returns the code.
  2.  In progress, might be buggy
  3. NOTE*< The bar-code format used is EAN >*ETON
  4. For a copy of the test file used, go to triclops200.com/files/test.pgm.tar.gz
  5. */
  6.  
  7. /////////////////barcode.cpp///////////////////
  8. #include <iostream>
  9. #include <fstream>
  10. #include <string>
  11. #include <stdlib.h>
  12. #include "barcode.h"
  13. #include <map>
  14. using namespace std;
  15. class Barcode
  16. {
  17.     private:
  18.         map<string,char> lookup;
  19.         map<string,char> mapping;      
  20.         int** data_;
  21.         bool* dataline;
  22.         bool* bincode;
  23.         int width;
  24.         int height;
  25.         bool** data;
  26.        
  27.         string final;
  28.         int pivot;
  29.     public:
  30.         Barcode(string filename,float pivot_){
  31.            
  32.             PGM pgm(filename);
  33.             data_= pgm.getdata();
  34.             width = pgm.getwidth();
  35.             height = pgm.getheight();
  36.             pivot = pivot_*pgm.getbaseline();
  37.             data = new bool*[height];
  38.             for(int y = 0; y<height;y++){
  39.                 data[y] = new bool[width];
  40.                 for(int x = 0; x<width;x++){
  41.                     data[y][x]=data_[y][x]<pivot;
  42.                 }
  43.             }
  44.             consolidate();
  45.         }
  46.         void consolidate(){
  47.             bool ondata=false;
  48.             bool tfdata=false;
  49.             dataline= new bool[width];
  50.             for(int y = 0; y<height;y++){
  51.                 for(int x = 0; x<width;x++){
  52.                     if(ondata){
  53.                         dataline[x]=data[y][x];
  54.                     }
  55.                     if(data[y][x]&&!ondata)
  56.                         tfdata=true;
  57.                 }
  58.                 if(ondata)  {
  59.                     y+= height;
  60.                 }
  61.                 if(tfdata){
  62.                     ondata=true;
  63.                     y+=10;
  64.                 }          
  65.             }
  66.             normalize();
  67.         }
  68.         void normalize()
  69.         {
  70.             int barlen;
  71.             int pos[2];
  72.             bool eof[3] = {1,0,1};
  73.             bool tri[3];
  74.             bool startlen = 0;
  75.  
  76.             for(int x = 0; x< width;x++)
  77.             {
  78.                 if(dataline[x]==1&&!startlen){
  79.                     startlen=true;
  80.                     pos[0] = x;
  81.                 }
  82.                 if(dataline[x]==0&&startlen){
  83.                     pos[1]= x;
  84.                     startlen = 0;
  85.                     x+=width;
  86.                 }      
  87.             }
  88.             barlen = pos[1]-pos[0];
  89.             for(int x = pos[1];x<width;x++)
  90.             {
  91.                 if(dataline[x])
  92.                     pos[1]  = x;
  93.             }
  94.             bincode = new bool[pos[1]-pos[0]];
  95.             lookup["0001101"] ='0';
  96.             mapping["0001101"] ='0';
  97.             lookup["0011001"] ='1';
  98.             mapping["0011001"] ='0';
  99.             lookup["0010011"] ='2';
  100.             mapping["0010011"] ='0';
  101.             lookup["0111101"] ='3';
  102.             mapping["0111101"] ='0';
  103.             lookup["0100011"] ='4';
  104.             mapping["0100011"] ='0';               
  105.             lookup["0110001"] ='5';
  106.             mapping["0110001"] ='0';
  107.             lookup["0101111"] ='6';
  108.             mapping["0101111"] ='0';
  109.             lookup["0111011"] ='7';
  110.             mapping["0111011"] ='0';
  111.             lookup["0110111"] ='8';
  112.             mapping["0110111"] ='0';
  113.             lookup["0001011"] ='9';
  114.             mapping["0001011"] ='0';
  115.             lookup["0100111"] ='0';
  116.             mapping["0100111"] ='1';
  117.             lookup["0110011"] ='1';
  118.             mapping["0110011"] ='1';
  119.             lookup["0011011"] ='2';
  120.             mapping["0011011"] ='1';
  121.             lookup["0100001"] ='3';
  122.             mapping["0100001"] ='1';
  123.             lookup["0011101"] ='4';
  124.             mapping["0011101"] ='1';               
  125.             lookup["0111001"] ='5';
  126.             mapping["0111001"] ='1';
  127.             lookup["0000101"] ='6';
  128.             mapping["0000101"] ='1';
  129.             lookup["0010001"] ='7';
  130.             mapping["0010001"] ='1';
  131.             lookup["0001001"] ='8';
  132.             mapping["0001001"] ='1';
  133.             lookup["0010111"] ='9';
  134.             mapping["0010111"] ='1';
  135.             lookup["1110010"] ='0';
  136.             mapping["1110010"] ='2';
  137.             lookup["1100110"] ='1';
  138.             mapping["1100110"] ='2';
  139.             lookup["1101100"] ='2';
  140.             mapping["1101100"] ='2';
  141.             lookup["1000010"] ='3';
  142.             mapping["1000010"] ='2';
  143.             lookup["1011100"] ='4';
  144.             mapping["1011100"] ='2';               
  145.             lookup["1001110"] ='5';
  146.             mapping["1001110"] ='2';
  147.             lookup["1010000"] ='6';
  148.             mapping["1010000"] ='2';
  149.             lookup["1000100"] ='7';
  150.             mapping["1000100"] ='2';
  151.             lookup["1001000"] ='8';
  152.             mapping["1001000"] ='2';
  153.             lookup["1110100"] ='9';
  154.             mapping["1110100"] ='2';
  155.             mapping["000000"]='0';
  156.             mapping["001011"]='1';
  157.             mapping["001101"]='2';
  158.             mapping["001110"]='3';
  159.             mapping["010011"]='4';
  160.             mapping["011001"]='5';
  161.             mapping["011100"]='6';
  162.             mapping["010101"]='7';
  163.             mapping["010110"]='8';
  164.             mapping["011010"]='9';  
  165.             for(int x = pos[0];x<pos[1];x+=barlen){
  166.                 int v = (x-pos[0])/barlen;
  167.                 bincode[v]=dataline[x];
  168.  
  169.             }
  170.             int length = (pos[1]-pos[0]+barlen)/barlen;
  171.             int consta = 3;
  172.             string str1="";
  173.             string str2="";
  174.             final = "";
  175.             for(int f = 0; f < 12; f++){
  176.                 for(int g = f*7+consta;g<((f+1)*7)+consta;g++){
  177.                     str1+=(char)((int)bincode[g]+48);
  178.                 }
  179.                 final+=lookup[str1];
  180.                 if(f<6)
  181.                     str2+=mapping[str1];
  182.                 str1="";
  183.                 if(f==5){
  184.                     consta+=5;
  185.                 }
  186.             }
  187.             final=mapping[str2]+final;  
  188.         }
  189.         string getstring()
  190.         {
  191.             return final;
  192.         }
  193.         ~Barcode()
  194.         {
  195.             for(int y = 0; y<height;y++)
  196.                 delete[] data[y];
  197.             delete[] dataline;
  198.             delete[] bincode;
  199.         }
  200. };
  201. ////////////pgm.cpp//////////////
  202. #include <iostream>
  203. #include <fstream>
  204. #include <string>
  205. #include <stdlib.h>
  206. using namespace std;
  207. class PGM{
  208.     private:
  209.         int width;
  210.         int** data;
  211.         int height;
  212.         int baseline;
  213.         bool binopen(ifstream& file_h,string filename){
  214.             int pos =file_h.tellg();
  215.             ifstream file;
  216.             file.open(filename.c_str(),ios::binary);
  217.             file.seekg(pos);
  218.             data = new int*[height];
  219.             for(int y = 0; y< height;y++){
  220.                 data[y] = new int[width];
  221.                 for(int x = 0; x< width; x++){
  222.                     char buff[2];
  223.                     file.read(buff,1);
  224.                     if((int)buff[0]<0)
  225.                         data[y][x]=(int)buff[0]+baseline+1;
  226.                     else
  227.                         data[y][x]=buff[0];
  228.                    
  229.                 }      
  230.             }
  231.             file.close();
  232.         }
  233.         bool asciiopen(ifstream & file,string filename){
  234.         }
  235.         bool startfile(string filename){
  236.             bool bin;
  237.             int dim[2];
  238.             int x=0;
  239.             string tmp;
  240.             string temp = "";
  241.             ifstream file_h;
  242.             file_h.open(filename.c_str());
  243.             getline(file_h,tmp);
  244.             bin = tmp=="P5";
  245.             getline(file_h,tmp);
  246.             getline(file_h,tmp);
  247.             for(int i = 0; i< tmp.length();i++){
  248.                 if(tmp[i]==' ') {
  249.                     i++;
  250.                     dim[x]=atoi(temp.c_str());
  251.                     x++;
  252.                     temp="";                   
  253.                 }
  254.                 temp+=tmp[i];
  255.             }
  256.             dim[x]=atoi(temp.c_str());
  257.             width = dim[0];
  258.             height = dim[1];
  259.             getline(file_h,tmp);
  260.             baseline=atoi(tmp.c_str());
  261.  
  262.             if(bin){
  263.                 binopen(file_h,filename);
  264.             }else{
  265.                 asciiopen(file_h,filename);
  266.             }
  267.             file_h.close();
  268.         }  
  269.     public:
  270.  
  271.         PGM(string filename){
  272.             startfile(filename);
  273.         }
  274.         ~PGM(){
  275.             for(int y = 0;y<height;y++){
  276.                 delete [] data[y];
  277.             }
  278.         }
  279.         int** getdata()
  280.         {
  281.         return data;
  282.             }
  283.             int getheight()
  284.             {return height;}
  285.             int getwidth()
  286.             {return width;}
  287.             int getbaseline()
  288.             {return baseline;}
  289. };
  290. //////////////////barcode.h////////////////
  291. #ifndef _barcode_h
  292. #define _barcode_h
  293. #include "pgm.cpp"
  294. #endif
  295. ///////////////////main.cpp////////////////
  296. #include <iostream>
  297. #include <fstream>
  298. #include "barcode.h"
  299. #include "barcode.cpp"
  300. using namespace std;
  301. int main(int argc, char* argv[]) {
  302.    try{
  303.     Barcode barcode(argv[1],.1);
  304.    cout<<barcode.getstring()<<endl;
  305.    }
  306.    catch(char* str)
  307.    {
  308.    cout<<"An error occured, please make sure you are using ./out <filepath>,\n the file is a binary (P5) pgm file, \nand the barcode image is of good quality (not tilted or highly shaded).";
  309.        }
  310.    return 0;
  311. }
  312. //////////////////makefile/////////////////
  313. all:out
  314. out:main.o pgm.o
  315.     g++ main.o pgm.o -o out -O2
  316. main.o: main.cpp barcode.h pgm.o barcode.o
  317.     g++ -c main.cpp -o main.o
  318. pgm.o: pgm.cpp
  319.     g++ -c pgm.cpp -o pgm.o
  320. barcode.o: barcode.cpp pgm.cpp
  321.     g++ -c barcode.cpp -o barcode.o
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement