2014年1月23日星期四

opengl 红蓝切换 上下翻转

opengl  红蓝切换   上下翻转


  1. #pragma mark -
  2. #pragma mark Capture Video Methods
  3. - (void) testVideoWriter {
  4.     
  5.     //initialize global info
  6.     MOVIE_NAME = @"Documents/Movie.mov";
  7.     CGSize size = CGSizeMake(1024, 768);
  8.     frameLength = CMTimeMake(1, 30);
  9.     currentTime = kCMTimeZero;
  10.     currentFrame = 0;
  11.     
  12.     NSString *MOVIE_PATH = [NSHomeDirectory() stringByAppendingPathComponent:MOVIE_NAME];
  13.     NSError *error = nil;
  14.     
  15.     unlink([MOVIE_PATH UTF8String]);
  16.     
  17.     videoWriter = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:MOVIE_PATH] fileType:AVFileTypeQuickTimeMovie error:&error];
  18.     
  19.     NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:AVVideoCodecH264, AVVideoCodecKey,
  20.                                    [NSNumber numberWithInt:size.width], AVVideoWidthKey,
  21.                                    [NSNumber numberWithInt:size.height], AVVideoHeightKey, nil];
  22.     writerInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];
  23.     
  24.     //writerInput.expectsMediaDataInRealTime = NO;
  25.     
  26.     NSDictionary *sourcePixelBufferAttributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:kCVPixelFormatType_32BGRA], kCVPixelBufferPixelFormatTypeKey, nil];
  27.     
  28.     adaptor = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput                                                                          sourcePixelBufferAttributes:sourcePixelBufferAttributesDictionary];
  29.     [adaptor retain];
  30.     
  31.     [videoWriter addInput:writerInput];
  32.     
  33.     [videoWriter startWriting];
  34.     [videoWriter startSessionAtSourceTime:kCMTimeZero];
  35.     
  36.     VIDEO_WRITER_IS_READY = true;
  37. }
  38. - (void) captureScreenVideo {
  39.     
  40.     if (!writerInput.readyForMoreMediaData) {
  41.         return;
  42.     }
  43.     
  44.     CGSize esize = CGSizeMake(1024, 768);
  45.     NSInteger myDataLength = esize.width * esize.height * 4;
  46.     GLubyte *buffer = (GLubyte *) malloc(myDataLength);
  47.     glReadPixels(0, 0, esize.width, esize.height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  48.     // swap red and blue
  49.     for (int i=0; i
  50.         GLubyte b_red = buffer[i];
  51.         buffer[i] = buffer[i+2];
  52.         buffer[i+2] = b_red;
  53.     }
  54.     // flip the buffer data
  55.     GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
  56.     NSInteger myDataLineLength = esize.width * 4;
  57.     for (int i=0; i
  58.         memcpy(buffer2+i*myDataLineLength, buffer+((GLuint)esize.height-i-1)*myDataLineLength, myDataLineLength);
  59.     }
  60.     // copy back flipped data
  61.     memcpy(buffer, buffer2, myDataLength);
  62.     free(buffer2);
  63.     
  64.     CVPixelBufferRef pixel_buffer = NULL;
  65.     CVPixelBufferCreateWithBytes (NULL, esize.width, esize.height, kCVPixelFormatType_32BGRA, buffer, 4 * esize.width, NULL, 0, NULL, &pixel_buffer);
  66.     
  67.     if(![adaptor appendPixelBuffer:pixel_buffer withPresentationTime:currentTime]) {
  68.         NSLog(@"FAIL");
  69.     } else {
  70.         NSLog(@"Success:%d", currentFrame);
  71.         currentTime = CMTimeAdd(currentTime, frameLength);
  72.     }
  73.     
  74.     free(buffer);
  75.     CVPixelBufferRelease(pixel_buffer);
  76.     currentFrame++;
  77.     
  78.     if (currentFrame > MAX_FRAMES) {
  79.         VIDEO_WRITER_IS_READY = false;
  80.         [writerInput markAsFinished];
  81.         [videoWriter finishWriting];
  82.         [videoWriter release];
  83.         
  84.         [self moveVideoToSavedPhotos];
  85.     }
  86. }
  87. - (void) moveVideoToSavedPhotos {
  88.     ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
  89.     NSString *localVid = [NSHomeDirectory() stringByAppendingPathComponent:MOVIE_NAME];    
  90.     NSURL* fileURL = [NSURL fileURLWithPath:localVid];
  91.     
  92.     [library writeVideoAtPathToSavedPhotosAlbum:fileURL
  93.                                 completionBlock:^(NSURL *assetURL, NSError *error) {
  94.                                     if (error) {  
  95.                                         NSLog(@"%@: Error saving context: %@", [self class], [error localizedDescription]);
  96.                                     }
  97.                                 }];
  98.     [library release];
  99. }

没有评论:

发表评论