2010-12-14 13 views
6

मैं कुछ दिनों से इस पर अपने सिर पर टक्कर लगी हूं।एक AVCaptureVideoPreviewLayer के शीर्ष पर एक आयताकार ड्राइंग, संभव है?

मैं एक कैलियर (AVCaptureVideoPreviewLayer) के शीर्ष पर एक आयताकार बनाना चाहता हूं, जो कि आईफोन 4 पर कैमरे से वीडियो फीड होता है।

यहां मेरे सेटअप का हिस्सा है;

//(in function for initialization) 

     -(void)initDevices { 
      AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput     deviceInputWithDevice:[AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo] error:nil]; 

      AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init]; 
      captureOutput.alwaysDiscardsLateVideoFrames = YES; 
      captureOutput.minFrameDuration = CMTimeMake(1, 30); 
      dispatch_queue_t queue; 
      queue = dispatch_queue_create("cameraQueue", NULL); 
      [captureOutput setSampleBufferDelegate:self queue:queue]; 
      dispatch_release(queue); 

      NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey; 
      NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA]; 
      NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key]; 
      [captureOutput setVideoSettings:videoSettings]; 
      self.captureSession = [[AVCaptureSession alloc] init]; 
      [self.captureSession addInput:captureInput]; 
      [self.captureSession addOutput:captureOutput]; 
      [self.captureSession setSessionPreset:AVCaptureSessionPresetHigh]; 
      self.prevLayer = [AVCaptureVideoPreviewLayer layerWithSession: self.captureSession]; 
      self.prevLayer.frame = CGRectMake(0, 0, 400, 400); 
      self.prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
      self.prevLayer.delegate = self; 
      [self.view.layer addSublayer: self.prevLayer]; 
    } 

    - (void)captureOutput:(AVCaptureOutput *)captureOutput 
     didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
     fromConnection:(AVCaptureConnection *)connection { 

     [self performSelectorOnMainThread:@selector(drawme) withObject:nil waitUntilDone:YES]; 
    } 

    - (void)drawme { 
[self.prevLayer setNeedsDisplay]; 
    } 

    //delegate function that draws to a CALayer 
    - (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx { 
    NSLog(@"hello layer!"); 
    CGContextSetRGBFillColor (ctx, 1, 0, 0, 1); 
      CGContextFillRect (ctx, CGRectMake (0, 0, 200, 100)); 
    } 

क्या यह भी संभव है? मेरे वर्तमान कोड से, मुझे "हैलो लेयर" प्रिंटिंग मिलती है, लेकिन कैमरा फीड में कोई भरे आयत नहीं है।

कोई भी मदद अद्भुत होगी। :)

उत्तर

1

मुझे लगता है कि आपको AVCaptureVideoPreviewLayer में एक और परत जोड़नी चाहिए और मैं आपके लिए उदाहरण कोड संशोधित करता हूं। आप इसे आज़मा सकते हैं।

//(in function for initialization) 

    -(void)initDevices { 
     AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput     deviceInputWithDevice:[AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo] error:nil]; 

     AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init]; 
     captureOutput.alwaysDiscardsLateVideoFrames = YES; 
     captureOutput.minFrameDuration = CMTimeMake(1, 30); 
     dispatch_queue_t queue; 
     queue = dispatch_queue_create("cameraQueue", NULL); 
     [captureOutput setSampleBufferDelegate:self queue:queue]; 
     dispatch_release(queue); 

     NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey; 
     NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA]; 
     NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key]; 
     [captureOutput setVideoSettings:videoSettings]; 
     self.captureSession = [[AVCaptureSession alloc] init]; 
     [self.captureSession addInput:captureInput]; 
     [self.captureSession addOutput:captureOutput]; 
     [self.captureSession setSessionPreset:AVCaptureSessionPresetHigh]; 
     self.prevLayer = [AVCaptureVideoPreviewLayer layerWithSession: self.captureSession]; 
     self.prevLayer.frame = CGRectMake(0, 0, 400, 400); 
     self.prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
     [self.view.layer addSublayer:self.prevLayer]; 

     self.drawLayer = [CAShapeLayer layer]; 
     CGRect parentBox = [self.captureVideoPreviewLayer frame]; 
     [self.drawLayer setFrame:parentBox]; 
     [self.drawLayer setDelegate:self]; 
     [self.drawLayer setNeedsDisplay]; 
     [self.captureVideoPreviewLayer addSublayer:self.drawLayer]; 
} 

- (void)captureOutput:(AVCaptureOutput *)captureOutput 
    didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
    fromConnection:(AVCaptureConnection *)connection { 

    [self performSelectorOnMainThread:@selector(drawme) withObject:nil waitUntilDone:YES]; 
} 

- (void)drawme { 
    [self.drawLayer setNeedsDisplay]; 
} 

//delegate function that draws to a CALayer 
- (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx { 
    NSLog(@"hello layer!"); 
    CGContextSetRGBFillColor (ctx, 1, 0, 0, 1); 
    CGContextFillRect (ctx, CGRectMake (0, 0, 200, 100)); 
} 
0

या आप केवल एक छवि डालने कर सकते हैं - आप वीडियो स्पष्ट रूप से कब्जा करने के लिए AVCaptureVideoPreviewLayer उपयोग कर सकते हैं, और एक अन्य CALayer (बनाने) और layer.insertSublayer का उपयोग करें (..., ऊपर: ...) सम्मिलित करने के लिए अपने " कस्टम "परत वीडियो परत के ऊपर, और कस्टम द्वारा मैं बस अभी तक लेट के साथ एक और CALayer कहना मतलब

layer.contents = spinner.cgImage 

यहाँ और अधिक विस्तृत एक सा instructions for Swift

संबंधित मुद्दे