圖像轉換成文本

轉自:http://blog.csdn.net/smells2/article/details/7338011

===============================================

此程序只能處理真彩色的bmp格式文件,如果沒有bmp格式的圖片可以使用windows自帶的畫圖工具轉換一下。

 

Image.h

  1. #pragma once  
  2.   
  3. #include "resource.h"  
  4.   
  5.   
  6. class CBitMapFile  
  7. {  
  8. protected:  
  9.     //位圖文件信息頭  
  10.     BITMAPFILEHEADER m_file_header;  
  11.     //位圖信息頭  
  12.     BITMAPINFOHEADER m_info_header;  
  13.   
  14.     CFile m_hFile;  
  15.     //存儲位圖數據  
  16.     BYTE* m_imageArray;  
  17.     //去掉填充的無用字節,存儲剩下的純數據  
  18.     BYTE* m_realImageArray;  
  19. public:  
  20.     //整個文件大小  
  21.     DWORD m_sizeFile;  
  22.     //位圖數據的大小  
  23.     DWORD m_sizeImage;  
  24.     //文件頭所佔字節數  
  25.     DWORD m_offBits;  
  26.     //圖像的寬,即行  
  27.     LONG m_width;  
  28.     //圖像的高,即列  
  29.     LONG m_height;  
  30.     //一個像素有多少比特表示  
  31.     WORD m_bitCount;  
  32.     //每個像素的所佔字節數  
  33.     int m_bytePixel;  
  34.     //文件的填充字節  
  35.     int m_addByte;  
  36.     //文件名  
  37.     CString m_fileName;  
  38.   
  39. public:  
  40.   
  41.     CBitMapFile():m_imageArray(NULL),m_realImageArray(NULL),m_sizeFile(0),m_sizeImage(0),  
  42.                 m_offBits(0),m_width(0),m_height(0),m_bitCount(0),m_bytePixel(0),m_addByte(0)  
  43.                 ,m_fileName(""){}  
  44.   
  45.     ~CBitMapFile()  
  46.     {  
  47.         delete m_realImageArray;  
  48.         delete m_imageArray;  
  49.           
  50.     }  
  51.   
  52.     int openfile(CString& str,int fileMode);  
  53.     int turn2Gray();  
  54.     int turn2Txt(CString txt_name);  
  55.       
  56. };  


 

