【问题标题】:Capture overlayimage using AVFoundation framework使用 AVFoundation 框架捕获叠加图像
【发布时间】:2013-03-19 13:13:10
【问题描述】:

我想用叠加图像拍照。

代码:

- (void)addStillImageOutput
{

   // NSLog(@"You capture image");
    [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];
                                                         }];


}

叠加图像:

-(void)ButtonPressed1{

UIImageView *overlayImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sof.png"]];
    [overlayImageView setFrame:CGRectMake(30, 100, 260, 200)];


    [[self view] addSubview:overlayImageView];
    [overlayImageView release];
}

【问题讨论】:

    标签: iphone uiimageview avfoundation avcapturesession camera-overlay


    【解决方案1】:

    captureStillImageAsynchronouslyFromConnection 仅捕获来自连接 (AVCaptureConnection) 的数据,并且其他图像未连接。它们在视图中。

    因此,要“生成”包含所有元素(图片和叠加层)的图像,您必须执行以下操作:

    UIGraphicsBeginImageContext(stillImage.size);
    
    [stillImage drawInRect:CGRectMake(0, 0, picture.size.width, picture.size.height)];
    [overlayImageView drawInRect:CGRectMake(overlayImageView.frame.origin.x, overlayImageView.frame.origin.y, overlayImageView.frame.size.width, overlayImageView.frame.size.height)];
    
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

    此代码考虑图片大小等于屏幕大小。如果图片大小不同,则必须计算坐标以将叠加层放置在drawRect 方法中。抱歉英语不好。

    【讨论】:

    • 您可以在- (void)viewDidLoad 上添加观察者[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(processImage) name:kImageCapturedSuccessfully object:nil];,在- (void)processImage 上添加上面的代码。
    • 我需要用新移动的位置拍摄叠加图像。我设置了 drawInRect 框架。但是当我将图像从默认位置移动到其他位置时,我想用覆盖图像的新移动位置拍照......但我只有默认帧大小的覆盖图像。我怎样才能获得移动位置框架?
    • 当我添加您的代码时,它会捕获屏幕截图。但是背景是透明的。我需要查看 cameraview 控制器背景。
    • 如果您移动叠加层,您将更改其框架位置,您应该获得新位置。
    • 是的。每当出现新职位时,我都需要拍摄新职位的快照……这可行吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多