版本记录
| 版本号 | 时间 |
|---|---|
| V1.0 | 2017.09.04 |
前言
GPUImage是直接利用显卡实现视频或者图像处理的技术。感兴趣可以看上面几篇文章。
1. GPUImage解析(一) —— 基本概览(一)
2. GPUImage解析(二) —— 基本概览(二)
3. GPUImage解析(三) —— 基本概览(三)
4. GPUImage解析(四) —— 安装方法及框架介绍
5. GPUImage解析(五) —— 框架中的几个基类
6. GPUImage解析(六) —— 一个简单的实例(一)
功能要求
利用GPUImage进行视频采集,利用GPUImageVideoCamera进行输入,并采取其中的一种滤镜进行处理,然后利用GPUImageView显示在界面上。
功能实现
下面我们就直接看代码。
#import "JJGPUImageCameraVC.h"
#import "GPUImage.h"
@interface JJGPUImageCameraVC ()
@property (nonatomic, strong) GPUImageView *gpuImageView;
@property (nonatomic, strong) GPUImageVideoCamera *gpuVideoCamera;
@end
@implementation JJGPUImageCameraVC
#pragma mark - Override Base Function
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self setupUI];
[self setupCamera];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
self.navigationController.navigationBarHidden = NO;
}
#pragma mark - Object Private Function
- (void)setupUI
{
self.gpuImageView = [[GPUImageView alloc] init];
self.gpuImageView.frame = self.view.frame;
[self.view addSubview:self.gpuImageView];
}
- (void)setupCamera
{
//videoCamera
self.gpuVideoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset1280x720 cameraPosition:AVCaptureDevicePositionBack];
//GPUImageView填充模式
self.gpuImageView.fillMode = kGPUImageFillModePreserveAspectRatioAndFill;
//滤镜
GPUImageSepiaFilter *filter = [[GPUImageSepiaFilter alloc] init];
[self.gpuVideoCamera addTarget:filter];
[filter addTarget:self.gpuImageView];
//Start camera capturing, 里面封装的是AVFoundation的session的startRunning
[self.gpuVideoCamera startCameraCapture];
//屏幕方向的检测
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
#pragma mark - Action && Notification
- (void)orientationDidChange:(NSNotification *)noti
{
UIInterfaceOrientation *orientation = (UIInterfaceOrientation)[UIDevice currentDevice].orientation;
self.gpuVideoCamera.outputImageOrientation = orientation;
self.gpuImageView.frame = self.view.frame;
}
@end
这里有几个枚举需要说一下:
-
GPUImageFillModeType:该枚举用于定义GPUImageView的填充模式。
typedef enum {
// Stretch to fill the full view, which may distort the image outside of its normal aspect ratio
kGPUImageFillModeStretch,
// Maintains the aspect ratio of the source image, adding bars of the specified background color
kGPUImageFillModePreserveAspectRatio,
// Maintains the aspect ratio of the source image, zooming in on its center to fill the view
kGPUImageFillModePreserveAspectRatioAndFill
} GPUImageFillModeType;
-
AVCaptureDevicePosition:该枚举是AVFoundation框架里面的,用于定义图像视频采集方向的,下面看一下这个枚举。
/*!
@enum AVCaptureDevicePosition
@abstract
Constants indicating the physical position of an AVCaptureDevice's hardware on the system.
@constant AVCaptureDevicePositionUnspecified
Indicates that the device's position relative to the system hardware is unspecified.
@constant AVCaptureDevicePositionBack
Indicates that the device is physically located on the back of the system hardware.
@constant AVCaptureDevicePositionFront
Indicates that the device is physically located on the front of the system hardware.
*/
typedef NS_ENUM(NSInteger, AVCaptureDevicePosition) {
AVCaptureDevicePositionUnspecified = 0,
AVCaptureDevicePositionBack = 1,
AVCaptureDevicePositionFront = 2
} NS_AVAILABLE(10_7, 4_0) __TVOS_PROHIBITED;
功能验证
下面看一下这个功能效果。

后记
未完,待续~~~

