主題:相機(jī)模塊制作
- 框架的選擇與使用
1.1 相機(jī)功能 使用 AVFoundation
1.2 矩形檢測(cè): 由于AVFoundation 里面并沒(méi)有矩形檢 測(cè),CoreImage中有矩形檢測(cè),Vision中也有矩形檢測(cè),但是Vision需要iOS11+才行,最終選中 CoreImage 框架來(lái)實(shí)現(xiàn)矩形檢測(cè)
- 相機(jī)的使用
2.1 展示到屏幕上
2.2 實(shí)現(xiàn)代理獲取到視頻數(shù)據(jù)
2.3 由于矩形的檢測(cè)并不需要每一幀都去檢測(cè),所以我采取10幀檢測(cè)一次的辦法
2.4 篩選最大矩形
2.5 展示最大矩形
2.6 點(diǎn)擊拍照后,將照片與特征傳入Result
效果
下面來(lái)看看具體實(shí)現(xiàn)
1. 代理的實(shí)現(xiàn)
- (void)captureOutput:(AVCaptureOutput *)output didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
// 實(shí)現(xiàn)10幀檢測(cè)一次
static long count = 0;
count ++;
if (count % 10 != 0 ) {
return;
}
//1. 生成CIImage
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CIImage *ciimg = [CIImage imageWithCVImageBuffer:imageBuffer];
//2. 生成矩形特征數(shù)組
NSArray *rectFeatures = [self.detector featuresInImage:ciimg];
//3. 找出最大矩形
CIRectangleFeature *rectFeature = [self biggestRectInRects:rectFeatures];
NSLog(@"%@---%@",NSStringFromCGRect(rectFeature.bounds) ,NSStringFromCGRect(ciimg.extent));
_currentImg = ciimg;
_currentRectFeature = rectFeature;
//4. 顯示在屏幕上
[self showRectWithImg:ciimg feature:rectFeature];
}
2.從矩形數(shù)組中獲取最大矩形
- (CIRectangleFeature *)biggestRectInRects:(NSArray *)rects
{
if (rects.count == 0) {
return nil;
}
if (rects.count == 1) {
return rects.firstObject;
}
float halfPerimiterValue = 0;
CIRectangleFeature *biggestRect = [rects firstObject];
for (CIRectangleFeature *rect in rects)
{
CGPoint p1 = rect.topLeft;
CGPoint p2 = rect.topRight;
CGFloat width = hypotf(p1.x - p2.x, p1.y - p2.y);
CGPoint p3 = rect.topLeft;
CGPoint p4 = rect.bottomLeft;
CGFloat height = hypotf(p3.x - p4.x, p3.y - p4.y);
CGFloat currentHalfPerimiterValue = height + width;
if (halfPerimiterValue < currentHalfPerimiterValue)
{
halfPerimiterValue = currentHalfPerimiterValue;
biggestRect = rect;
}
}
return biggestRect;
}
3.根據(jù)特征,屏幕上畫出矩形
- (void)showRectWithImg:(CIImage *)img feature:(CIRectangleFeature *)rectFeature
{
//當(dāng)連續(xù)2次沒(méi)有檢測(cè)到矩形的時(shí)候 清楚屏幕上的矩形
static int rectIsNullCount = 0;
if (rectFeature == nil) {
rectIsNullCount++;
if (rectIsNullCount >= 2) {
rectIsNullCount = 0;
[self.rectView drawWithPointsfirst:CGPointZero
second:CGPointZero
thrid:CGPointZero
forth:CGPointZero];
dispatch_async(dispatch_get_main_queue(), ^{
[self.rectView setNeedsDisplay];
});
return;
}
return;
}
rectIsNullCount = 0; //檢測(cè)到矩形,計(jì)數(shù)清零
CGRect previewRect = self.rect;
CGRect imageRect = img.extent;
CGFloat scaleW = CGRectGetWidth(previewRect) / CGRectGetHeight(imageRect);
CGFloat scaleH = CGRectGetHeight(previewRect) / CGRectGetWidth(imageRect);
// 由于ciimage 與屏幕 是旋轉(zhuǎn)90度的,所有需要進(jìn)行調(diào)校和等比例縮放才能畫出點(diǎn)
[self.rectView
drawWithPointsfirst:CGPointMake(rectFeature.topLeft.y * scaleW, rectFeature.topLeft.x * scaleH) second:CGPointMake(rectFeature.topRight.y * scaleW, rectFeature.topRight.x * scaleH) thrid:CGPointMake(rectFeature.bottomRight.y * scaleW, rectFeature.bottomRight.x * scaleH) forth:CGPointMake(rectFeature.bottomLeft.y * scaleW, rectFeature.bottomLeft.x * scaleH)];
dispatch_async(dispatch_get_main_queue(), ^{
[self.rectView setNeedsDisplay];
});
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者