把一张bmp格式图片转化成二维数组并写入txt文件,黑色像素设为1,白色像素设为0,红色设为2,绿色设为3.
最好用vc++!谢谢!

解决方案 »

  1.   

    参考图像转换成字符
    void CPictureToTextDlg::OnConvert() 
    {
    //BITMAPINFOHEADER infoheader;
    CPaintDC dc(this);
    long* m_pbmpdata;
    long* p_tmp;
    BITMAPINFO *m_pbmpinfo,m_bmp;

    HBITMAP hbmp=(HBITMAP)LoadImage(::AfxGetResourceHandle(),"bitmap.bmp",
    IMAGE_BITMAP,0, 0,LR_DEFAULTCOLOR|LR_LOADFROMFILE);
    m_bmp.bmiHeader.biSize=sizeof(m_bmp.bmiHeader);
    m_bmp.bmiHeader.biBitCount=0; int bmpWidth;
    int bmpHeight;
    GetDIBits(dc.GetSafeHdc(),hbmp,0,1,NULL,&m_bmp,DIB_RGB_COLORS);
    bmpWidth=m_bmp.bmiHeader.biWidth;
    bmpHeight=m_bmp.bmiHeader.biHeight;

    p_tmp=(long*)malloc(bmpWidth*bmpHeight*m_bmp.bmiHeader.biBitCount+sizeof(BITMAPINFO));
    m_pbmpinfo=(BITMAPINFO*)p_tmp;
    m_pbmpdata=p_tmp+sizeof(BITMAPINFO);
    memcpy(p_tmp,(const void*)&m_bmp,sizeof(BITMAPINFOHEADER));
    GetDIBits(dc.GetSafeHdc(),hbmp,0,m_bmp.bmiHeader.biHeight,m_pbmpdata,
    m_pbmpinfo,DIB_RGB_COLORS);
    /*
    BITMAPINFOHEADER RGB32BITSBITMAPINFO=
    {sizeof(BITMAPINFOHEADER),m_bmp.bmiHeader.biWidth,m_bmp.bmiHeader.biHeight,
    1,32,BI_RGB,0,0,0,0};
    SetDIBitsToDevice(dc.GetSafeHdc(),10,10,bmpWidth,
    bmpHeight,0,0,0,bmpHeight,
    m_pbmpdata,(LPBITMAPINFO)&RGB32BITSBITMAPINFO,DIB_RGB_COLORS);
    */ long lngBackColor=RGB(255,255,255);
    long lTemp;
    int r,g,b;
    CString strChar,strTemp;
    for(int i=bmpHeight;i>1;i--)
    {
    for(int j=1;j<bmpWidth;j++)
    {
    lTemp=m_pbmpdata[(j-1)+(i-1)*bmpWidth]; r=GetRValue(lTemp);
    g=GetGValue(lTemp);
    b=GetBValue(lTemp); if(r>224 || g >224 ||  r >224)
    strChar=" ";
    else if(r>192 || g >192 ||  r >192 )
    strChar="#";
    else if(r>160 || g >160 ||  r >160 )
    strChar="%";
    else if(r>128 || g >128 ||  r >128 )
    strChar="$";
    else if(r>96 || g >96 ||  r >96 )
    strChar="8";
    else if(r>64 || g >64 ||  r >64 )
    strChar="X";
    else if(r>32 || g >32 ||  r >32 )
    strChar="?";
    else
    strChar="?";
    m_edit.GetWindowText(strTemp);
    strTemp=strTemp+strChar;
    m_edit.SetWindowText(strTemp);
    /*
    if(r>224 || g >224 ||  r >224)
    strChar="?";
    else if(r>192 || g >192 ||  r >192 )
    strChar="?";
    else if(r>160 || g >160 ||  r >160 )
    strChar="X";
    else if(r>128 || g >128 ||  r >128 )
    strChar="8";
    else if(r>96 || g >96 ||  r >96 )
    strChar="$";
    else if(r>64 || g >64 ||  r >64 )
    strChar="%";
    else if(r>32 || g >32 ||  r >32 )
    strChar="#";
    else
    strChar="@";
    */
    }
    m_edit.GetWindowText(strTemp);
    strTemp=strTemp+"\r\n";
    m_edit.SetWindowText(strTemp);
    }
    }