Android截屏及圖片解析

來源:互聯網
上載者:User

ImageData getDeviceImage(Device mDevice) ;

ImageData 類型封裝了我們需要的所有映像資訊。其中我們使用其

public byte data[];

屬性涵蓋了映像的像素矩陣資訊。
(320*480像素的映像,其data數組的大小是320*480*3+480 byte),
每個像素由3個色素(紅值,綠值和藍值)構成,且每行行首有1個行標識位元組。

在項目中,我們沒採用這種方法,也未發現每行行首有什麼標識位元組。採用的方法是直接從/dev/graphics/fb0讀取映像資訊,在PC端,可以用adb pull /dev/graphics/fb0 D:\a 將Android手機上當前螢幕資訊截回來存到PC中。對於320*480螢幕,該a檔案大小為1024K 對於240*320的螢幕 ,a檔案大小為620K

解析映像時,發現該a檔案裡麵包含了幾幅圖的資訊。不過我們只採用了第一幅圖。每個像素佔2位元組RGB565的格式。這樣我們就可以按像素解析成我們想要的圖片格式。由於BMP效果較好,可以解析成BMP映像。

   解析成BMP時,關鍵代碼如下:首先設定BMP檔案頭,修改大小以及圖片解析度,然後從a檔案中按像素讀取rgb值寫入BMP像素中。

     sg_BHeader[0x02] = (UCHAR)(m_Width * m_Height * 3 + 0x36) & 0xff;
     sg_BHeader[0x03] = (UCHAR)((m_Width * m_Height * 3 + 0x36) >> 8) & 0xff;
     sg_BHeader[0x04] = (UCHAR)((m_Width * m_Height * 3 + 0x36) >> 16) & 0xff;
     sg_BHeader[0x05] = (UCHAR)((m_Width * m_Height * 3 + 0x36) >> 24) & 0xff;
     sg_BHeader[0x12] = (UCHAR)m_Width & 0xff;
     sg_BHeader[0x13] = (UCHAR)(m_Width >> 8) & 0xff;
     sg_BHeader[0x14] = (UCHAR)(m_Width >> 16) & 0xff;
     sg_BHeader[0x15] = (UCHAR)(m_Width >> 24) & 0xff;
     sg_BHeader[0x16] = (UCHAR)m_Height & 0xff;
     sg_BHeader[0x17] = (UCHAR)(m_Height >> 8) & 0xff;
     sg_BHeader[0x18] = (UCHAR)(m_Height >> 16) & 0xff;
     sg_BHeader[0x19] = (UCHAR)(m_Height >> 24) & 0xff;
// sg_BHeader[0x34] = (UCHAR)(m_Width * m_Height * 3 ) & 0xff;
// sg_BHeader[0x35] = (UCHAR)(m_Width * m_Height * 3 >>8) & 0xff;
// sg_BHeader[0x36] = (UCHAR)(m_Width * m_Height * 3 >>16) & 0xff;
// sg_BHeader[0x37] = (UCHAR)(m_Width * m_Height * 3 >>24) & 0xff;
     write(bmp, sg_BHeader, sizeof(sg_BHeader));

    for(i = 0; i < m_Height; i++)
     {
         unsigned char *c = p + (m_Height - 1 - i) * m_Width * 2;
         unsigned char cc;
         for(j = 0; j < m_Width * 2; j+=2)
         {
    value = c[j] & 0x00FF;
             value |= (c[j+1] << 8) & 0x0FF00;
    r = ((value >> 11) & 0x01F) << 3;
             g = ((value >> 5) & 0x03F) << 2;
    b = ((value >> 0) & 0x01F) << 3;
       outBuffer[index++] = (unsigned char)b;
    outBuffer[index++] = (unsigned char)g;
    outBuffer[index++] = (unsigned char)r;
            
         }
     }
write(bmp, outBuffer, sizeof(outBuffer));

建了一個簡單的工程,裡麵包含了圖片解析的主要代碼,a,b,c是從手機截回來的圖片,temp.bmp 是解析出來的圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.