0

I want to take picture with overlay image.

code:

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


}

OverlayImage:

-(void)ButtonPressed1{

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


    [[self view] addSubview:overlayImageView];
    [overlayImageView release];
}
Ram
  • 1,687
  • 3
  • 18
  • 28

1 Answers1

2

captureStillImageAsynchronouslyFromConnection captures only data from connection (AVCaptureConnection) and the additional images are not in connection. They are in the view.

So, to "generate" an image with all elements (picture and overlay), you must have to do something like this:

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();

This code is considering that picture size is equal to screen size. If picture size is different, you have to calculate the coordinates to place the overlay in the drawRect method. Sorry for bad english.

djserva
  • 395
  • 1
  • 11
  • You can add the observer `[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(processImage) name:kImageCapturedSuccessfully object:nil];` on `- (void)viewDidLoad` and on `- (void)processImage` you add the code above. – djserva Mar 20 '13 at 13:54
  • I need to take overlay image with newly moved position. I set drawInRect frame. But when i move image from default position to some other position, then i want to take picture with newly moved position with overlay image... But i got only default frame size overlay image. How can i get move position frame? – Ram Mar 25 '13 at 11:24
  • when i add your code it capture screenshot. But background is transparent. I need to see cameraview controller background. – Ram Mar 27 '13 at 11:50
  • If you move the overlay, you're changing its frame position and you should get the new position. – djserva Mar 28 '13 at 19:11
  • Yes. I need to take snapshot of new position whenever new position will occur....Is it posiible? – Ram Mar 29 '13 at 03:42
  • Yes. Like I said, get overlay frame position and use it in `drawInRect`. – djserva Apr 01 '13 at 14:01