linux的驱动就是个字符设备,可以用read write ioctl mmap操作,通过/dev/fbx可以像文件一样直接读写
截屏dd if=/dev/fb0 of=snapshot
恢复cat snapshot > /dev/fb0
开源的有fbgrab工具,不过是生成png文件,我自己写了一个生成bmp文件的工具叫fbcap,录制成avi格式,通过socket或serial把设备的操作发送到host上:)。
保存bmp文件代码:
int savebmp(char* filename, int size, int bmWidth, int bmHeight, int bmBitsPixel, unsigned char* buffer) { FILE *fp = NULL; int j = bmHeight; int nColors = 0; int nPeletteLen = 0; RGBQUAD *rgbquad = NULL; fp = fopen(filename, "w+"); if(fp == NULL) { printf("open file :%s fail/n", filename); return -1; } printf("size=%d, width=%d, height=%d, bitsPixel=%d/n", size, bmWidth, bmHeight, bmBitsPixel); if (bmBitsPixel <= 8) { nColors = bmBitsPixel << 1; nPeletteLen = nColors * sizeof(RGBQUAD); rgbquad= malloc(sizeof(RGBQUAD)*nColors); if (rgbquad == NULL) { fclose(fp); return -1; } memset(rgbquad, 0, nColors * sizeof(RGBQUAD)); int index = 0; for(index = 0; index < nColors; index++) { rgbquad[index].rgbBlue = index; rgbquad[index].rgbGreen = index; rgbquad[index].rgbRed = index; } #if 1 if (bmBitsPixel == 1)//blue word,white backgroud { rgbquad[nColors -2].rgbBlue = 255; rgbquad[nColors -2].rgbGreen = 255; rgbquad[nColors -2].rgbRed = 255; rgbquad[nColors -1].rgbBlue = 0xff; rgbquad[nColors -1].rgbGreen = 0; rgbquad[nColors -1].rgbRed = 0; } #else if (bmBitsPixel == 1)//white word,black backgroud { rgbquad[nColors -2].rgbBlue = 0 ; rgbquad[nColors -2].rgbGreen = 0; rgbquad[nColors -2].rgbRed = 0; rgbquad[nColors -1].rgbBlue =0xFF; rgbquad[nColors -1].rgbGreen = 0xff; rgbquad[nColors -1].rgbRed = 0xFF; } #endif } printf("nColors=%d,pelettelen=%d/n",nColors, nPeletteLen); // BITMAPFILEHEADER bfh; bfh.bfType = ((unsigned short)('M'<< 8)|'B'); bfh.bfReserved1 = 0; bfh.bfReserved2 = 0; bfh.bfSize = 54 + size + nPeletteLen; bfh.bfOffBits = 54 + nPeletteLen; fwrite(&bfh,sizeof(bfh), 1, fp); // BITMAPINFOHEADER bih; bih.biSize = sizeof(BITMAPINFOHEADER); bih.biWidth = bmWidth; bih.biHeight = bmHeight; bih.biPlanes = 1; bih.biBitCount = bmBitsPixel; bih.biCompression = 0; bih.biSizeImage = size; bih.biXPelsPerMeter = 0; bih.biYPelsPerMeter = 0; bih.biClrUsed = 0; bih.biClrImportant = 0; fwrite(&bih, sizeof(BITMAPINFOHEADER), 1, fp); if (nColors > 0) { fwrite(rgbquad, nColors * sizeof(RGBQUAD), 1, fp); if (rgbquad != NULL) free(rgbquad); } while(j > 0) { fwrite(&buffer[(j-1)*bmWidth*bmBitsPixel/8], bmWidth*bmBitsPixel/8, 1, fp); j--; } fclose(fp); return 1; }
注意:对于1,4,8位色的位图,要有调色版,否则黑压压一片。