全網(wǎng)找了一圈也沒(méi)有看到一個(gè) GPUImageRawDataInput 的完整 Demo囚霸,這里提供一個(gè)簡(jiǎn)單的使用示例腰根,Demo:
#import "RawDataViewController.h"
#import "GPUImage.h"
@interface RawDataViewController ()
@property (nonatomic, strong) GPUImageRawDataInput *rawDataInput;
@property (nonatomic, strong) GPUImageRawDataOutput *rawDataOutput;
@property (nonatomic, strong) GPUImageBrightnessFilter *filter;
@property (nonatomic, strong) GPUImageView *filterView;
@end
@implementation RawDataViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.filterView = [[GPUImageView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 300)];
[self.view addSubview:self.filterView];
// 1. UIImage -> CGImage -> CFDataRef -> UInt8 * data
UIImage *image = [UIImage imageNamed:@"img1.jpg"];
CGImageRef newImageSource = [image CGImage];
CFDataRef dataFromImageDataProvider = CGDataProviderCopyData(CGImageGetDataProvider(newImageSource));
GLubyte* imageData = (GLubyte *)CFDataGetBytePtr(dataFromImageDataProvider);
// 2. UInt8 * data -> GPUImageRawDataInput
self.rawDataInput = [[GPUImageRawDataInput alloc] initWithBytes:imageData size:image.size pixelFormat:GPUPixelFormatRGBA];
self.filter = [[GPUImageBrightnessFilter alloc] init];
self.filter.brightness = 0.1;
[self.rawDataInput addTarget:self.filter];
// 3. 輸出到 GPUImageView
[self.filter addTarget:self.filterView];
// 4. 同時(shí)輸出到 raw data output
self.rawDataOutput = [[GPUImageRawDataOutput alloc] initWithImageSize:image.size resultsInBGRAFormat:YES];
[self.filter addTarget:self.rawDataOutput];
// important
[self.filter useNextFrameForImageCapture];
[self.rawDataInput processData];
// 5. read data from GPUImageRawDataOutput
[self.rawDataOutput lockFramebufferForReading];
GLubyte *outputBytes = [self.rawDataOutput rawBytesForImage];
NSInteger bytesPerRow = [self.rawDataOutput bytesPerRowInOutput];
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, outputBytes, bytesPerRow * image.size.height, NULL);
CGImageRef cgImage = CGImageCreate(image.size.width, image.size.height, 8, 32, bytesPerRow, rgbColorSpace, kCGImageAlphaPremultipliedFirst|kCGBitmapByteOrder32Little, provider, NULL, true, kCGRenderingIntentDefault);
[self.rawDataOutput unlockFramebufferAfterReading];
// 斷點(diǎn)到這一行,查看 outImage
UIImage *outImage = [UIImage imageWithCGImage:cgImage];
NSLog(@"%@", outImage);
}
@end
使用場(chǎng)景
這個(gè)示例的使用場(chǎng)景:
- 用 ffmpeg 將視頻流的一幀讀取成 RGBA 數(shù)據(jù)拓型;
- 將數(shù)據(jù)傳入 GPUImageRawDataInput -> 添加濾鏡 -> 輸出到 GPUImageRawDataOutput
- 從 GPUImageRawDataOutput 中取出 RGBA 數(shù)據(jù)额嘿,再交給 ffmpeg 編碼,寫(xiě)入新的視頻文件
注:
- 這個(gè)流程使用 GPUImage 完全可以完成劣挫,不需要 ffmpeg册养;需要跨平臺(tái)的編解碼并加濾鏡時(shí),可以使用 OpenGL 對(duì)視頻流加濾鏡揣云;
- 在 Xcode 里斷點(diǎn)查看 UIImage 的方法如下: