For my application, I am trying to use OCR Tesseract to convert an image into text. I have learned how to take a screen shot of the whole AVCaptureSession but I only want to capture an image of the green square to make it easier for OCR Tesseract to convert and for a cleaner user experience.
I have read up on article (below) but it only captures the view and not AVCaptureSession behind the view.
ios how to capture a particular portion of screen
Here is the code
@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;
}