18910140161

iOS扫描二维码实现手势拉近拉远镜头

顺晟科技

2021-06-16 10:41:42

522

在做扫码需求,往往会有放大镜头需求。

苹果提供了AVCaptureConnection中,视频缩放和缩放因子:缩放裁剪系数,使用该属性,可以实现拉近拉远镜头。再结合手势UIPinchGestureRecognizer,就很简单实现手势拉近拉远镜头。

手势代码

///记录开始的缩放比例

@property(nonatomic,assign)CGFloat beginGestureScale;

///最后的缩放比例

@property(nonatomic,assign)CGFloat effectiveScale;

-(无效)摄像机

{

if (self.isVideoZoom) {

UIPinchGestureRecognizer * pinch=[[UIPinchGestureRecognizer alloc]initwithtarget 3360 self action : @ selector(Pinchedetected :)];

pinch.delegate=self

【自我。view addgesturerecognizer : pinch];

}

}

-(void)pinchdetectcted :(UIPinchGestureRecognizer *)识别器

{

自我。有效逃避=自我。begingesturescale *识别器。规模;

if (self.effectiveScale 1.0){

self.effectiveScale=1.0

}

【自我。scanobj setvideoscale : self。有效escale];

}

- (BOOL)手势识别器应该开始:(手势识别器*)手势识别器

{

if([gestureRecognizer是类:的一种[UIPinchGestureRecognizer类]]){

_ beginGestureScale=_ effectiveScale;

}

返回是;

}

拉近拉远镜头代码

-(void)set video scale :(CGFloat)标尺

{

[_输入。设备锁定配置: nil];

AVCaptureConnection * video connection=[self connections with mediatype: avmediatypevideo from connections 3360[[self StillimageOutput]connections]];

CGFloat maxscalandcrofactor=([[self。stillimageoutput connections with mediatype : avmediatypevideo]video maxscalandcrofactor])/16;

if(缩放更大缩放和缩放因子)

scale=maxScaleAndCropFactor

CGFloat zoom=缩放/视频连接。videoscalandcrofactor

视频连接。videoScaleAnDropfactor=scale

[_输入。设备解锁进行配置];

cgaffinitetransform视频预览。变换;

[交易开始];

[cattransaction setanimationduration :025];

_视频预览。transform=cgaffinitetransformscale(变换、缩放、缩放);

[交易提交];

}

有一点需要注意:视频缩放和缩放因子属性可以设置为1.0到视频更大缩放和缩放因子、视频缩放和缩放因子之间的值这个属性取值范围是1.0-videomaxscalandcrofactor,如果你设置超出范围会崩溃哦!

相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航