更新时间 : 2023-07-18 09:33:29
创建相机模板需要通过 SXTemplate
类的初始化方法创建
/**
* 初始化一个模板实例
* @param camera 相机对象
* @return 模板实例
*/
- (instancetype)initWithCamera:(SXCamera *)camera;
或
/**
* 初始化一个模板实例
* @param width 画布宽度
* @param height 画布高度
* @param frameRate 帧速率
* @param camera 相机对象
* @return 模板实例
*/
- (instancetype)initWithWidth:(CGFloat)width height:(CGFloat)height frameRate:(int)frameRate camera:(SXCamera *)camera;
SXCamera 初始化方法
/// 初始化相机对象
/// @param size 相机尺寸
/// @param frameRate 相机帧速率
/// @param position 前置或者后置
/// @param type 相机类型
- (instancetype)initWithSize:(SXCameraSize)size
frameRate:(SXCameraFrameRate)frameRate
position:(SXCameraPosition)position
type:(SXCameraSourceType)type;
相机的画面展示,通过SXTemplatePlayer播放在UIView上
相机api
/// 启动相机
- (void)start;
/// 停止相机
- (void)stop;
/// 设置相机焦点
/// @param point 焦点
- (void)focusAt:(CGPoint)point;
/// 是否启用闪光灯
/// @param enable YES启用 NO不启用
- (void)enableTorch:(BOOL)enable;
/// 镜像相机画面 需要在[template commit] 之后调用
- (void)videoMirrored:(BOOL)isMirrored;
代码示例
#import <SXVideoEnging/SXVideoEnging.h>
SXCamera *camera = [[SXCamera alloc] initWithSize:SXCameraSize720 frameRate:SXCameraFrameRate30 position:SXCameraPositionBack type:SXCameraSourceTypeAudioAndVideo];
SXTemplate *mTemplate = [[SXTemplate alloc] initWithWidth:720 height:1280 frameRate:30 camera:camera];
SXTemplatePlayer *mPlayer = [[SXTemplatePlayer alloc] initWithTemplate:mTemplate audioPath:nil];
mPlayer.delegate = self;
UIView *playView = [mPlayer getPlayerView];
int videoWidth = [mTemplate mainCompWidth];
int videoHeight = [mTemplate mainCompHeight];
CGFloat playerScale = self.view.bounds.size.height / videoHeight;
playView.bounds = CGRectMake(0, 0, videoWidth * playerScale, self.view.bounds.size.height);
playView.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height / 2);
[self.view addSubview:playView];
[self.view sendSubviewToBack:playView];
[mTemplate commit];
[mPlayer start];
[camera start];
//根据前置后置设置相机是否镜像
//[mTemplate.camera videoMirrored:NO];
创建相机模板需要通过 SXTemplate
类的初始化方法创建
/**
* 初始化一个模板实例
* @param camera 相机对象
* @return 模板实例
*/
- (instancetype)initWithCamera:(SXCamera *)camera;
或
/**
* 初始化一个模板实例
* @param width 画布宽度
* @param height 画布高度
* @param frameRate 帧速率
* @param camera 相机对象
* @return 模板实例
*/
- (instancetype)initWithWidth:(CGFloat)width height:(CGFloat)height frameRate:(int)frameRate camera:(SXCamera *)camera;
SXCamera 初始化方法
/// 初始化相机对象
/// @param size 相机尺寸
/// @param frameRate 相机帧速率
/// @param position 前置或者后置
/// @param type 相机类型
- (instancetype)initWithSize:(SXCameraSize)size
frameRate:(SXCameraFrameRate)frameRate
position:(SXCameraPosition)position
type:(SXCameraSourceType)type;
相机的画面展示,通过SXTemplatePlayer播放在UIView上
相机api
/// 启动相机
- (void)start;
/// 停止相机
- (void)stop;
/// 设置相机焦点
/// @param point 焦点
- (void)focusAt:(CGPoint)point;
/// 是否启用闪光灯
/// @param enable YES启用 NO不启用
- (void)enableTorch:(BOOL)enable;
/// 镜像相机画面 需要在[template commit] 之后调用
- (void)videoMirrored:(BOOL)isMirrored;
代码示例
#import <SXVideoEnging/SXVideoEnging.h>
SXCamera *camera = [[SXCamera alloc] initWithSize:SXCameraSize720 frameRate:SXCameraFrameRate30 position:SXCameraPositionBack type:SXCameraSourceTypeAudioAndVideo];
SXTemplate *mTemplate = [[SXTemplate alloc] initWithWidth:720 height:1280 frameRate:30 camera:camera];
SXTemplatePlayer *mPlayer = [[SXTemplatePlayer alloc] initWithTemplate:mTemplate audioPath:nil];
mPlayer.delegate = self;
UIView *playView = [mPlayer getPlayerView];
int videoWidth = [mTemplate mainCompWidth];
int videoHeight = [mTemplate mainCompHeight];
CGFloat playerScale = self.view.bounds.size.height / videoHeight;
playView.bounds = CGRectMake(0, 0, videoWidth * playerScale, self.view.bounds.size.height);
playView.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height / 2);
[self.view addSubview:playView];
[self.view sendSubviewToBack:playView];
[mTemplate commit];
[mPlayer start];
[camera start];
//根据前置后置设置相机是否镜像
//[mTemplate.camera videoMirrored:NO];