0
votes

I am using Brad Larson's GPUImage Framework to add a UIImage element,i have successfully added the image but the main issue is that the image is getting stretched to the video's aspect ratio. Here is my code:

    GPUImageView *filterView = (GPUImageView *)self.view;
    videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];
    videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
    transformFilter=[[GPUImageTransformFilter alloc]init];
    CGAffineTransform t=CGAffineTransformMakeScale(0.5, 0.5);
    [(GPUImageTransformFilter *)filter setAffineTransform:t];
    [videoCamera addTarget:transformFilter];

    filter = [[GPUImageOverlayBlendFilter alloc] init];
    [videoCamera addTarget:filter];
    inputImage = [UIImage imageNamed:@"eye.png"];
    sourcePicture = [[GPUImagePicture alloc] initWithImage:inputImage smoothlyScaleOutput:YES];
    [sourcePicture forceProcessingAtSize:CGSizeMake(50, 50)];
    [sourcePicture processImage];
        [sourcePicture addTarget:filter];
    [sourcePicture addTarget:transformFilter];


    [filter addTarget:filterView];
    [videoCamera startCameraCapture];

I have tried to use transform filter before blending the image,but it isn't getting scaled. I want the image to appear at the center.How do i do it? Thanks

1

1 Answers

0
votes

You are on the right track, just have a few things out of place.

The following code will load an overlay image and apply a transformation to keep it at actual size. By default is will be centered over the video.

GPUImageView *filterView = (GPUImageView *)self.view;
videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];
videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;

filter = [[GPUImageOverlayBlendFilter alloc] init];
transformFilter = [[GPUImageTransformFilter alloc]init];

[videoCamera addTarget:filter];
[transformFilter addTarget:filter];

// setup overlay image
inputImage = [UIImage imageNamed:@"eye.png"];
sourcePicture = [[GPUImagePicture alloc] initWithImage:inputImage smoothlyScaleOutput:YES];

// determine the necessary scaling to keep image at actual size
CGFloat tx = inputImage.size.width / 480.0;     // 480/640: based on video camera preset
CGFloat ty = inputImage.size.height / 640.0;

// apply transform to filter
CGAffineTransform t = CGAffineTransformMakeScale(tx, ty);
[(GPUImageTransformFilter *)transformFilter setAffineTransform:t];

//
[sourcePicture addTarget:filter];
[sourcePicture addTarget:transformFilter];
[sourcePicture processImage];

[filter addTarget:filterView];
[videoCamera startCameraCapture];