2013-04-11 41 views
32

这使我发疯,因为我无法使其工作。我有以下情况:如何从AVCapture中将图像裁剪到在显示器上看到的矩形

我使用AVCaptureSessionAVCaptureVideoPreviewLayer来创建我自己的相机界面。界面显示一个矩形。以下是填充整个屏幕的AVCaptureVideoPreviewLayer

我希望以某种方式裁剪捕获的图像,使得生成的图像精确地显示在显示器上的矩形中显示的内容。

我的设置是这样的:

_session = [[AVCaptureSession alloc] init]; 
AVCaptureSession *session = _session; 
session.sessionPreset = AVCaptureSessionPresetPhoto; 

AVCaptureDevice *camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 
if (camera == nil) { 
    [self showImagePicker]; 
    _isSetup = YES; 
    return; 
} 
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; 
captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 

captureVideoPreviewLayer.frame = self.liveCapturePlaceholderView.bounds; 
[self.liveCapturePlaceholderView.layer addSublayer:captureVideoPreviewLayer]; 

NSError *error; 
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:camera error:&error]; 
if (error) { 
    HGAlertViewWrapper *av = [[HGAlertViewWrapper alloc] initWithTitle:kFailedConnectingToCameraAlertViewTitle message:kFailedConnectingToCameraAlertViewMessage cancelButtonTitle:kFailedConnectingToCameraAlertViewCancelButtonTitle otherButtonTitles:@[kFailedConnectingToCameraAlertViewRetryButtonTitle]]; 
    [av showWithBlock:^(NSString *buttonTitle){ 
     if ([buttonTitle isEqualToString:kFailedConnectingToCameraAlertViewCancelButtonTitle]) { 
      [self.delegate gloameCameraViewControllerDidCancel:self]; 
     } 
     else { 
      [self setupAVSession]; 
     } 
    }]; 
} 
[session addInput:input]; 

NSDictionary *options = @{ AVVideoCodecKey : AVVideoCodecJPEG }; 
_stillImageOutput = [[AVCaptureStillImageOutput alloc] init]; 
[_stillImageOutput setOutputSettings:options]; 

[session addOutput:_stillImageOutput]; 

[session startRunning]; 
_isSetup = YES; 

我捕捉这样的形象:

[_stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) 
{ 
    if (error) { 
     MWLogDebug(@"Error capturing image from camera. %@, %@", error, [error userInfo]); 
     _capturePreviewLayer.connection.enabled = YES; 
    } 
    else 
    { 
     NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; 
     UIImage *image = [[UIImage alloc] initWithData:imageData]; 

     CGRect cropRect = [self createCropRectForImage:image]; 
     UIImage *croppedImage;// = [self cropImage:image toRect:cropRect]; 
     UIGraphicsBeginImageContext(cropRect.size); 
     [image drawAtPoint:CGPointMake(-cropRect.origin.x, -cropRect.origin.y)]; 
     croppedImage = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
     self.capturedImage = croppedImage; 
     [_session stopRunning];    
    } 
}]; 

createCropRectForImage:方法我试过各种方法来计算矩形切出的形象,但迄今为止没有成功。

- (CGRect)createCropRectForImage:(UIImage *)image 
{ 
    CGPoint maskTopLeftCorner = CGPointMake(self.maskRectView.frame.origin.x, self.maskRectView.frame.origin.y); 
    CGPoint maskBottomRightCorner = CGPointMake(self.maskRectView.frame.origin.x + self.maskRectView.frame.size.width, self.maskRectView.frame.origin.y + self.maskRectView.frame.size.height); 

    CGPoint maskTopLeftCornerInLayerCoords = [_capturePreviewLayer convertPoint:maskTopLeftCorner fromLayer:self.maskRectView.layer.superlayer]; 
    CGPoint maskBottomRightCornerInLayerCoords = [_capturePreviewLayer convertPoint:maskBottomRightCorner fromLayer:self.maskRectView.layer.superlayer]; 
    CGPoint maskTopLeftCornerInDeviceCoords = [_capturePreviewLayer captureDevicePointOfInterestForPoint:maskTopLeftCornerInLayerCoords]; 
    CGPoint maskBottomRightCornerInDeviceCoords = [_capturePreviewLayer captureDevicePointOfInterestForPoint:maskBottomRightCornerInLayerCoords]; 

    float x = maskTopLeftCornerInDeviceCoords.x * image.size.width; 
    float y = (1 - maskTopLeftCornerInDeviceCoords.y) * image.size.height; 
    float width = fabsf(maskTopLeftCornerInDeviceCoords.x - maskBottomRightCornerInDeviceCoords.x) * image.size.width; 
    float height = fabsf(maskTopLeftCornerInDeviceCoords.y - maskBottomRightCornerInDeviceCoords.y) * image.size.height; 

    return CGRectMake(x, y, width, height); 
} 

这是我目前的版本,但没有得到正确的比例。有人可以帮助我!

