1

I am taking picture using avcaptureSession The images are right below . M i using right approach or is there something wrong? I also change preset but no sucess

Here is the image before taking picture enter image description here

output is like that(stretched) enter image description here

My Code is:

     AVCaptureDeviceInput*  input1 = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
    AVCaptureVideoDataOutput* output1 = [[AVCaptureVideoDataOutput alloc] init];
    output1.alwaysDiscardsLateVideoFrames = YES;
    dispatch_queue_t queue;
    queue = dispatch_queue_create("cameraQueue", NULL);
    [output1 setSampleBufferDelegate:self queue:queue];
    NSString* key = (NSString *) kCVPixelBufferPixelFormatTypeKey;
    NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
    NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];
    [output1 setVideoSettings:videoSettings];

    self.captureSession = [[AVCaptureSession alloc] init];

    [self.captureSession addInput:input1];
    [self.captureSession addOutput:output1];
    [self.captureSession setSessionPreset:AVCaptureSessionPresetiFrame960x540];

    self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
    self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

    // CHECK FOR YOUR APP
    self.previewLayer.frame = CGRectMake(self.cameraImageView.frame.origin.x, self.cameraImageView.frame.origin.y, self.img_view.frame.size.width, self.img_view.frame.size.height);


    [self.previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortrait];
       [self.view_captureImage.layer insertSublayer:self.previewLayer atIndex:0];           //[self.captureSession startRunning];


- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
    NSLog(@"im captueoutput");
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    CVPixelBufferLockBaseAddress(imageBuffer,0);
    uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
    CGImageRef newImage = CGBitmapContextCreateImage(newContext);
    CGContextRelease(newContext);
    CGColorSpaceRelease(colorSpace);



   self.cameraImage= [UIImage imageWithCGImage:newImage
                        scale:1.0
                  orientation: UIImageOrientationRight];
    CGImageRelease(newImage);
    CVPixelBufferUnlockBaseAddress(imageBuffer,0);
   // free(baseAddress);

}
- (IBAction)snapshot:(id)sender
{
        NSLog(@"image snap");
        [self.captureSession stopRunning];
        [self.cameraImageView setImage:self.cameraImage];
        UIImage *img=self.cameraImageView.image;
        [self.img_view setImage:img];
        [self.view_captureImage setHidden:YES];
}
zkn
  • 63
  • 1
  • 9
  • Before diving into AVCapture code, check if the UIImageView you've used to display images in the grid has it's content mode set to 'Aspect Fit' or 'Aspect Fill'. Chances are you're capturing the image correctly but the image shows up stretched because of wrong content mode. – Swapnil Luktuke Sep 09 '15 at 11:57

1 Answers1

3

i think the problem is in your UIImageViews. Try to set the contentMode to UIViewContentModeScaleAspectFill

[imageView setContentMode:UIViewContentModeScaleAspectFill] 
MarkHim
  • 5,686
  • 5
  • 32
  • 64
  • yup exactly stretching issue is reslolve now have you noticed the capture area is more wider than before taking picture – zkn Sep 09 '15 at 12:11