使用 avcapture 会话拉伸捕获图像

Posted

技术标签:

【中文标题】使用 avcapture 会话拉伸捕获图像【英文标题】:capture image is stretched using avcapture session 【发布时间】:2015-09-09 11:51:27 【问题描述】:

我正在使用 avcaptureSession 拍照 图片就在下面。 我使用正确的方法还是有什么问题? 我也更改了预设但没有成功

这是拍照前的图片

输出是这样的(拉伸)

我的代码是:

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

【问题讨论】:

在深入研究 AVCapture 代码之前,请检查您用于在网格中显示图像的 UIImageView 是否将其内容模式设置为“Aspect Fit”或“Aspect Fill”。您可能正确捕获了图像,但由于内容模式错误,图像显示为拉伸。 【参考方案1】:

我认为问题出在您的 UIImageViews 中。尝试将 contentMode 设置为UIViewContentModeScaleAspectFill

[imageView setContentMode:UIViewContentModeScaleAspectFill] 

【讨论】:

是的,确切的拉伸问题已经解决,现在您是否注意到拍摄区域比拍照前更宽了

以上是关于使用 avcapture 会话拉伸捕获图像的主要内容,如果未能解决你的问题,请参考以下文章

在 AVCapture Swift 中仅捕获相机预览

AvCapture / AVCaptureVideoPreviewLayer 无法获取正确的可见图像

当预设为640x480时,为什么使用AVFoundation捕获图像会给我480x640图像?

avcapture video 在改变方向时重复部分

在 iPhone 上静音 AVCapture 快门声音 [重复]

相机表面视图图像看起来拉伸