Ios 如何捕获AVCaptureSession的一部分?
对于我的应用程序,我尝试使用OCR Tesseract将图像转换为文本。我已经学会了如何拍摄整个AVCaptureSession的屏幕截图,但我只想拍摄一张绿色正方形的图像,以便于OCR Tesseract转换,并获得更清晰的用户体验 我已经阅读了文章(如下),但它只捕获视图,而不是视图后面的AVCaptureSession 这是密码Ios 如何捕获AVCaptureSession的一部分?,ios,xcode,ocr,Ios,Xcode,Ocr,对于我的应用程序,我尝试使用OCR Tesseract将图像转换为文本。我已经学会了如何拍摄整个AVCaptureSession的屏幕截图,但我只想拍摄一张绿色正方形的图像,以便于OCR Tesseract转换,并获得更清晰的用户体验 我已经阅读了文章(如下),但它只捕获视图,而不是视图后面的AVCaptureSession 这是密码 @interface OCRScannerViewController () @property (strong, nonatomic) IBOutlet UI
@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;
}
您可以使用CIDetector检测卡矩形并从原始图像中裁剪它,然后从卡图像中裁剪绿色矩形。最后,使用绿色矩形图像进行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;
}
}
CIImage*CIImage=image.CIImage;
CIDetector*detector=[CIDetector detectorOfType:CIDetectorTypeRectangle
上下文:无
选项:@{CIDetectorAccuracy:CIDetectorAccuracyHigh,
CIDetectorTracking:@是,
CIDetectorMinFeatureSize:@.5f}];
NSArray*矩形特征=(NSArray*)[探测器特征图像:ciImage];
用于(CIRectangleFeature*rect-in-rectangleFeatures)
{
//找到一个合适的矩形,比如卡片的宽度/高度=4:3
//以下步骤只是一个示例,请根据您的实际需要进行调整。
CGFloat width=fabs(rect.topRight.x-rect.topLeft.x);
CGFloat height=fabs(rect.topLeft.y-rect.bottomLeft.y);
如果((宽度/高度-4/3)绿色框是一个背景清晰的UIView。我是否应该将其更改为背景清晰的UIImage?它是否仍然有效?对我来说,绿色框只是指示并表示图像中的矩形区域,因此视图层次结构无关紧要。