- (UIImage*)cropImage:(UIImage*)originalImage toRect:(CGRect)rect{ 

    CGImageRef imageRef = CGImageCreateWithImageInRect([originalImage CGImage], rect); 

    CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef); 
    CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef); 
    CGContextRef bitmap = CGBitmapContextCreate(NULL, rect.size.width, rect.size.height, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo); 

    if (originalImage.imageOrientation == UIImageOrientationLeft) { 
     CGContextRotateCTM (bitmap, radians(90)); 
     CGContextTranslateCTM (bitmap, 0, -rect.size.height); 

    } else if (originalImage.imageOrientation == UIImageOrientationRight) { 
     CGContextRotateCTM (bitmap, radians(-90)); 
     CGContextTranslateCTM (bitmap, -rect.size.width, 0); 

    } else if (originalImage.imageOrientation == UIImageOrientationUp) { 
     // NOTHING 
    } else if (originalImage.imageOrientation == UIImageOrientationDown) { 
     CGContextTranslateCTM (bitmap, rect.size.width, rect.size.height); 
     CGContextRotateCTM (bitmap, radians(-180.)); 
    } 

    CGContextDrawImage(bitmap, CGRectMake(0, 0, rect.size.width, rect.size.height), imageRef); 
    CGImageRef ref = CGBitmapContextCreateImage(bitmap); 

    UIImage *resultImage=[UIImage imageWithCGImage:ref]; 
    CGImageRelease(imageRef); 
    CGContextRelease(bitmap); 
    CGImageRelease(ref); 

    return resultImage; 
} 

没有任何人有方法的“最佳组合”,使这项工作:

我也用这个方法来裁剪我的形象试过吗? :)

+1

你解决问题了吗?我正在寻找答案。 – kkocabiyik 2013-04-23 00:29:30

回答

40

我已经使用metadataOutputRectOfInterestForRect函数解决了这个问题。

它适用于任何方向。

[_stillImageOutput captureStillImageAsynchronouslyFromConnection:stillImageConnection 
               completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) 
{ 
    if (error) 
    { 
     [_delegate cameraView:self error:@"Take picture failed"]; 
    } 
    else 
    { 

     NSData *jpegData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; 
     UIImage *takenImage = [UIImage imageWithData:jpegData]; 

     CGRect outputRect = [_previewLayer metadataOutputRectOfInterestForRect:_previewLayer.bounds]; 
     CGImageRef takenCGImage = takenImage.CGImage; 
     size_t width = CGImageGetWidth(takenCGImage); 
     size_t height = CGImageGetHeight(takenCGImage); 
     CGRect cropRect = CGRectMake(outputRect.origin.x * width, outputRect.origin.y * height, outputRect.size.width * width, outputRect.size.height * height); 

     CGImageRef cropCGImage = CGImageCreateWithImageInRect(takenCGImage, cropRect); 
     takenImage = [UIImage imageWithCGImage:cropCGImage scale:1 orientation:takenImage.imageOrientation]; 
     CGImageRelease(cropCGImage); 

    } 
} 
]; 

The takenImage is still imageOrientation dependent image。您可以删除进一步图像处理的方向信息。

UIGraphicsBeginImageContext(takenImage.size); 
[takenImage drawAtPoint:CGPointZero]; 
takenImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
+0

很好,代码不错 – Shashi3456643 2016-03-17 21:59:53

+0

@unknownStack我有同样的问题,但是你的代码并没有解决我的问题。我保存在专辑库中的起源和图像,但他们是一样的:(但当它设置在一个uiimageView中,它显示了一个非常放大的图像:( – 2016-12-26 08:22:29

0

AVMakeRectWithAspectRatioInsideRect 这个API是AVFoundation,它将返回裁剪区域给定作物尺寸的图像。

27

在斯威夫特3:

private func cropToPreviewLayer(originalImage: UIImage) -> UIImage { 
    let outputRect = previewLayer.metadataOutputRectOfInterest(for: previewLayer.bounds) 
    var cgImage = originalImage.cgImage! 
    let width = CGFloat(cgImage.width) 
    let height = CGFloat(cgImage.height) 
    let cropRect = CGRect(x: outputRect.origin.x * width, y: outputRect.origin.y * height, width: outputRect.size.width * width, height: outputRect.size.height * height) 

    cgImage = cgImage.cropping(to: cropRect)! 
    let croppedUIImage = UIImage(cgImage: cgImage, scale: 1.0, orientation: originalImage.imageOrientation) 

    return croppedUIImage 
} 
+3

诚实的善良。这将是前10名之一在这个网站上发布的答案我当然希望你留在网站上我发送了一个巨大的赏金**谢谢你,Patrick Montalto ** – Fattie 2017-05-03 15:37:53

+2

真的很棒的回答:)帮助了很多,从几个小时工作 – 2017-05-22 09:01:30

+2

什么是previewLayer,获取错误“未解决的标识符” – Yatendra 2017-06-27 07:43:37