2012-01-10 14 views
12

से आईफोन छवि अनुपात कैप्चर किया गया है मैं AVCaptureSession के साथ छवि को कैप्चर करने के लिए किसी के स्रोत कोड का उपयोग कर रहा हूं। हालांकि, मैंने पाया कि कैप्चर सत्र प्रबंधक का पूर्वावलोकन लेयर अंतिम कैप्चर की गई छवि के बाद शटर है। enter image description hereAVCaptureSession

मुझे पता चला कि परिणामस्वरूप छवि हमेशा 720x1280 = 9: 16 अनुपात के साथ है। अब मैं परिणामस्वरूप छवि को अनुपात 320: 480 के साथ यूआईएममेज में फसल करना चाहता हूं ताकि यह केवल पूर्वावलोकन लेयर में दिखाई देने वाले हिस्से को कैप्चर करे। कोई उपाय? बहुत बहुत धन्यवाद।

प्रासंगिक stackoverflow में प्रश्न (सं अच्छा जवाब अभी तक): Q1, Q2

स्रोत कोड: कुछ और अधिक शोध और परीक्षण करने के बाद

- (id)init { 
if ((self = [super init])) { 
    [self setCaptureSession:[[[AVCaptureSession alloc] init] autorelease]]; 
} 
return self; 
} 

- (void)addVideoPreviewLayer { 
[self setPreviewLayer:[[[AVCaptureVideoPreviewLayer alloc] initWithSession:[self captureSession]] autorelease]]; 
[[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill]; 

} 

- (void)addVideoInput { 
AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 
if (videoDevice) { 
    NSError *error; 


    if ([videoDevice isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus] && [videoDevice lockForConfiguration:&error]) { 
     [videoDevice setFocusMode:AVCaptureFocusModeContinuousAutoFocus]; 
     [videoDevice unlockForConfiguration]; 
    }   

    AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error]; 
    if (!error) { 
     if ([[self captureSession] canAddInput:videoIn]) 
      [[self captureSession] addInput:videoIn]; 
     else 
      NSLog(@"Couldn't add video input");  
    } 
    else 
     NSLog(@"Couldn't create video input"); 
} 
else 
    NSLog(@"Couldn't create video capture device"); 
} 

- (void)addStillImageOutput 
{ 
    [self setStillImageOutput:[[[AVCaptureStillImageOutput alloc] init] autorelease]]; 
    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey,nil]; 
    [[self stillImageOutput] setOutputSettings:outputSettings]; 

    AVCaptureConnection *videoConnection = nil; 
    for (AVCaptureConnection *connection in [[self stillImageOutput] connections]) { 
    for (AVCaptureInputPort *port in [connection inputPorts]) { 
     if ([[port mediaType] isEqual:AVMediaTypeVideo]) { 
     videoConnection = connection; 
     break; 
     } 
    } 
    if (videoConnection) { 
     break; 
    } 
    } 

    [[self captureSession] addOutput:[self stillImageOutput]]; 
} 

- (void)captureStillImage 
{ 
AVCaptureConnection *videoConnection = nil; 
for (AVCaptureConnection *connection in [[self 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: %@", [self stillImageOutput]); 
[[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection 
                completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) { 
                CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL); 
                if (exifAttachments) { 
                 NSLog(@"attachements: %@", exifAttachments); 
                } else { 
                 NSLog(@"no attachments"); 
                } 
                NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];  
                UIImage *image = [[UIImage alloc] initWithData:imageData]; 
                [self setStillImage:image]; 
                [image release]; 
                [[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:nil]; 
                }]; 
} 


संपादित करें: AVCapture सत्र की संपत्ति "sessionPreset" में निम्नलिखित स्थिरांक हैं, मेरे पास है उनमें से प्रत्येक की जांच नहीं की गई, लेकिन ध्यान दिया गया कि उनमें से अधिकतर अनुपात 9:16 है, या 3: 4,

  • एनएसएसटींग * कॉन्स AVCaptureSessionPresetPhoto;
  • एनएसएसटींग * कॉन्स AVCaptureSessionPresetHigh;
  • एनएसएसटींग * कॉन्स AVCaptureSessionPresetMedium;
  • एनएसएसटींग * कॉन्स AVCaptureSessionPresetLow;
  • एनएसएसटींग * कॉन्स AVCaptureSessionPreset352x288;
  • एनएसएसटींग * कॉन्स AVCaptureSessionPreset640x480;
  • एनएसएसटींग * कॉन्स AVCaptureSessionPresetiFrame960x540;
  • एनएसएसटींग * कॉन्स AVCaptureSessionPreset1280x720;
  • एनएसएसटींग * कॉन्स AVCaptureSessionPresetiFrame1280x720;

मेरे परियोजना में, मैं पूर्ण स्क्रीन पूर्वावलोकन (फ्रेम आकार 320x480 है) भी: [[आत्म previewLayer] setVideoGravity: AVLayerVideoGravityResizeAspectFill];

मैंने इसे इस तरह से किया है: फोटो 9:16 आकार में लें और इसे 320: 480 तक फसल करें, बिल्कुल पूर्वावलोकन प्लेयर का दृश्य भाग। यह सही दिखता है।

आकार बदलने और पुराने कोड के साथ बदलने के croping के लिए कोड का टुकड़ा

NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; 
UIImage *image = [UIImage imageWithData:imageData]; 
UIImage *scaledimage=[ImageHelper scaleAndRotateImage:image]; 
//going to crop the image 9:16 to 2:3;with Width fixed 
float width=scaledimage.size.width; 
float height=scaledimage.size.height; 
float top_adjust=(height-width*3/2.0)/2.0; 
[self setStillImage:[scaledimage croppedImage:rectToCrop]]; 
+0

आप 4: 3-सत्र प्रिसेट का उपयोग क्यों नहीं करते हैं, कि आपको पूरे कैमरे-सेंसर का फसल और उपयोग करने की आवश्यकता नहीं है? –

उत्तर

28

iPhone के कैमरे देशी रूप 4 है: 3। आपके द्वारा प्राप्त 16: 9 छवियां पहले से ही 4: 3 से फसल हो चुकी हैं। उन 16: 9 छवियों को फिर से 4: 3 तक फसल करना आप जो चाहते हैं वह नहीं है। इसके बजाय self.captureSession.sessionPreset = AVCaptureSessionPresetPhoto (सत्र में कोई भी इनपुट/आउटपुट जोड़ने से पहले) को आईफोन के कैमरे से देशी 4: 3 छवियां प्राप्त करें।

+1

डोमिनिक, आपके उत्तर के लिए धन्यवाद। आईफोन कैमरा ऐप में नीचे एक टूल बार है, पूर्वावलोकन अनुपात (480-44) है: 320 ~ = 4: 3 = 1.3333, एल्बम ऐप में खोलें, पहलू 480: 320 = 1.50 की तरह दिखता है, इसलिए वास्तव में तस्वीर विकृत हो जाती है पहले से ही सही है? लेकिन चूंकि 1.333 1 से बहुत अलग नहीं है।50, इसलिए उपयोगकर्ता को –

+0

पर इतना स्पष्ट नहीं दिखता है, हां, एल्बम-एप पूरी स्क्रीन को भरने के लिए शुरुआत में फ्लाई पर चित्रों को फसल (पहलू भरने, कोई विरूपण नहीं) फसल पर फसल कर रहा है। लेकिन एक चुटकी इशारा के साथ आप पूरी 4: 3 तस्वीर देख सकते हैं। –

+0

धन्यवाद मेरी समस्या का समाधान – Naresh

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