TesseractOCR 文字识别 自定义拍照界面(AVFoundation)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_30190885/article/details/52174595

<pre name="code" class="objc">//头文件
<pre name="code" class="objc">#import "MyViewController.h"
#import <TesseractOCR/TesseractOCR.h>
#import "GPUImage.h"
#import <AVFoundation/AVFoundation.h>
#import "CameraViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import <GLKit/GLKit.h>


 
 
//需要引入的 代理
@interface MyViewController ()<G8TesseractDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, AVCaptureMetadataOutputObjectsDelegate, AVCaptureVideoDataOutputSampleBufferDelegate>{
    AVCaptureSession *session;
    
}


 
 
</pre><pre name="code" class="objc">#viewwilldid load 里面写的
AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    //创建输入流
    AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
    //创建输出流
    AVCaptureVideoDataOutput * output = [[AVCaptureVideoDataOutput alloc]init];
    //设置代理 在主线程里刷新
    [output setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
    //初始化链接对象
    session = [[AVCaptureSession alloc]init];
    //高质量采集率
    [session setSessionPreset:AVCaptureSessionPresetHigh];
    
    
    self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

    self.glkView = [[GLKView alloc] initWithFrame:CGRectMake(0, 0, systemHeight, systemWidth) context:self.context];
    
    self.cicontext = [CIContext contextWithEAGLContext:self.context];
    [self.view addSubview:self.glkView];<pre name="code" class="objc">[session addOutput:output];
    [session addInput:input];

 
 
<pre name="code" class="objc">#pragma mark AVCaptureVideoDataOutputSampleBufferDelegate(代理)
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection{
    
    CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    CIImage *image = [CIImage imageWithCVPixelBuffer:pixelBuffer];
    if (self.context != [EAGLContext currentContext]) {
        [EAGLContext setCurrentContext:self.context];
    }
    [self.glkView bindDrawable];
    [self.cicontext drawImage:image inRect:[image extent] fromRect:[image extent]];
    [self.glkView display];
    
    
    CGAffineTransform at = CGAffineTransformMakeRotation(M_PI/2);
    at = CGAffineTransformTranslate(at, 0, 0);
    
    [self.glkView setTransform:at];
    
    self.glkView.center = self.view.center;
    
    self.myImage = [self.glkView snapshot];
    
}

#pragma mark 点击拍照按钮(自己随便加一个按钮就行)
- (IBAction)onoffButtonClick:(id)sender {
    
    if ([session isRunning]) {
        //停止捕获
        [session stopRunning];
        
        UIImage *image = [self image:self.myImage rotation:UIImageOrientationRight];
        
        int x = image.size.width/systemWidth;
        int y = image.size.height/systemHeight;
        
        UIImage *getImage = [self imageFromImage:image inRect:CGRectMake(self.redView.frame.origin.x * x, self.redView.frame.origin.y * y, self.redView.frame.size.width * x, self.redView.frame.size.height * y)];
        
        [self getStringFrom:getImage];
//        UIImageWriteToSavedPhotosAlbum( getImage, nil, nil, nil);
        
    }else{
        //开始捕获
        [session startRunning];
    }
}

//旋转图片(glkview 需要旋转 所以图片也是)
-(UIImage *)image:(UIImage *)image rotation:(UIImageOrientation)orientation
{
    long double rotate = 0.0;
    CGRect rect;
    float translateX = 0;
    float translateY = 0;
    float scaleX = 1.0;
    float scaleY = 1.0;
    
    switch (orientation) {
        case UIImageOrientationLeft:
            rotate = M_PI_2;
            rect = CGRectMake(0, 0, image.size.height, image.size.width);
            translateX = 0;
            translateY = -rect.size.width;
            scaleY = rect.size.width/rect.size.height;
            scaleX = rect.size.height/rect.size.width;
            break;
        case UIImageOrientationRight:
            rotate = 3 * M_PI_2;
            rect = CGRectMake(0, 0, image.size.height, image.size.width);
            translateX = -rect.size.height;
            translateY = 0;
            scaleY = rect.size.width/rect.size.height;
            scaleX = rect.size.height/rect.size.width;
            break;
        case UIImageOrientationDown:
            rotate = M_PI;
            rect = CGRectMake(0, 0, image.size.width, image.size.height);
            translateX = -rect.size.width;
            translateY = -rect.size.height;
            break;
        default:
            rotate = 0.0;
            rect = CGRectMake(0, 0, image.size.width, image.size.height);
            translateX = 0;
            translateY = 0;
            break;
    }
    
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    //做CTM变换
    CGContextTranslateCTM(context, 0.0, rect.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextRotateCTM(context, rotate);
    CGContextTranslateCTM(context, translateX, translateY);
    
    CGContextScaleCTM(context, scaleX, scaleY);
    //绘制图片
    CGContextDrawImage(context, CGRectMake(0, 0, rect.size.width, rect.size.height), image.CGImage);
    
    UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext();
    
    return newPic;
}
//识别
- (void)getStringFrom:(UIImage *)image{
    G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:@"eng"];
    tesseract.pageSegmentationMode = G8PageSegmentationModeAutoOnly;
    tesseract.engineMode = G8OCREngineModeTesseractOnly;//让他更准
    tesseract.maximumRecognitionTime = 3.0;
    tesseract.charWhitelist = @"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNO0PQRSTUVWXYZ";
    
    GPUImageAdaptiveThresholdFilter *fileter = [[GPUImageAdaptiveThresholdFilter alloc] init];
    
    fileter.blurRadiusInPixels = 2;
    
    tesseract.image = [fileter imageByFilteringImage:[self Erzhiimage:image]];
    
    
    [tesseract recognize];
  
    
    
    self.mylabel.text = [tesseract recognizedText];
    NSLog(@"%@", [tesseract recognizedText]);
    
    [self convertImage:image];
    
    [G8Tesseract clearCache];
}

//从图片中截取一部分图片(按比例)
- (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect {
    CGImageRef sourceImageRef = [image CGImage];
    CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
    return newImage;
}


 
 
 
 

猜你喜欢

转载自blog.csdn.net/sinat_30190885/article/details/52174595