GPUImage关于GPUImageLookupFilter和GPUImageToneCurveFilter滤镜

GPUImageLookupFilter需要一个颜色查找表文件,这个文件是一个图片,这个原始图片在gpuimage资源文件中找;

至于该滤镜的初始化和自定义,可以参考GPUImage官方写好的滤镜GPUImageAmatorkaFilter来写。

GPUImageToneCurveFilter需要个颜色曲线文件,这个文件是一个acv格式的文件,使用的时候用initWithACVURL这个方法初始化,url可以是本地的url(没有尝试网络url)
以下片段是把色调曲线滤镜加到了摄像头上,实时渲染

    NSBundle *bundle = [NSBundle bundleForClass:self.class];
    NSURL *filterAmaro = [NSURL fileURLWithPath:[bundle pathForResource:@"fugu" ofType:@"acv"]];
    self.toneCurveFilter = [[GPUImageToneCurveFilter alloc] initWithACVURL:filterAmaro];

    GPUImageBeautifyFilter *beautifyFilter = [[GPUImageBeautifyFilter alloc] init];
    [self.camera addTarget:beautifyFilter];
    [beautifyFilter addTarget:self.toneCurveFilter];
    [self.toneCurveFilter addTarget:self.preview];
    [self.camera startCameraCapture];

以下片段是把色调曲线滤镜加到图片上,渲染

+ (UIImage *)applyFIlter:(UIImage *)originalImg withCurrentFilterName:(NSString *)filterName{
    
    UIImage *inputImage =originalImg;
    
    UIImage *outputImage = nil;
    
    GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:inputImage];
    
    //添加滤镜
    
    NSBundle *bundle = [NSBundle bundleForClass:self.class];
    NSURL *filterAmaro = [NSURL fileURLWithPath:[bundle pathForResource:filterName ofType:@"acv"]];
    
    GPUImageToneCurveFilter *toneCurveFilter = [[GPUImageToneCurveFilter alloc] initWithACVURL:filterAmaro];
    //导入之前保存的NewLookupTable.png文件
    FSKGPUImageBeautyFilter *beautyFilter = [[FSKGPUImageBeautyFilter alloc] init];
    GPUImageFilterGroup *group = [[GPUImageFilterGroup alloc] init];
    [self addGPUImageFilterFromGroup:group withGPUImageFilter:toneCurveFilter];
    [self addGPUImageFilterFromGroup:group withGPUImageFilter:beautyFilter];
    
    
    [stillImageSource addTarget:group];//相当于retain,不加这一句,滤镜会被提前释放导致系统崩溃
    
    [group useNextFrameForImageCapture];
    
    if([stillImageSource processImageWithCompletionHandler:nil]) {
        
        outputImage= [group imageFromCurrentFramebuffer];
        
    }
    
    return outputImage;
    
}
+ (void)addGPUImageFilterFromGroup:(GPUImageFilterGroup *)myFilterGroup withGPUImageFilter:(GPUImageFilter *)filter{
    
    [myFilterGroup addFilter:filter];
    
    GPUImageOutput *newTerminalFilter = filter;
    
    NSInteger count = myFilterGroup.filterCount;
    
    if (count == 1)
    {
        //设置初始滤镜
        myFilterGroup.initialFilters = @[newTerminalFilter];
        //设置末尾滤镜
        myFilterGroup.terminalFilter = newTerminalFilter;
        
    } else
    {
        GPUImageOutput *terminalFilter    = myFilterGroup.terminalFilter;
        NSArray *initialFilters                          = myFilterGroup.initialFilters;
        
        [terminalFilter addTarget:newTerminalFilter];
        
        //设置初始滤镜
        myFilterGroup.initialFilters = @[initialFilters[0]];
        //设置末尾滤镜
        myFilterGroup.terminalFilter = newTerminalFilter;
    }
}

上面两种情况下都是加上了美颜滤镜,如果不需要的话,直接去掉就行

你可能感兴趣的:(GPUImage关于GPUImageLookupFilter和GPUImageToneCurveFilter滤镜)