根据每一帧数据获得图片

// 根据每一帧数据获得图片  
NSData *imageFromSampleBuffer(CMSampleBufferRef sampleBuffer)
{
       CVImageBufferRef imageBuffer = CMSampleBufferGetImageBu ffer(sampleBuffer);
       // Lock the base address of the pixel buffer.
       CVPixelBufferLockBaseAdd ress(imageBuffer,0);
      
       // Get the number of bytes per row for the pixel buffer.
       size_t bytesPerRow = CVPixelBufferGetBytesPer Row(imageBuffer);
       // Get the pixel buffer width and height.
       size_t pixelsWide = CVPixelBufferGetWidth(imageBuffer);
       size_t pixelsHigh = CVPixelBufferGetHeight(imageBuffer);
       //       size_t dataSize = CVPixelBufferGetDataSize (imageBuffer);
       //       OSType type = CVPixelBufferGetPixelFor matType(imageBuffer);
      
       // Get the base address of the pixel buffer.
       uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddr essOfPlane(imageBuffer,0);
      
       // Get the data size for contiguous planes of the pixel buffer.
       //size_t bufferSize = CVPixelBufferGetDataSize (imageBuffer);
      
       //       if (!baseAddress) {
       //               NSLog(@"baseAddress 为空");
       //       }
       //       NSLog(@"%ld - %ld - %ld - %ld - %ld",pixelsWide,pixelsHigh,bytesPerRow,dataSize,type);
      
       CGContextRef       context = NULL;
       CGColorSpaceRef colorSpace;
      
      
       colorSpace = CGColorSpaceCreateDevice RGB();//CGColorSpaceCreateWithNa me(kCGColorSpaceGenericRGB);// 2
      
       context = CGBitmapContextCreate(baseAddress,
                                                                       pixelsWide,
                                                                       pixelsHigh,
                                                                       8,
                                                                       bytesPerRow,
                                                                       colorSpace,
                                                                       kCGBitmapByteOrder32Litt le | kCGImageAlphaPremultipli edFirst);
       if (context== NULL)
       {
               //               //free (bitmapData);// 5
               //               NSLog(@"context == NULL") ;
               //               fprintf (stderr, "Context not created!");
               return NULL;
       }
       CGColorSpaceRelease( colorSpace );// 6
      
       CGImageRef imageRef = CGBitmapContextCreateIma ge(context);
       UIImage *image = [[UIImage alloc] initWithCGImage:imageRef scale:1.0f orientation:UIImageOrientationRight];
      
       CGContextRelease(context);
#if 0
       //       NSData *data = UIImagePNGRepresentation (image);
       //       NSArray *paths = NSSearchPathForDirectori esInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
       //       NSString *docDir = [paths objectAtIndex:0];
       //       if (!docDir) {
       //               NSLog(@"Documents directory not found!");
       //               return NO;      
       //       }      
       //       NSString *filePath = [docDir stringByAppendingPathCom ponent:[NSString stringWithFormat:@"%.4f.png",[[NSDate date] timeIntervalSince1970]]];
       //       NSLog(@"输出%@",filePath);
       //       NSLog(@"bool = %d",[data writeToFile:filePath atomically:YES]);
#endif
       CVPixelBufferUnlockBaseA ddress(imageBuffer,0);
       NSData *data = UIImageJPEGRepresentatio n(image, 0.5f);
       [image release];
       CGImageRelease(imageRef);
       return data;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值