2012-07-09 73 views
0

我想在Mac OS中提到的配置下屏幕捕獲的glGrab代碼集成和我目前停留在一個全藍屏渲染我的窗口內。我相信在創建圖像紋理時存在一些問題,但不能說明是什麼。在OpenGL中我只有幾個星期的時間,所以如果我錯過了一些明顯的東西,請在我身上輕鬆一下。glGrab在Mac OS 10.7.3屏幕捕獲與XCode的4.3.2

我用glGrab代碼,因爲它是除CGLSetFullScreen方法(甚至不CGLSetFullScreenOnDisplay),因爲這些方法現在已經過時。所以這一行代碼暫時已經被註釋掉了。

我一直在做關於這個主題的一些研究,因爲有一段時間了,發現在計算器這可能本來是完整的答案另一個線程,但它幫助了很多不過。 Convert UIImage to CVImageBufferRef

直接參照glGrab代碼是http://code.google.com/p/captureme/source/browse/trunk/glGrab.c

回答

0

的答案我的上述問題是本下面。所以沒有更多的opengl或glGrab。使用最適合Mac OSX的優化。這不包括用於捕捉鼠標指針也代碼,但我相信,如果你已經登陸這個頁面上,你很聰明足以通過自己看着辦吧。或者,如果有人讀這知道解決辦法則是你的機會來幫助博愛:)另外該代碼返回CVPixelBufferRef。您可以選擇發回無論是CGImageRef甚至字節流,因爲它是,它只是調整自己的喜好。 :

void swizzleBitmap(void *data, int rowBytes, int height) { 
    int top, bottom; 
    void * buffer; 
    void * topP; 
    void * bottomP; 
    void * base; 

    top = 0; 
    bottom = height - 1; 
    base = data; 
    buffer = malloc(rowBytes); 

    while (top < bottom) { 
     topP = (void *)((top * rowBytes) + (intptr_t)base); 
     bottomP = (void *)((bottom * rowBytes) + (intptr_t)base); 

     bcopy(topP, buffer, rowBytes); 
     bcopy(bottomP, topP, rowBytes); 
     bcopy(buffer, bottomP, rowBytes); 

     ++top; 
     --bottom; 
    } 
    free(buffer); 
} 

CVImageBufferRef grabViaOpenGL() { 
    int bytewidth; 

    CGImageRef image = CGDisplayCreateImage(kCGDirectMainDisplay); // Main screenshot capture call 

    CGSize frameSize = CGSizeMake(CGImageGetWidth(image), CGImageGetHeight(image)); // Get screenshot bounds 

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: 
          [NSNumber numberWithBool:NO], kCVPixelBufferCGImageCompatibilityKey, 
          [NSNumber numberWithBool:NO], kCVPixelBufferCGBitmapContextCompatibilityKey, 
          nil]; 

    CVPixelBufferRef pxbuffer = NULL; 
    CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, frameSize.width, 
              frameSize.height, kCVPixelFormatType_32ARGB, (CFDictionaryRef) options, 
              &pxbuffer); 


    CVPixelBufferLockBaseAddress(pxbuffer, 0); 
    void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer); 

    CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef context = CGBitmapContextCreate(pxdata, frameSize.width, 
               frameSize.height, 8, 4*frameSize.width, rgbColorSpace, 
               kCGImageAlphaNoneSkipLast); 

    CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image), 
              CGImageGetHeight(image)), image); 

    bytewidth = frameSize.width * 4; // Assume 4 bytes/pixel for now 
    bytewidth = (bytewidth + 3) & ~3; // Align to 4 bytes 
    swizzleBitmap(pxdata, bytewidth, frameSize.height);  // Solution for ARGB madness 

    CGColorSpaceRelease(rgbColorSpace); 
    CGImageRelease(image); 
    CGContextRelease(context); 

    CVPixelBufferUnlockBaseAddress(pxbuffer, 0); 

    return pxbuffer; 
}