在Max OSX中从屏幕获取像素(屏幕截图) [英] Get pixel from the screen (screenshot) in Max OSX

查看:98
本文介绍了在Max OSX中从屏幕获取像素(屏幕截图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试从MacOSX 10.8.4中的C ++程序获取屏幕截图,这是不可能的.我什至无法得到一个像素.甚至有可能吗?

I've been trying to get a screenshot from a C++ program in MacOSX 10.8.4 and it's been impossible. I can't even get a single pixel. Is it even possible?

有人可以帮助我吗?或者至少给我一些线索或链接.

Can somebody help me? Or at least give me some clue or link.

推荐答案

void captureScreen(){
    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;
    uint8 *pixels = malloc(width * height * bpp);
    memcpy(pixels, CFDataGetBytePtr(dataref), width * height * bpp);
    CFRelease(dataref); 
   CGImageRelease(image_ref); 
   FILE *stream = fopen("/Users/robert/Desktop/screencap.raw", "w+");
   fwrite(pixels, bpp, width * height, stream);
   fclose(stream); 
   free(pixels);
}

参考 https://gist.github.com/robert-wallis/5063309

这篇关于在Max OSX中从屏幕获取像素(屏幕截图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