Image.cpp

  1. #include "stdafx.h"  
  2. #include "Image.h"  
  3. /********************************** 
  4.  *打開bmp格式文件,並初始化數據成員* 
  5.  **********************************/  
  6. int CBitMapFile::openfile(CString& str,int fileMode)  
  7. {  
  8.     if(!m_hFile.Open(str,fileMode))  
  9.     {  
  10.   
  11.         return -1;  
  12.     }  
  13.     m_fileName = m_hFile.GetFileName();  
  14.   
  15.     m_hFile.Read(&m_file_header,sizeof(BITMAPFILEHEADER));  
  16.     m_sizeFile = m_file_header.bfSize;  
  17.     m_offBits = m_file_header.bfOffBits;  
  18.   
  19.     m_hFile.Read(&m_info_header,sizeof(BITMAPINFOHEADER));  
  20.     m_width = m_info_header.biWidth;  
  21.     m_height = m_info_header.biHeight;  
  22.     m_bitCount = m_info_header.biBitCount;  
  23.     m_bytePixel = m_bitCount>>3;  
  24.     m_sizeImage =((((m_width * m_bitCount) + 31) & ~31) / 8) * m_height;  
  25.     m_addByte = (4-(m_width*m_bytePixel)%4)%4;  
  26.   
  27.     m_imageArray = new BYTE[m_sizeImage];  
  28.     memset(m_imageArray,0,m_sizeImage);  
  29.     m_hFile.Read(m_imageArray,m_sizeImage*sizeof(BYTE));  
  30.   
  31.     m_realImageArray = new BYTE[m_width*m_height];  
  32.     memset(m_realImageArray,0,m_width*m_height);  
  33.       
  34.     return 1;  
  35. }  
  36. /******************************************** 
  37.  *將文件轉換成灰度圖像,再對圖像做歸一化處理* 
  38.  ********************************************/  
  39. int CBitMapFile::turn2Gray()  
  40. {  
  41.       
  42.   
  43.   
  44.   
  45.     if (m_imageArray == NULL)  
  46.     {  
  47.         return -2;  
  48.     }  
  49.       
  50.     for (int i =0,j= 0;i<m_sizeImage;)  
  51.     {  
  52.         //轉換爲灰度圖像,注意填充的字節  
  53.         BYTE blue,green,red,gray;  
  54.   
  55.         blue = m_imageArray[i];  
  56.         green = m_imageArray[i+1];  
  57.         red = m_imageArray[i+2];  
  58.   
  59.         gray = (BYTE)(0.3*red+0.59*green+0.11*blue);  
  60.   
  61.         m_imageArray[i] = gray;  
  62.         m_imageArray[i+1] = gray;  
  63.         m_imageArray[i+2] = gray;  
  64.   
  65.         m_realImageArray[j] = m_imageArray[i]>>5;  
  66.   
  67.         i += 3;  
  68.         j++;  
  69.   
  70.         ////跳過填充字節  
  71.         if (j % m_width == 0)  
  72.         {  
  73.             i += m_addByte;  
  74.         }  
  75.           
  76.     }  
  77.       
  78.     return 1;  
  79. }  
  80. /****************************************** 
  81.  *對圖像的每個像素做文本映射,最終寫入文件* 
  82.  ******************************************/  
  83. int CBitMapFile::turn2Txt(CString txt_name)  
  84. {  
  85.     char* _txtBuf = new char[m_width*m_height+2*m_height];  
  86.     memset(_txtBuf,0,m_width*m_height+2*m_height);  
  87.   
  88.     //文本映射  
  89.     char txtMap[8] = {'@','$','#','%','!','~','^','`'};  
  90.   
  91.     char* _buf = new char[m_width+2];  
  92.     memset(_buf,0,m_width+2);  
  93.     TRACE(_T("\'\\r\'=%x,\'\\n\'=%x"),'\r','\n');  
  94.     for (int i = m_height-1;i>=0;i--)  
  95.     {  
  96.         for (int j = 0;j<m_width;j++)  
  97.         {  
  98.             _buf[j] = txtMap[m_realImageArray[i*m_width+j]];  
  99.               
  100.         }  
  101.           
  102.         _buf[m_width] = '\r';  
  103.         _buf[m_width+1] = '\n';  
  104.           
  105.         for (int k=0;k<m_width+2;k++)  
  106.         {  
  107.             _txtBuf[(m_height-1-i)*m_width+k+(m_height-1-i)*2] = _buf[k];  
  108.           
  109.         }  
  110.     }  
  111.       
  112.       
  113.     CFile txtFile;  
  114.     if (txtFile.Open(txt_name,CFile::modeCreate|CFile::modeNoInherit|CFile::modeWrite)==FALSE)  
  115.     {  
  116.         return -1;  
  117.     }  
  118.       
  119.     txtFile.Write(_txtBuf,sizeof(char)*(m_width*m_height+2*m_height));  
  120.     txtFile.Close();  
  121.     delete _txtBuf;  
  122.     delete _buf;  
  123.     return 1;  
  124. }  


 

main.cpp

  1. // Image.cpp : 定義控制檯應用程序的入口點。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include "Image.h"  
  6. #include <iostream>  
  7. #ifdef _DEBUG  
  8. #define new DEBUG_NEW  
  9. #endif  
  10.   
  11.   
  12. // 唯一的應用程序對象  
  13.   
  14. CWinApp theApp;  
  15.   
  16. using namespace std;  
  17.   
  18. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])  
  19. {  
  20.     int nRetCode = 0;  
  21.   
  22.     // 初始化 MFC 並在失敗時顯示錯誤  
  23.     if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))  
  24.     {  
  25.         // TODO: 更改錯誤代碼以符合您的需要  
  26.         _tprintf(_T("錯誤: MFC 初始化失敗\n"));  
  27.         nRetCode = 1;  
  28.     }  
  29.     else  
  30.     {  
  31.         // TODO: 在此處爲應用程序的行爲編寫代碼。  
  32.         if (argc<3)  
  33.         {  
  34.             std::cout<<"you should input like this:Image.exe (c:\\sourceFile.bmp) (c:\\goalFile.txt)\n"<<std::endl;  
  35.             return -1;  
  36.         }  
  37.   
  38.         CBitMapFile bitFile;  
  39.         CString arg1 = argv[1];  
  40.         CString str = argv[2];  
  41.         if (bitFile.openfile(arg1,CFile::modeRead) != 1)  
  42.         {  
  43.             std::cout<<"ERROR:sourceFile.bmp open failed."<<std::endl;  
  44.             return -1;  
  45.         }  
  46.           
  47.         if(bitFile.turn2Gray() != 1)  
  48.         {  
  49.             std::cout<<"ERROR:turn2Gray function failed."<<std::endl;  
  50.             return -1;  
  51.         }  
  52.           
  53.         if (bitFile.turn2Txt(str) != 1)  
  54.         {  
  55.             std::cout<<"ERROR:turn2Txt function failed."<<std::endl;  
  56.             return -1;  
  57.         }  
  58.           
  59.   
  60.     }  
  61.   
  62.     return nRetCode;  
  63. }  




發佈了28 篇原創文章 · 獲贊 6 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章