【问题标题】:image cropping an AVCaptureSession Image图像裁剪 AVCaptureSession 图像
【发布时间】:2023-03-22 20:25:01
【问题描述】:

所以我整天都没有运气,不用说非常令人沮丧,我查找了许多示例和可下载的类别,它们都吹捧能够完美地裁剪图像。他们这样做了,但是当我尝试从通过 AVCaptureSession 生成的图像中执行此操作时,它就无法正常工作。我咨询了这两个来源

http://codefuel.wordpress.com/2011/04/22/image-cropping-from-a-uiscrollview/

http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

第一个链接中的项目似乎可以像宣传的那样直接工作,但是一旦我破解它以在 av 捕获图像上做同样的魔法......不......

有人对此有所了解吗?这里也是我的代码供参考。

- (IBAction)TakePhotoPressed:(id)sender 
{
     AVCaptureConnection *videoConnection = nil;
     for (AVCaptureConnection *connection in 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: %@", stillImageOutput);
     [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
     {
         CFDictionaryRef exifAttachments = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
         if (exifAttachments)
         {
             // Do something with the attachments.
             //NSLog(@"attachements: %@", exifAttachments);
         }
         else

         NSLog(@"no attachments");

         NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
         UIImage *image = [[UIImage alloc] initWithData:imageData];

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


         float scale = 1.0f/_scrollView.zoomScale;

         CGRect visibleRect;
         visibleRect.origin.x = _scrollView.contentOffset.x * scale;
         visibleRect.origin.y = _scrollView.contentOffset.x * scale;
         visibleRect.size.width = _scrollView.bounds.size.width * scale;
         visibleRect.size.height = _scrollView.bounds.size.height * scale;

         UIImage* cropped = [self cropImage:image withRect:visibleRect];

         [croppedImage setImage:cropped];         

         [image release];
     }
      ];

    [croppedImage setHidden:NO];


}

上面用到的cropImage函数。

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


    CGRect transformedRect=rect;
    if(originalImage.imageOrientation==UIImageOrientationRight) 
    {
        transformedRect.origin.x = rect.origin.y;
        transformedRect.origin.y = originalImage.size.width-(rect.origin.x+rect.size.width);
        transformedRect.size.width = rect.size.height;
        transformedRect.size.height = rect.size.width;
    }

    CGImageRef cr = CGImageCreateWithImageInRect(originalImage.CGImage, transformedRect);
    UIImage* cropped = [UIImage imageWithCGImage:cr scale:originalImage.scale orientation:originalImage.imageOrientation];
    [croppedImage setFrame:CGRectMake(croppedImage.frame.origin.x, 
                                      croppedImage.frame.origin.y, 
                                      cropped.size.width, 
                                      cropped.size.height)];

    CGImageRelease(cr);
    return cropped;
}

我也很想冗长,并用尽可能多的信息来武装任何可能帮助我解决困境的人,以发布我的 scrollView 和 avcapture 会话的初始化。不过这可能有点过分了,所以如果你想看就问吧。

现在代码的实际作用是什么?..

拍照前的样子

然后……

编辑:

嗯,我现在有一些意见,没有评论,所以要么没有人想通,要么很简单,他们认为我会再次想通......无论如何,我没有取得任何进展。因此,对于任何对此感兴趣的人来说,这里有一个小示例应用程序,所有代码都已设置好,您可以看到我在做什么

https://docs.google.com/open?id=0Bxr4V3a9QFM_NnoxMkhzZTVNVEE

【问题讨论】:

    标签: uiimage crop avcapturesession


    【解决方案1】:

    似乎这个小难题不仅让我在将近一周后难住了,而且几乎没有看过我问题的人也没有任何建议。我必须说,对于这个特殊的问题,我无法让它以这种方式工作,我思考、修补和沉思了一段时间,但无济于事。直到我这样做了

    [self HideElements];
    
    UIGraphicsBeginImageContext(chosenPhotoView.frame.size);
    [chosenPhotoView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    [self ShowElements];
    

    就是这样,更少的代码,它几乎可以立即运行。因此,我当时没有尝试通过滚动视图裁剪图像,而是截取屏幕截图,然后使用滚动视图框架变量裁剪图像。并且隐藏/显示元素功能隐藏了我想要的图片上的任何重叠元素。

    【讨论】:

    • 我面临同样的问题。你能告诉我我需要在哪里调用这个代码sn-p吗?我会很感激的。
    • 这方面有什么更新吗?我有完全一样的问题。似乎截屏的质量不如您从 AV 捕获会话中获得的照片。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2017-05-22
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 1970-01-01
    • 2015-02-02
    相关资源
    最近更新 更多