ORC

iOS 自己实现 名片识别 二 相机模块制作

2018-08-07  本文已影响563人  呆木大人

1. iOS 自己实现 名片识别 一 功能分析,模块划分

2. iOS 自己实现 名片识别 二 相机模块制作

3. iOS 自己实现 名片识别 三 图片处理

4. iOS 自己实现 名片识别 四 图片转文字以及归类

timg.png

主题:相机模块制作

效果

效果图.gif

下面来看看具体实现

1. 代理的实现

- (void)captureOutput:(AVCaptureOutput *)output didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
    // 实现10帧检测一次
    static long count = 0;
    count ++;
    if (count % 10 != 0 ) {
        return;
    }
    
    //1. 生成CIImage
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    CIImage *ciimg = [CIImage imageWithCVImageBuffer:imageBuffer];
    
    //2. 生成矩形特征数组
    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.从矩形数组中获取最大矩形

- (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.根据特征,屏幕上画出矩形

- (void)showRectWithImg:(CIImage *)img feature:(CIRectangleFeature *)rectFeature
{
    //当连续2次没有检测到矩形的时候 清楚屏幕上的矩形
    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;   //检测到矩形,计数清零
    
    CGRect previewRect = self.rect;
    CGRect imageRect = img.extent;
    
    CGFloat scaleW = CGRectGetWidth(previewRect) / CGRectGetHeight(imageRect);
    CGFloat scaleH = CGRectGetHeight(previewRect) / CGRectGetWidth(imageRect);
   
    
    // 由于ciimage 与屏幕 是旋转90度的,所有需要进行调校和等比例缩放才能画出点
    [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];
    });
}
上一篇下一篇

猜你喜欢

热点阅读