設(shè)置掃描范圍
在AVCaptureMetadataOutput中有一個(gè)叫做rectOfInterest的CGRect類型屬性。
/*!
@property rectOfInterest
@abstract
Specifies a rectangle of interest for limiting the search area for visual metadata.
@discussion
The value of this property is a CGRect that determines the receiver's rectangle of interest for each frame of video. The rectangle's origin is top left and is relative to the coordinate space of the device providing the metadata. Specifying a rectOfInterest may improve detection performance for certain types of metadata. The default value of this property is the value CGRectMake(0, 0, 1, 1). Metadata objects whose bounds do not intersect with the rectOfInterest will not be returned.
*/
@property(nonatomic) CGRect rectOfInterest NS_AVAILABLE_IOS(7_0);
這個(gè)屬性用來限制掃描范圍参淹。
這個(gè)屬性的每一個(gè)值代表的是對應(yīng)軸上的比例大小。
設(shè)置的時(shí)候當(dāng)做是橫屏來寫就可以了荔燎。
獲取rectOfInterest
//readerViewBounds為屏幕大小骂因,rect為掃描view的frame
-(CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)readerViewBounds {
CGFloat x,y,width,height;
x = (CGRectGetHeight(readerViewBounds)-CGRectGetHeight(rect))/2/CGRectGetHeight(readerViewBounds);
y = (CGRectGetWidth(readerViewBounds)-CGRectGetWidth(rect))/2/CGRectGetWidth(readerViewBounds);
width = CGRectGetHeight(rect)/CGRectGetHeight(readerViewBounds);
height = CGRectGetWidth(rect)/CGRectGetWidth(readerViewBounds);
return CGRectMake(x, y, width, height);
}
縮放
需要兩個(gè)全局變量
AVCaptureDevice * currentdevice; //獲取攝像設(shè)備
CGFloat _initialPinchZoom; //縮放系數(shù)
添加Pinch手勢
Pinch手勢響應(yīng)
- (void)pinchDetected:(UIPinchGestureRecognizer*)recogniser {
//檢查是否有可用的視頻設(shè)備
if (!currentdevice) {
return;
}
// 在手勢識別開始時(shí),記錄初始縮放系數(shù)练对。
if (recogniser.state == UIGestureRecognizerStateBegan) {
_initialPinchZoom = currentdevice.videoZoomFactor;
}
//在開始修改視頻設(shè)備參數(shù)前,鎖定視頻設(shè)備吹害。鎖定成功后螟凭,計(jì)算縮放系數(shù)(設(shè)置縮放范圍)。
NSError *error = nil;
[currentdevice lockForConfiguration:&error];
if (!error) {
CGFloat zoomFactor;
CGFloat scale = recogniser.scale;
if (scale < 1.0f) {
zoomFactor = _initialPinchZoom - pow(currentdevice.activeFormat.videoMaxZoomFactor, 1.0f - recogniser.scale);
} else {
zoomFactor = _initialPinchZoom + pow(currentdevice.activeFormat.videoMaxZoomFactor, (recogniser.scale - 1.0f) / 2.0f);
}
zoomFactor = MIN(10.0f, zoomFactor);
zoomFactor = MAX(1.0f, zoomFactor);
currentdevice.videoZoomFactor = zoomFactor;
// 解鎖視頻設(shè)備
[currentdevice unlockForConfiguration];
}
}