裁剪AVCaptureSession捕获的图像 [英] Cropping image captured by AVCaptureSession

查看:152
本文介绍了裁剪AVCaptureSession捕获的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个iPhone应用程序,它使用AVFoundation拍摄照片并裁剪它。
该应用程序类似于QR代码阅读器:它使用带覆盖的AVCaptureVideoPreviewLayer。
叠加层有一个正方形。我想剪裁图像,以便裁剪的图像正好是用户在广场内的位置。

I'm writing an iPhone App which uses AVFoundation to take a photo and crop it. The App is similar to a QR code reader: It uses a AVCaptureVideoPreviewLayer with an overlay. The overlay has a square. I want to crop the image so the cropped image is exactly what the user has places inside the square.

预览图层有重力AVLayerVideoGravityResizeAspectFill。

The preview layer has gravity AVLayerVideoGravityResizeAspectFill.

看起来相机实际拍摄的内容并不完全是用户在预览图层中看到的内容。这意味着我需要从预览坐标系移动到捕获的图像坐标系,以便我可以裁剪图像。为此,我认为我需要以下参数:
1.视图大小和捕获图像大小之间的比例。
2.告知捕获图像的哪一部分与预览图层中显示的内容相匹配的信息。

It looks like what the camera actually captures is not exactly what the user sees in the preview layer. This means that I need to move from the preview coordinate system to the captured image coordinate system so I can crop the image. For this I think that I need the following parameters: 1. ration between view size and captured image size. 2. information which tells which part of the captured image matches what is displayed in the preview layer.

是否有人知道如何获取此信息,或者如果有不同的方法来裁剪图像。

Does anybody know how I can obtain this info, or if there is a different approach to crop the image.

(ps捕获预览的屏幕截图不是一个选项,因为据我所知它可能会导致应用被拒绝)。

(p.s. capturing a screenshot of the preview is not an option, as I understand it might resulting in the App being rejected).

提前谢谢

推荐答案

希望这符合你的要求要求

Hope this meets your requirements

- (UIImage *)cropImage:(UIImage *)image to:(CGRect)cropRect andScaleTo:(CGSize)size {
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGImageRef subImage = CGImageCreateWithImageInRect([image CGImage], cropRect);
    NSLog(@"---------");     
    NSLog(@"*cropRect.origin.y=%f",cropRect.origin.y);
    NSLog(@"*cropRect.origin.x=%f",cropRect.origin.x);

    NSLog(@"*cropRect.size.width=%f",cropRect.size.width);     
    NSLog(@"*cropRect.size.height=%f",cropRect.size.height);     

    NSLog(@"---------");     

    NSLog(@"*size.width=%f",size.width);     
    NSLog(@"*size.height=%f",size.height);     

    CGRect myRect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    CGContextScaleCTM(context, 1.0f, -1.0f);
    CGContextTranslateCTM(context, 0.0f, -size.height);
    CGContextDrawImage(context, myRect, subImage);
    UIImage* croppedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CGImageRelease(subImage);     

    return croppedImage;
}

这篇关于裁剪AVCaptureSession捕获的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