2014-03-14 40 views
1

我可以讀取條形碼,但無法獲取屏幕快照。 getScreenImage函數獲得一個白色屏幕。我怎樣才能得到屏幕截圖,包括我看到相機視圖的屏幕?謝謝。獲取唯一的白色屏幕截圖

@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不透明度的屏幕。 This link是我獲得幫助的地方。我怎樣才能得到它的100%不透明截圖?

_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]; 
    }]; 

} 
+0

如果您的部署目標是iOS的7.x及更高版本,使用'snapshotViewAfterScreenUpdates:','resizableSnapshotViewFromRect:afterScreenUpdates:withCapInsets:'或'drawViewHierarchyInRect:afterScreenUpdates:'。 – n00bProgrammer

+0

你只需要捕捉屏幕中的所有元素? –

+0

@Charan:是的。這是我需要的。 – user3309441

回答

0

我用AVCaptureStillImageOutput和它的工作更換。

-(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]; 

     }]; 

    } 
-1

試試這個這裏contentScrollview是我的滾動視圖我得到的所有內容,滾動型的屏幕截圖你的你的看法對象

- (UIImage *) imageFromViewIniOS7 
{ 
UIImage* image = nil; 
UIGraphicsBeginImageContext(contentScrollview.contentSize); 
{ 
    CGPoint savedContentOffset = contentScrollview.contentOffset; 
    CGRect savedFrame = contentScrollview.frame; 

    contentScrollview.contentOffset = CGPointZero; 
    contentScrollview.frame = CGRectMake(0, 0, contentScrollview.contentSize.width, contentScrollview.contentSize.height); 
    if ([[NSString versionofiOS] intValue]>=7) 
    { 
     [contentScrollview drawViewHierarchyInRect:contentScrollview.bounds afterScreenUpdates:YES]; 

    } 
    else 
    { 
     [contentScrollview.layer renderInContext: UIGraphicsGetCurrentContext()]; 

    } 
    image = UIGraphicsGetImageFromCurrentImageContext(); 

    contentScrollview.contentOffset = savedContentOffset; 
    contentScrollview.frame = savedFrame; 
} 
UIGraphicsEndImageContext(); 


return image; 
} 
+0

它不起作用。我檢查了我想要的屏幕。它是AVCaptureVideoPreviewLayer。 renderInContext無法到達它的圖層。我認爲這就是爲什麼屏幕截圖一直是白色的。 – user3309441

+0

I%50使它工作。你能檢查我編輯的問題嗎? – user3309441