2017-08-22 10 views
0

enter image description hereAVCaptureSessionの一部をキャプチャする方法は?

私のアプリケーションでは、OCR Tesseractを使用してイメージをテキストに変換しようとしています。私はAVCaptureSession全体のスクリーンショットを撮る方法を学びましたが、緑の四角形の画像をキャプチャして、OCR Tesseractを簡単に変換し、よりクリーンなユーザーエクスペリエンスを実現したいだけです。

私は記事(下記)を読んでいますが、ビューの後ろにはAVCaptureSessionではなく、ビューのみをキャプチャしています。ここで

ios how to capture a particular portion of screen

あなたはカードの矩形を検出して、元の画像から、それをトリミングして、カードの画像から緑色の四角形をトリミングするCIDetectorを使用することができ、コード

@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

です。最後に、OCRの緑色の矩形画像を使用します。検出と切り取りの例:

CIImage *ciImage = image.CIImage; 
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeRectangle 
              context:nil 
              options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh, 
                CIDetectorTracking:@YES, 
                CIDetectorMinFeatureSize:@.5f}]; 

NSArray<CIRectangleFeature *> *rectangleFeatures = (NSArray<CIRectangleFeature *> *)[detector featuresInImage:ciImage]; 
for (CIRectangleFeature *rect in rectangleFeatures) 
{ 
    //find a proper rect, like card's width/height = 4:3 
    //following procedure is just an example, adjust it to fit your real needs. 
    CGFloat width = fabs(rect.topRight.x - rect.topLeft.x); 
    CGFloat height = fabs(rect.topLeft.y - rect.bottomLeft.y); 
    if ((width/height - 4/3) <= 0.1) { 
     CIImage *cardImage = [ciImage imageByCroppingToRect:rect.bounds]; //or create a custom rect to crop if it's not good. 
     CGRect greenRect = CGRectMake(0, rect.bounds.size.height * 0.8, rect.bounds.size.width, rect.bounds.size.height * 0.2); //in image coordinates 
     CIImage *greenRectCIImage = [cardImage imageByCroppingToRect:greenRect]; 

     UIImage *greenRectImage = [[UIImage alloc] initWithCIImage:greenRectCIImage]; 
     //use greenRectImage for OCR 

     return; 
    } 
} 
+0

緑色のボックスは、背景が透明なUIViewです。私はそれを明確な背景のUIImageに変更すべきですか?それはまだ動作しますか? – Mochi

+0

私にとって、緑色のボックスはイメージ内の矩形領域を示しているだけなので、ビュー階層は重要ではありません。 –

関連する問題