13

我正在使用某人的源代码来使用 AVCaptureSession 捕获图像。但是,我发现 CaptureSessionManager 的 previewLayer 是 shotter,然后是最终捕获的图像。在此处输入图像描述

我发现结果图像的比例始终为 720x1280=9:16。现在我想将生成的图像裁剪为比例为 320:480 的 UIImage,这样它只会捕获在 previewLayer 中可见的部分。任何想法?非常感谢。

stackoverflow 中的相关问题(还没有好的答案): Q1Q2

源代码:

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


在进行更多研究和测试后进行编辑: AVCaptureSession 的属性“sessionPreset”具有以下常量,我没有检查每一个,但注意到大多数比例是 9:16 或 3:4,

  • NSString *const AVCaptureSessionPresetPhoto;
  • NSString *const AVCaptureSessionPresetHigh;
  • NSString *const AVCaptureSessionPresetMedium;
  • NSString *const AVCaptureSessionPresetLow;
  • NSString *const AVCaptureSessionPreset352x288;
  • NSString *const AVCaptureSessionPreset640x480;
  • NSString *const AVCaptureSessionPresetiFrame960x540;
  • NSString *const AVCaptureSessionPreset1280x720;
  • NSString *const AVCaptureSessionPresetiFrame1280x720;

在我的项目中,我还有全屏预览(帧大小为 320x480): [[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];

我是这样做的:以 9:16 的尺寸拍摄照片并将其裁剪为 320:480,这正是预览层的可见部分。它看起来很完美。

用于调整大小和裁剪以替换旧代码的代码是

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

1 回答 1

39

iPhone 的摄像头本来就是 ​​4:3。您获得的 16:9 图像已经从 4:3 裁剪。将那些 16:9 的图像再次裁剪为 4:3 并不是您想要的。self.captureSession.sessionPreset = AVCaptureSessionPresetPhoto而是通过设置(在将任何输入/输出添加到会话之前)从 iPhone 的相机中获取本机 4:3 图像。

于 2012-01-10T08:35:42.387 回答