2012-01-26 13 views
18

यह एक साधारण काम की तरह लगता है, फिर भी यह मुझे पागल कर रहा है। क्या UIView को AVCaptureVideoPreviewLayer को सहेजने के लिए एक छवि में एक उपन्यास के रूप में परिवर्तित करना संभव है? मैं एक उन्नत वास्तविकता ओवरले बनाना चाहता हूं और एक बटन को कैमरा रोल में चित्र सहेजना चाहता हूं। पावर बटन + होम कुंजी को पकड़ना कैमरा रोल पर स्क्रीनशॉट को कैप्चर करता है, जिसका अर्थ है कि मेरा सभी कैप्चर तर्क काम कर रहा है, और कार्य संभव है। लेकिन मैं इसे प्रोग्रामेटिक रूप से काम करने में सक्षम नहीं लग सकता।क्या ग्राफिक्स संदर्भ में AVCaptureVideoPreviewLayer प्रस्तुत करना संभव है?

मैं AVCaptureVideoPreviewLayer का उपयोग कर कैमरे की छवि का लाइव पूर्वावलोकन कैप्चर कर रहा हूं। छवि प्रस्तुत करने के लिए मेरे प्रयास की सभी असफल:

previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession]; 
//start the session, etc... 


//this saves a white screen 
- (IBAction)saveOverlay:(id)sender { 
    NSLog(@"saveOverlay"); 

    UIGraphicsBeginImageContext(appDelegate.window.bounds.size); 
     UIGraphicsBeginImageContext(scrollView.frame.size); 

    [previewLayer.presentationLayer renderInContext:UIGraphicsGetCurrentContext()]; 


// [appDelegate.window.layer renderInContext:UIGraphicsGetCurrentContext()]; 

    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    UIImageWriteToSavedPhotosAlbum(screenshot, self, 
            @selector(image:didFinishSavingWithError:contextInfo:), nil); 
} 

// इस सब कुछ प्रस्तुत हुई है, पूर्वावलोकन परत है, जो खाली है के लिए छोड़कर।

[appDelegate.window.layer renderInContext:UIGraphicsGetCurrentContext()]; 

मैंने कहीं पढ़ा है कि यह आईफोन के सुरक्षा मुद्दों के कारण हो सकता है। क्या ये सच है?

बस स्पष्ट होने के लिए: मैं कैमरे के लिए छवि को सहेजना नहीं चाहता। मैं पारदर्शी पूर्वावलोकन परत को दूसरी छवि पर अतिरंजित करना चाहता हूं, इस प्रकार पारदर्शिता बना रहा हूं। फिर भी किसी कारण से मैं इसे काम नहीं कर सकता।

उत्तर

4

मैं आपको GPU छवि आज़माने की सलाह दे सकता हूं।

https://github.com/BradLarson/GPUImage

यह ओपन का उपयोग करता है, तो यह नहीं बल्कि तेज है। यह कैमरे से चित्रों को संसाधित कर सकता है और उनमें से फ़िल्टर जोड़ सकता है (उनमें से बहुत सारे हैं) किनारे का पता लगाने, गति का पता लगाने और बहुत अधिक

यह ओपनसीवी की तरह है लेकिन मेरे अपने अनुभव के आधार पर जीपीयू छवि आपके प्रोजेक्ट से कनेक्ट करना आसान है और भाषा उद्देश्य-सी है। अगर आप भौतिकी के लिए box2d उपयोग करने का फैसला

समस्या दिखाई दे सकता है - है भी ओपन का उपयोग करता है और आप खर्च इस 2 चौखटे तक कुछ समय की जरूरत है लड़ बंद हो जाएगा))

+0

मुझे लगता है कि क्या ओ पी और मैं कोशिश कर रहे हैं, avcapturevideopreviewlayer का उपयोग कर एक लाइव वीडियो फीड प्राप्त करने के लिए है और एक आकार बदली गई चित्र जोड़ने की कोशिश कर रहे हैं। मैं फेस डिटेक्शन के आधार पर लाइव वीडियो फीड के शीर्ष पर एक स्केल की गई छवि जोड़ने की कोशिश कर रहा हूं, और सहेजा गया स्क्रीनशॉट केवल ओवरले का है और avcapturevideopreviewlayer नहीं है। –

