Not a member of Pastebin yet?
                        Sign Up,
                        it unlocks many cool features!                    
                - /* This program reads a bar-code in .pgm image format and returns the code.
 - In progress, might be buggy
 - NOTE*< The bar-code format used is EAN >*ETON
 - For a copy of the test file used, go to triclops200.com/files/test.pgm.tar.gz
 - */
 - /////////////////barcode.cpp///////////////////
 - #include <iostream>
 - #include <fstream>
 - #include <string>
 - #include <stdlib.h>
 - #include "barcode.h"
 - #include <map>
 - using namespace std;
 - class Barcode
 - {
 - private:
 - map<string,char> lookup;
 - map<string,char> mapping;
 - int** data_;
 - bool* dataline;
 - bool* bincode;
 - int width;
 - int height;
 - bool** data;
 - string final;
 - int pivot;
 - public:
 - Barcode(string filename,int pivot_){
 - pivot = pivot_;
 - PGM pgm(filename);
 - data_= pgm.getdata();
 - width = pgm.getwidth();
 - height = pgm.getheight();
 - data = new bool*[height];
 - for(int y = 0; y<height;y++){
 - data[y] = new bool[width];
 - for(int x = 0; x<width;x++){
 - data[y][x]=data_[y][x]<pivot;
 - }
 - }
 - consolidate();
 - }
 - void consolidate(){
 - bool ondata=false;
 - bool tfdata=false;
 - dataline= new bool[width];
 - for(int y = 0; y<height;y++){
 - for(int x = 0; x<width;x++){
 - if(ondata){
 - dataline[x]=data[y][x];
 - }
 - if(data[y][x]&&!ondata)
 - tfdata=true;
 - }
 - if(ondata) {
 - y+= height;
 - }
 - if(tfdata){
 - ondata=true;
 - y+=10;
 - }
 - }
 - normalize();
 - }
 - void normalize()
 - {
 - int barlen;
 - int pos[2];
 - bool eof[3] = {1,0,1};
 - bool tri[3];
 - bool startlen = 0;
 - for(int x = 0; x< width;x++)
 - {
 - if(dataline[x]==1&&!startlen){
 - startlen=true;
 - pos[0] = x;
 - }
 - if(dataline[x]==0&&startlen){
 - pos[1]= x;
 - startlen = 0;
 - x+=width;
 - }
 - }
 - barlen = pos[1]-pos[0];
 - for(int x = pos[1];x<width;x++)
 - {
 - if(dataline[x])
 - pos[1] = x;
 - }
 - bincode = new bool[pos[1]-pos[0]];
 - lookup["0001101"] ='0';
 - mapping["0001101"] ='0';
 - lookup["0011001"] ='1';
 - mapping["0011001"] ='0';
 - lookup["0010011"] ='2';
 - mapping["0010011"] ='0';
 - lookup["0111101"] ='3';
 - mapping["0111101"] ='0';
 - lookup["0100011"] ='4';
 - mapping["0100011"] ='0';
 - lookup["0110001"] ='5';
 - mapping["0110001"] ='0';
 - lookup["0101111"] ='6';
 - mapping["0101111"] ='0';
 - lookup["0111011"] ='7';
 - mapping["0111011"] ='0';
 - lookup["0110111"] ='8';
 - mapping["0110111"] ='0';
 - lookup["0001011"] ='9';
 - mapping["0001011"] ='0';
 - lookup["0100111"] ='0';
 - mapping["0100111"] ='1';
 - lookup["0110011"] ='1';
 - mapping["0110011"] ='1';
 - lookup["0011011"] ='2';
 - mapping["0011011"] ='1';
 - lookup["0100001"] ='3';
 - mapping["0100001"] ='1';
 - lookup["0011101"] ='4';
 - mapping["0011101"] ='1';
 - lookup["0111001"] ='5';
 - mapping["0111001"] ='1';
 - lookup["0000101"] ='6';
 - mapping["0000101"] ='1';
 - lookup["0010001"] ='7';
 - mapping["0010001"] ='1';
 - lookup["0001001"] ='8';
 - mapping["0001001"] ='1';
 - lookup["0010111"] ='9';
 - mapping["0010111"] ='1';
 - lookup["1110010"] ='0';
 - mapping["1110010"] ='2';
 - lookup["1100110"] ='1';
 - mapping["1100110"] ='2';
 - lookup["1101100"] ='2';
 - mapping["1101100"] ='2';
 - lookup["1000010"] ='3';
 - mapping["1000010"] ='2';
 - lookup["1011100"] ='4';
 - mapping["1011100"] ='2';
 - lookup["1001110"] ='5';
 - mapping["1001110"] ='2';
 - lookup["1010000"] ='6';
 - mapping["1010000"] ='2';
 - lookup["1000100"] ='7';
 - mapping["1000100"] ='2';
 - lookup["1001000"] ='8';
 - mapping["1001000"] ='2';
 - lookup["1110100"] ='9';
 - mapping["1110100"] ='2';
 - mapping["000000"]='0';
 - mapping["001011"]='1';
 - mapping["001101"]='2';
 - mapping["001110"]='3';
 - mapping["010011"]='4';
 - mapping["011001"]='5';
 - mapping["011100"]='6';
 - mapping["010101"]='7';
 - mapping["010110"]='8';
 - mapping["011010"]='9';
 - for(int x = pos[0];x<pos[1];x+=barlen){
 - int v = (x-pos[0])/barlen;
 - bincode[v]=dataline[x];
 - }
 - int length = (pos[1]-pos[0]+barlen)/barlen;
 - int consta = 3;
 - string str1="";
 - string str2="";
 - final = "";
 - for(int f = 0; f < 12; f++){
 - for(int g = f*7+consta;g<((f+1)*7)+consta;g++){
 - str1+=(char)((int)bincode[g]+48);
 - }
 - final+=lookup[str1];
 - if(f<6)
 - str2+=mapping[str1];
 - str1="";
 - if(f==5){
 - consta+=5;
 - }
 - }
 - final=mapping[str2]+final;
 - }
 - string getstring()
 - {
 - return final;
 - }
 - ~Barcode()
 - {
 - for(int y = 0; y<height;y++)
 - delete[] data[y];
 - delete[] dataline;
 - delete[] bincode;
 - }
 - };
 - ////////////pgm.cpp//////////////
 - #include <iostream>
 - #include <fstream>
 - #include <string>
 - #include <stdlib.h>
 - using namespace std;
 - class PGM{
 - private:
 - int width;
 - int** data;
 - int height;
 - int baseline;
 - bool binopen(ifstream& file_h,string filename){
 - int pos =file_h.tellg();
 - ifstream file;
 - file.open(filename.c_str(),ios::binary);
 - file.seekg(pos);
 - data = new int*[height];
 - for(int y = 0; y< height;y++){
 - data[y] = new int[width];
 - for(int x = 0; x< width; x++){
 - char buff[2];
 - file.read(buff,1);
 - if((int)buff[0]<0)
 - data[y][x]=(int)buff[0]+baseline+1;
 - else
 - data[y][x]=buff[0];
 - }
 - }
 - file.close();
 - }
 - bool asciiopen(ifstream & file,string filename){
 - }
 - bool startfile(string filename){
 - bool bin;
 - int dim[2];
 - int x=0;
 - string tmp;
 - string temp = "";
 - ifstream file_h;
 - file_h.open(filename.c_str());
 - getline(file_h,tmp);
 - bin = tmp=="P5";
 - getline(file_h,tmp);
 - getline(file_h,tmp);
 - for(int i = 0; i< tmp.length();i++){
 - if(tmp[i]==' ') {
 - i++;
 - dim[x]=atoi(temp.c_str());
 - x++;
 - temp="";
 - }
 - temp+=tmp[i];
 - }
 - dim[x]=atoi(temp.c_str());
 - width = dim[0];
 - height = dim[1];
 - getline(file_h,tmp);
 - baseline=atoi(tmp.c_str());
 - if(bin){
 - binopen(file_h,filename);
 - }else{
 - asciiopen(file_h,filename);
 - }
 - file_h.close();
 - }
 - public:
 - PGM(string filename){
 - startfile(filename);
 - }
 - ~PGM(){
 - for(int y = 0;y<height;y++){
 - delete [] data[y];
 - }
 - }
 - int** getdata()
 - {
 - return data;
 - }
 - int getheight()
 - {return height;}
 - int getwidth()
 - {return width;}
 - };
 - //////////////////barcode.h////////////////
 - #ifndef _barcode_h
 - #define _barcode_h
 - #include "pgm.cpp"
 - #endif
 - ///////////////////main.cpp////////////////
 - #include <iostream>
 - #include <fstream>
 - #include "barcode.h"
 - #include "barcode.cpp"
 - using namespace std;
 - int main(int argc, char* argv[]) {
 - Barcode barcode(argv[1],20);
 - cout<<barcode.getstring()<<endl;
 - return 0;
 - }
 - //////////////////makefile/////////////////
 - all:out
 - out:main.o pgm.o
 - g++ main.o pgm.o -o out -O2
 - main.o: main.cpp barcode.h pgm.o barcode.o
 - g++ -c main.cpp -o main.o
 - pgm.o: pgm.cpp
 - g++ -c pgm.cpp -o pgm.o
 - barcode.o: barcode.cpp pgm.cpp
 - g++ -c barcode.cpp -o barcode.o
 
Advertisement
 
                    Add Comment                
                
                        Please, Sign In to add comment