博客> GPUImage --> StillCamera 简单使用
GPUImage --> StillCamera 简单使用
2019-05-23 00:00 评论:0 阅读:175 Wensidun

_Overview: GPUImage是基于图形处理器开发的用于处理图片、视频流和影片的框架。相较于Core Image (part of iOS5) 框架,GPUImage 允许你创建自定义滤镜,有着简单的接口,支持iOS4.0以上。

Technical requirements: ™.OpenGL ES2.0: 使用GPUImage的应用不能运行在老设备上。(iPhone3G、1st and 2nd generation iPod touches) ™.iOS4.0不支持影片资源读取的扩展,需要请使用iOS4.1版本。如果你想使用拍照请将 deployment target 修改为iOS4.3以上. ™.设备必须有相机功能。 ™.框架使用了arc,同时也支持mrc。对于mrc的应用需要在other linker flag中添加 -fobjc-arc.

General architecture

    GPUImageVideoCamera -> GPUImageFilter --> GPUImageView

Adding the static library to your iOS project ' 1.CoreMedia 2.CoreVideo
3.OpenGLES 4.AVFoundation 5.QuartzCore

Adding this as a framework yo your iOS Project import GPUImage

Performing common tasks 1. Filtering live video

         GPUImageVideoCamera *videoCamera = [[GPUImageVideoCamera alloc]             initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];
        videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;

        GPUImageFilter *customFilter = [[GPUImageFilter alloc] initWithFragmentShaderFromFile:@"CustomShader"];
         GPUImageView *filteredVideoView = [[GPUImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, viewWidth, viewHeight)];
          // Add the view somewhere so it's visible

          [videoCamera addTarget:customFilter];
          [customFilter addTarget:filteredVideoView];

          [videoCamera startCameraCapture];

2.Capturing and filtering a still Photo

        stillCamera = [[GPUImageStillCamera alloc] init];
        stillCamera.outputImageOrientation = UIInterfaceOrientationPortrait;

        filter = [[GPUImageGammaFilter alloc] init];
        [stillCamera addTarget:filter];
        GPUImageView *filterView = (GPUImageView *)self.view;
        [filter addTarget:filterView];

        [stillCamera startCameraCapture];

        如果你想实现拍照,可以使用
            [stillCamera capturePhotoProcessedUpToFilter:filter withCompletionHandler:^(UIImage             *processedImage, NSError *error){
                NSData *dataForJPEGFile = UIImageJPEGRepresentation(processedImage, 0.8);

                NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,             NSUserDomainMask, YES);
                NSString *documentsDirectory = [paths objectAtIndex:0];

                NSError *error2 = nil;
                if (![dataForJPEGFile writeToFile:[documentsDirectory         stringByAppendingPathComponent:@"FilteredPhoto.jpg"] options:NSAtomicWrite error:&error2])
{
        return;
}

}];

3.Processing a still image

        UIImage *inputImage = [UIImage imageNamed:@"Lambeau.jpg"];

        GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:inputImage];
        GPUImageSepiaFilter *stillImageFilter = [[GPUImageSepiaFilter alloc] init];

        [stillImageSource addTarget:stillImageFilter];
        [stillImageFilter useNextFrameForImageCapture];
        [stillImageSource processImage];

        UIImage *currentFilteredVideoFrame = [stillImageFilter imageFromCurrentFramebuffer];

4.Writing a custom filter

                    GPUImageFilter *customFilter = [[GPUImageFilter alloc]         initWithFragmentShaderFromFile:@"CustomShader"];
                    varying highp vec2 textureCoordinate;

                    uniform sampler2D inputImageTexture;

                    void main()
                    {
                            lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
                            lowp vec4 outputColor;
                            outputColor.r = (textureColor.r * 0.393) + (textureColor.g * 0.769) + (textureColor.b * 0.189);
                            outputColor.g = (textureColor.r * 0.349) + (textureColor.g * 0.686) + (textureColor.b * 0.168);    
                            outputColor.b = (textureColor.r * 0.272) + (textureColor.g * 0.534) + (textureColor.b * 0.131);
                         outputColor.a = 1.0;

                         gl_FragColor = outputColor;
                     }

5.Filtering and re-encoding a movie

            ovieFile = [[GPUImageMovie alloc] initWithURL:sampleURL];
            pixellateFilter = [[GPUImagePixellateFilter alloc] init];

            [movieFile addTarget:pixellateFilter];

            NSString *pathToMovie = [NSHomeDirectory()                 stringByAppendingPathComponent:@"Documents/Movie.m4v"];
            unlink([pathToMovie UTF8String]);
            NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];

            movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(480.0, 640.0)];
            [pixellateFilter addTarget:movieWriter];

            movieWriter.shouldPassthroughAudio = YES;
            movieFile.audioEncodingTarget = movieWriter;
            [movieFile enableSynchronizedEncodingUsingMovieWriter:movieWriter];

            [movieWriter startRecording];
            [movieFile startProcessing];

            [pixellateFilter removeTarget:movieWriter];
            [movieWriter finishRecording];

Built-in filters 内置滤镜种类比较多,简单列出一些。 GPUImageBrightnessFilter GPUImageExposureFilter GPUImageContrastFilter GPUImageGammaFilter GPUImageLevelsFilter GPUImageColorMatrixFilter GPUImageRGBFilter GPUImageHueFilter GPUImageSaturationFilter GPUImageVibranceFilter GPUImageWhiteBalanceFilter GPUImageToneCurveFilter GPUImageHighlightShadowFilter GPUImageHighlightShadowTintFilter GPUImageLookupFilter GPUImageAmatorkaFilter GPUImageMissEtikateFilter GPUImageSoftEleganceFilter

收藏
0
sina weixin mail 回到顶部