仅获取白色屏幕截图 [英] Getting only white screenshot

查看:104
本文介绍了仅获取白色屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以读取条形码,但无法获取屏幕快照. getScreenImage函数获取白屏.如何获得屏幕截图,包括可以看到摄像机视图的屏幕?谢谢.

I can read the barcode but I can't get the snapshot of the screen. getScreenImage function gets a white screen. How can I get the screenshot including the screen which I see the camera view? Thank you.

@interface igViewController () <AVCaptureMetadataOutputObjectsDelegate,AVCaptureVideoDataOutputSampleBufferDelegate>
{
    AVCaptureSession *_session;
    AVCaptureDevice *_device;
    AVCaptureDeviceInput *_input;
    AVCaptureMetadataOutput *_output;
    AVCaptureVideoPreviewLayer *_prevLayer;

    UIView *_highlightView;
    UILabel *_label;
    UIImage *img;

}
@end
- (void)viewDidLoad
{
    [super viewDidLoad];

    _highlightView = [[UIView alloc] init];
    _highlightView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin;
    _highlightView.layer.borderColor = [UIColor greenColor].CGColor;
    _highlightView.layer.borderWidth = 3;
    [self.view addSubview:_highlightView];

    _label = [[UILabel alloc] init];
    _label.frame = CGRectMake(0, self.view.bounds.size.height -100, self.view.bounds.size.width, 100);
    _label.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    _label.backgroundColor = [UIColor colorWithWhite:0.15 alpha:0.65];
    _label.textColor = [UIColor whiteColor];
    _label.textAlignment = NSTextAlignmentCenter;


    //[_label addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:NULL];
    [self.view addSubview:_label];

    _session = [[AVCaptureSession alloc] init];
    _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    NSError *error = nil;

    _input = [AVCaptureDeviceInput deviceInputWithDevice:_device error:&error];
    if (_input) {
        [_session addInput:_input];
    } else {
        NSLog(@"Error: %@", error);
    }

    _output = [[AVCaptureMetadataOutput alloc] init];
    [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    [_session addOutput:_output];

    _output.metadataObjectTypes = [_output availableMetadataObjectTypes];

    _prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
    _prevLayer.frame = self.view.bounds;
    _prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.view.layer addSublayer:_prevLayer];
    [_session startRunning];

    [self.view bringSubviewToFront:_highlightView];
    [self.view bringSubviewToFront:_label];
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    CGRect highlightViewRect = CGRectZero;
    AVMetadataMachineReadableCodeObject *barCodeObject;
    detectionString = nil;
    NSArray *barCodeTypes = @[AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code,
            AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeCode128Code,
            AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode];

    for (AVMetadataObject *metadata in metadataObjects) {

        for (NSString *type in barCodeTypes) {
            if ([metadata.type isEqualToString:type])
            {
                barCodeObject = (AVMetadataMachineReadableCodeObject *)[_prevLayer transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject *)metadata];
                highlightViewRect = barCodeObject.bounds;
                detectionString = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
                break;
            }
        }

        _highlightView.frame = highlightViewRect;
        if (detectionString != nil)
        {    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
            [self getScreenImage];
            _label.text = detectionString;
            [self performSelector:@selector(changeText) withObject:nil afterDelay:2.0];
            break;
        }
    }

    if(detectionString!=nil){
        [MainViewController setResultTexts:detectionString img:img];
        detectionString=nil;
        [self dismissViewControllerAnimated:YES completion:nil];
        [_session stopRunning];

    }
}
CGImageRef UIGetScreenImage(void);
-(void)getScreenImage{
    if(detectionString!=nil){
    CGImageRef screen = UIGetScreenImage();
    img = [UIImage imageWithCGImage:screen];
    CGImageRelease(screen);
    }
}

这有效.但是需要更快地更改会话的输出.因为会话更改为捕获图像.因此屏幕将消失一秒钟.而且我只得到%50不透明度的屏幕. 此链接是我获得帮助的地方.我现在如何获得它的100%不透明度屏幕截图?

This works. But need to make output change for session more quickly. Because the session changing to capture an image. So the screen is getting disappear for a second. And I only get screen with %50 opacity. This link is where I get help. How can I get a 100% opacity screenshot of it now?

_highlightView.frame = highlightViewRect;
        if (detectionString != nil)
        {
            **[_session removeOutput:_output];
            [_session addOutput:_stillImageOutput];**
            _label.text = detectionString;
            **[self captureNow];**
            [self performSelector:@selector(changeText) withObject:nil afterDelay:1.0];
            break;
        }

-(void)captureNow {
    AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection in _stillImageOutput.connections)
    {
        for (AVCaptureInputPort *port in [connection inputPorts])
        {
            if ([[port mediaType] isEqual:AVMediaTypeVideo] )
            {
                videoConnection = connection;
                break;
            }
        }
        if (videoConnection)
        {
            break;
        }
    }

    NSLog(@"about to request a capture from: %@", _stillImageOutput);
    [_stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
     {
         NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
         img = [[UIImage alloc] initWithData:imageData];
     }];

}

推荐答案

我使用了AVCaptureStillImageOutput,并且有效.

I used AVCaptureStillImageOutput and it worked.

    -(void)captureNow {
        AVCaptureConnection *videoConnection = nil;
        for (AVCaptureConnection *connection in _stillImageOutput.connections)
        {
            for (AVCaptureInputPort *port in [connection inputPorts])
            {
                if ([[port mediaType] isEqual:AVMediaTypeVideo] )
                {
                    videoConnection = connection;
                    break;
                }
            }
            if (videoConnection)
            {
                break;
            }
        }


        [_stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
         {
             NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
             img = [[UIImage alloc] initWithData:imageData];
//I used image in another viewcontroller
             [MainViewController setResultTexts:str img:img];
             [MainViewController set_from_view:0 scanner:1];
             [self dismissViewControllerAnimated:YES completion:nil];

         }];

    }

这篇关于仅获取白色屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