如何捕获AVCaptureSession的一部分?

时间:2017-08-22 01:49:29

标签: ios xcode ocr

enter image description here

对于我的应用程序,我正在尝试使用OCR Tesseract将图像转换为文本。我已经学会了如何拍摄整个AVCaptureSession的屏幕截图,但我只想拍摄绿色方块的图像,以便更容易转换OCR Tesseract并获得更清晰的用户体验。

我已经阅读了文章(下面),但它只捕获视图而不是视图后面的AVCaptureSession。

ios how to capture a particular portion of screen

这是代码

@interface OCRScannerViewController ()
@property (strong, nonatomic) IBOutlet UIPinchGestureRecognizer *pinchGestureRecognizer;
@property (weak, nonatomic) IBOutlet UIView *cameraView;
@property (weak, nonatomic) IBOutlet VINCaptureView *captureView;
@property (weak, nonatomic) IBOutlet UIImageView *sampleImageView;



@end

@implementation OCRScannerViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    //Start Session
    //Capture Session
    AVCaptureSession *session = [[AVCaptureSession alloc]init];
    session.sessionPreset = AVCaptureSessionPresetPhoto;

    //Add device
    AVCaptureDevice *device =
    [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    //Input
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];

    if (!input)
    {
        NSLog(@"No Input");
    }
    [session addInput:input];

    //Output
    AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
    [session addOutput:output];
    output.videoSettings =
    @{ (NSString *)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA) };

    //Preview Layer
    AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    previewLayer.frame = self.cameraView.bounds;
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft];

    //Place Camera View behind all subviews
    [self.view.layer insertSublayer:previewLayer atIndex:0];

    //Start capture session
    [session startRunning];

}

- (UIImage *)takeSnapshotOfView:(UIView *)view
{
    UIGraphicsBeginImageContext(CGSizeMake(view.frame.size.width, view.frame.size.height));
    [view drawViewHierarchyInRect:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height) afterScreenUpdates:NO];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

1 个答案:

答案 0 :(得分:1)

您可以使用CIDetector检测卡片矩形并从原始图像中裁剪,然后从卡片图像中裁剪绿色矩形。最后,使用绿色矩形图像进行OCR。检测和裁剪的一个例子:

L = [1,2,3,4,5,6,7,8,9]
summ = 0

for val in L:
    summ = summ+val*(summ+val <= 25)