我在 Mac OS X 上的 C++ 项目中实现了以下代码,用于捕获桌面屏幕:
int ScreenCaptureRoutines::CaptureImage(int width, int height)
{
CGRect captureRect;
captureRect.origin.x = 0;
captureRect.origin.y = 0;
captureRect.size.width = width;
captureRect.size.height = height;
CGImageRef img = CGWindowListCreateImage(captureRect, kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault);
if(img == NULL)
{
fprintf(stderr, "CGWindowListCreateImage failed\n!");
return -1;
}
/* get pixels */
CGDataProviderRef provider = CGImageGetDataProvider(img);
CFDataRef dataRef = CGDataProviderCopyData(provider);
uint8_t* pixels = (uint8_t*)CFDataGetBytePtr(dataRef);
BitmapUtility::SaveBitmapToFile(pixels, width, height, 32, "/Users/Main/test32.bmp");
}
该代码适用于 1024x768、1280x1024、1280x960、1280x768 等分辨率,但它完全打乱了 1366x768 的图像。
这是用于保存像素的 SaveBitmapFile 方法:
void BitmapUtility::SaveBitmapToFile(u8* pBitmapBits, long lWidth, long lHeight, unsigned short wBitsPerPixel, char* lpszFileName)
{
BITMAPINFOHEADER bmpInfoHeader = {0};
bmpInfoHeader.biSize = sizeBITMAPINFOHEADER;
bmpInfoHeader.biBitCount = wBitsPerPixel;
bmpInfoHeader.biClrImportant = 0;
bmpInfoHeader.biClrUsed = 0;
bmpInfoHeader.biCompression = BI_RGB;
bmpInfoHeader.biHeight = lHeight;
bmpInfoHeader.biWidth = lWidth;
bmpInfoHeader.biPlanes = 1;
bmpInfoHeader.biSizeImage = lWidth* lHeight * (wBitsPerPixel / 8);
BITMAPFILEHEADER bfh = {0};
bfh.bfType=0x4D42;
bfh.bfOffBits = sizeBITMAPINFOHEADER + sizeBITMAPFILEHEADER;
bfh.bfSize = bfh.bfOffBits + bmpInfoHeader.biSizeImage;
//Creating bitmap file
int fileDescriptor = open(lpszFileName, O_CREAT | O_WRONLY);
if (fileDescriptor == -1)
{
//returning if error encountered when opening file
return;
}
ByteBufferOutputStream bufferInfoHeader;
bufferInfoHeader.write(&bmpInfoHeader.biSize, 4);
bufferInfoHeader.write(&bmpInfoHeader.biWidth, 4);
bufferInfoHeader.write(&bmpInfoHeader.biHeight, 4);
bufferInfoHeader.write(&bmpInfoHeader.biPlanes, 2);
bufferInfoHeader.write(&bmpInfoHeader.biBitCount, 2);
bufferInfoHeader.write(&bmpInfoHeader.biCompression, 4);
bufferInfoHeader.write(&bmpInfoHeader.biSizeImage, 4);
bufferInfoHeader.write(&bmpInfoHeader.biXPelsPerMeter, 4);
bufferInfoHeader.write(&bmpInfoHeader.biYPelsPerMeter, 4);
bufferInfoHeader.write(&bmpInfoHeader.biClrUsed, 4);
bufferInfoHeader.write(&bmpInfoHeader.biClrImportant, 4);
ByteBufferOutputStream bufferFileHeader;
bufferFileHeader.write(&bfh.bfType, 2);
bufferFileHeader.write(&bfh.bfSize, 4);
bufferFileHeader.write(&bfh.bfReserved1, 2);
bufferFileHeader.write(&bfh.bfReserved2, 2);
bufferFileHeader.write(&bfh.bfOffBits, 4);
//Writing bitmap to file
ssize_t bytesWritten = write(fileDescriptor, bufferFileHeader.get()->getData(), sizeBITMAPFILEHEADER);
bytesWritten = write(fileDescriptor, bufferInfoHeader.get()->getData(), sizeBITMAPINFOHEADER);
bytesWritten = write(fileDescriptor, pBitmapBits, bmpInfoHeader.biSizeImage);
//Closing bitmap file
close(fileDescriptor);
}
SaveBitmapToFile 方法仅用于测试,在实际场景中,我实际上是通过 libavcodec 传递捕获的像素以将帧转换并保存为 FLV 格式,但最终结果是相同的,图像被打乱了。
有谁知道为什么代码不适用于 1366x768(或其他非标准 4:3 或 16:9 分辨率)?