+0

जीपीयू छवि में पहले परतों पर फ़िल्टर जोड़ते हैं, इसलिए मुझे पूरा यकीन है कि केवल मौजूदा परत को सहेजने का तरीका है (GPI छवि में विधि है जो फ्रेम से UIImage बनाने के लिए है) यदि आप डिस्प्ले पर क्या चाहते हैं - वहां स्क्रीनशॉट प्रोग्रामेटिक रूप से बनाने का तरीका है। मुझे अब याद नहीं है लेकिन मैंने इसे स्टैक ओवरफ्लो पर देखा है) – Roma

+0

रोमा, आपके उत्तर के लिए बहुत बहुत धन्यवाद। हालांकि मैंने GPUImage का उपयोग नहीं किया है, मैं निश्चित रूप से जल्द ही इसका उपयोग करूँगा। इसे वैकल्पिक विकल्प के रूप में पोस्ट करने के लिए धन्यवाद। –

15

मुझे पसंद है @ GPU छवि का उपयोग करने का रोमा के सुझाव होगा - महान विचार। । । । लेकिन अगर आप एक शुद्ध CocoaTouch दृष्टिकोण चाहते हैं, यहाँ क्या करना है:

लागू AVCaptureVideoDataOutputSampleBufferDelegate

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
fromConnection:(AVCaptureConnection *)connection 
{ 
    // Create a UIImage+Orientation from the sample buffer data 
    if (_captureFrame) 
    { 
     [captureSession stopRunning]; 

     _captureFrame = NO; 
     UIImage *image = [ImageTools imageFromSampleBuffer:sampleBuffer]; 
     image = [image rotate:UIImageOrientationRight]; 

     _frameCaptured = YES; 

     if (delegate != nil) 
     { 
      [delegate cameraPictureTaken:image]; 
     } 
    } 
} 

कब्जा इस प्रकार है:

+ (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer 
{ 
    // Get a CMSampleBuffer's Core Video image buffer for the media data 
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 
    // Lock the base address of the pixel buffer 
    CVPixelBufferLockBaseAddress(imageBuffer, 0); 

    // Get the number of bytes per row for the pixel buffer 
    void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer); 

    // Get the number of bytes per row for the pixel buffer 
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); 
    // Get the pixel buffer width and height 
    size_t width = CVPixelBufferGetWidth(imageBuffer); 
    size_t height = CVPixelBufferGetHeight(imageBuffer); 

    // Create a device-dependent RGB color space 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

    // Create a bitmap graphics context with the sample buffer data 
    CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8, 
              bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); 
    // Create a Quartz image from the pixel data in the bitmap graphics context 
    CGImageRef quartzImage = CGBitmapContextCreateImage(context); 
    // Unlock the pixel buffer 
    CVPixelBufferUnlockBaseAddress(imageBuffer,0); 

    // Free up the context and color space 
    CGContextRelease(context); 
    CGColorSpaceRelease(colorSpace); 

    // Create an image object from the Quartz image 
    UIImage *image = [UIImage imageWithCGImage:quartzImage]; 

    // Release the Quartz image 
    CGImageRelease(quartzImage); 

    return (image); 
} 

ब्लेंड ओवरले साथ UIImage

  • अब आपके पास UIImage है, इसे एक नए UIView में जोड़ें।
  • उप-दृश्य के रूप में शीर्ष पर ओवरले जोड़ें।

कैद नई UIView

+ (UIImage*)imageWithView:(UIView*)view 
{ 
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, [UIScreen mainScreen].scale); 
    [view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage* img = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return img; 
} 
+0

मुझे लगता है कि यह वही है जो मैं ढूंढ रहा हूं। यह दौड़ने की कोशिश करेगा और आपको यह बताएगा कि यह कैसा चल रहा है। त्वरित उत्तर के लिए धन्यवाद! –

+0

क्या आप छवि के लिए uiimage एक्सटेंशन का उपयोग कर रहे हैं [छवि घुमाने: UIImageOrientationRight]; ? –

+0

हां, वह चरण वैकल्पिक है। हालांकि, मुझे आपको कोड देने दो। –

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