提交 8a373ef9 编写于 作者: 杨时权

【需求】帧率可配置。

上级 e923efea
......@@ -23,8 +23,10 @@
#pragma mark private
- (void)initAVCaptureSession {
NSError* error = nil;
self.session = [[AVCaptureSession alloc] init];
// 设置视频分辨率
if (![self.session canSetSessionPreset:self.config.preset]) {
if (![self.session canSetSessionPreset:AVCaptureSessionPresetiFrame960x540]) {
if (![self.session canSetSessionPreset:AVCaptureSessionPreset640x480]) {
......@@ -40,7 +42,7 @@
}
[self.session setSessionPreset:self.config.preset];
[self.session beginConfiguration];
// 获取系统设备信息
AVCaptureDeviceDiscoverySession* deviceDiscoverySession = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera] mediaType:AVMediaTypeVideo position:self.config.position];
NSArray* devices = deviceDiscoverySession.devices;
for (AVCaptureDevice* device in devices) {
......@@ -50,17 +52,38 @@
}
}
NSError* error = nil;
// 设置帧率
BOOL frameRateSupport = NO;
NSArray* ranges = [self.device.activeFormat videoSupportedFrameRateRanges];
for (AVFrameRateRange* range in ranges) {
if (CMTIME_COMPARE_INLINE(self.config.duration, >=, range.minFrameDuration) &&
CMTIME_COMPARE_INLINE(self.config.duration, <=, range.maxFrameDuration)) {
frameRateSupport = YES;
}
}
if (frameRateSupport && [self.device lockForConfiguration:&error]) {
[self.device setActiveVideoMaxFrameDuration:self.config.duration];
[self.device setActiveVideoMinFrameDuration:self.config.duration];
[self.device unlockForConfiguration];
}
// 开始配置参数
[self.session beginConfiguration];
// 从设备中创建输入,之后需要设置到session
self.videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:self.device error:&error];
if (error) {
NSLog(@"%s:%d init input error!!!", __func__, __LINE__);
return;
}
// 设置session的输入
if ([self.session canAddInput:self.videoInput]) {
[self.session addInput:self.videoInput];
}
// 配置session的输出
self.videoOutput = [[AVCaptureVideoDataOutput alloc] init];
self.videoOutput.alwaysDiscardsLateVideoFrames = NO;
[self.videoOutput setVideoSettings:@{
......@@ -73,12 +96,16 @@
[self.session addOutput:self.videoOutput];
}
// 设置连接器
AVCaptureConnection* connect = [self.videoOutput connectionWithMediaType:AVMediaTypeVideo];
// 设置图片的位置
connect.videoOrientation = self.config.orientation;
if ([connect isVideoStabilizationSupported]) {
connect.preferredVideoStabilizationMode = AVCaptureVideoStabilizationModeAuto;
}
connect.videoScaleAndCropFactor = connect.videoMaxScaleAndCropFactor;
// 提交采集相关配置
[self.session commitConfiguration];
self.captureLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
......
......@@ -5,6 +5,8 @@
typedef NS_ENUM(NSUInteger, AVCaptureOutputPixelBufferType) {
kAVCaptureOutputPixelBufferType_420v = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange,
kAVCaptureOutputPixelBufferType_420f = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange,
kAVCaptureOutputPixelBufferType_bgra = kCVPixelFormatType_32BGRA,
};
@interface LWVideoCaptureConfig : NSObject
......@@ -21,6 +23,9 @@ typedef NS_ENUM(NSUInteger, AVCaptureOutputPixelBufferType) {
// 视频的方向
@property (assign, nonatomic) AVCaptureVideoOrientation orientation;
// 设置每一帧的时间间隔,其的分子为时间,单位秒,分母为帧率。
@property (assign, nonatomic) CMTime duration;
- (instancetype) init;
@end
......
......@@ -13,6 +13,7 @@
self.preset = AVCaptureSessionPreset1280x720;
self.pixelBufferType = kAVCaptureOutputPixelBufferType_420v;
self.orientation = AVCaptureVideoOrientationPortrait;
self.duration = CMTimeMake(1, 30);
}
return self;
}
......
......@@ -56,11 +56,11 @@
[self.btnItems addObject:[[LWCheckBoxButton alloc] init]];
self.btnItems[i].contentMode = UIViewContentModeScaleAspectFit;
self.btnItems[i].delegate = self;
self.btnItems[i].tag = i;
[self.labelItems addObject:[[UILabel alloc] init]];
}
self.btnItems[i].tag = i;
self.btnItems[i].image = [self.btnNoSelectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.btnItems[i].frame = CGRectMake(checkBoxItemX, checkBoxItemY, 30, 30);
......@@ -93,7 +93,7 @@
self.selectItem = self.items[index];
if (self.delegate && [self.delegate respondsToSelector:@selector(onSelect:item:)]) {
[self.delegate onSelect:index item:self.items[index]];
[self.delegate onSelect:index item:self.selectItem];
}
}
......
......@@ -36,6 +36,7 @@
self.checkBoxView = [[LWCheckBoxView alloc] initWithItems:array];
self.checkBoxView.frame = CGRectMake(checkBoxViewX, checkBoxViewY, checkBoxViewW, checkBoxViewH);
self.checkBoxView.displayType = kLWCheckBoxViewDisplayType_LR;
self.checkBoxView.delegate = self;
[self.view addSubview:self.checkBoxView];
self.captureTypeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, checkBoxViewY, 100, 30)];
......@@ -59,9 +60,6 @@
[self.stopBtn setBackgroundColor:[UIColor whiteColor]];
[self.stopBtn addTarget:self action:@selector(stopSender:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.stopBtn];
self.captureSessionConfig = [[LWVideoCaptureConfig alloc] init];
self.captureSession = [[LWVideoCaptureSession alloc] initWithConfig:self.captureSessionConfig];
}
- (void)viewDidDisappear:(BOOL)animated {
......@@ -69,6 +67,9 @@
}
- (IBAction)startSender:(id)sender {
self.captureSessionConfig = [[LWVideoCaptureConfig alloc] init];
self.captureSession = [[LWVideoCaptureSession alloc] initWithConfig:self.captureSessionConfig];
[self.captureSession startCapture];
AVCaptureVideoPreviewLayer* layer = [self.captureSession layer];
......@@ -84,6 +85,7 @@
[layer removeFromSuperlayer];
[self.captureSession stopCapture];
self.captureSession = nil;
}
#pragma mark LWVideoCaptureSessionDelegate
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册