objective-c - 使用目标 C++ 截取的屏幕截图有问题
问题描述
我查看了以下帖子:在 Max OSX 中从屏幕(屏幕截图)获取像素
并实现了接受的答案:
文件名:screenshot.mm
#import <Foundation/Foundation.h>
#include <iostream>
#include <cstdint>
#include <cstdio>
#include <fstream>
void captureScreen(void)
{
CGImageRef image_ref = CGDisplayCreateImage(CGMainDisplayID());
CGDataProviderRef provider = CGImageGetDataProvider(image_ref);
CFDataRef dataref = CGDataProviderCopyData(provider);
size_t width, height;
width = CGImageGetWidth(image_ref);
height = CGImageGetHeight(image_ref);
size_t bpp = CGImageGetBitsPerPixel(image_ref) / 8;
std::cout << bpp << " " << width << " " << height << std::endl;
uint8 *pixels = (uint8*)malloc(width * height * bpp);
memcpy(pixels, CFDataGetBytePtr(dataref), width * height * bpp);
CFRelease(dataref);
CGImageRelease(image_ref);
std::ofstream file("image.ppm");
file << "P6 " << width << " " << height << " " << "256" << "\n";
for (size_t i = 0; i < height; i++)
{
for (size_t j = 0; j < width; j++)
{
file << pixels[(j + j * i) * bpp + 0]
<< pixels[(j + j * i) * bpp + 1]
<< pixels[(j + j * i) * bpp + 2];
}
file << std::endl;
}
}
int main(void)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
captureScreen();
[pool drain];
return 0;
}
编译:clang++ -framework Foundation -framework CoreGraphics screenshot.mm
但是当我查看屏幕截图时,它看起来像这样:
注意:这只是图像的一部分,因为文件太大,但你明白了
如何正确获取屏幕截图?
解决方案
推荐阅读
- javascript - 改变数组结构
- python - 如何将namedtuple转换为元组
- php - 如何在php中循环通过web服务获取的参数
- javascript - 获取 API css-loader
- angular - 在组件 service.ts url:“http://localhost:4200/data/products.json”未找到
- angular - 一次只选择一个复选框,并在一个特定复选框被选中或两个复选框都未选中时执行特定任务
- ionic3 - IOS无法拍照或从图库中选择图像
- android - 如何重定向 URL?
- c# - 如何在给定的指定时间(包括 OnTimeEvent)停止此异步功能?
- python - 如何用 Python 重写在 Lua 上编写的密码哈希?