简体   繁体   中英

want zooming functionality in iphone camera using AVFoundation framework

I want to zoom a camera by using UISlider .

I have done it successfully by adjusting the AffineTransform of AVCaptureVideoPreviewLayer .

Here is code of it

-(void)sliderAction:(UISlider*)sender{
    CGAffineTransform affineTransform = CGAffineTransformMakeTranslation(sender.value, sender.value);
    affineTransform = CGAffineTransformScale(affineTransform, sender.value, sender.value);
    affineTransform = CGAffineTransformRotate(affineTransform, 0);
    [CATransaction begin];
    [CATransaction setAnimationDuration:.025];
     //previewLayer is object of AVCaptureVideoPreviewLayer
    [[[self captureManager]previewLayer] setAffineTransform:affineTransform];
    [CATransaction commit];
}

but when I capture it, I am getting non- zoomed object of image.

It's a bit late to reply. But I am replying for future reference. Actually what you have done in you code is only that you have changed the zoom factor of the preview layer and not the underlying output connection. But for the zoom to be originally reflected on the captured output, you must put the factor in your output connection as well. You can use something similar to below:

-(void)sliderAction:(UISlider*)sender
{
    AVCaptureConnection* connection = [self.photoOutput connectionWithMediaType:AVMediaTypeVideo]; // photoOutput is a AVCaptureStillImageOutput object, representing a capture session output with customized preset

    CGAffineTransform affineTransform = CGAffineTransformMakeTranslation(sender.value, sender.value);
    affineTransform = CGAffineTransformScale(affineTransform, sender.value, sender.value);
    affineTransform = CGAffineTransformRotate(affineTransform, 0);
    [CATransaction begin];
    [CATransaction setAnimationDuration:.025];
     //previewLayer is object of AVCaptureVideoPreviewLayer
    [[[self captureManager]previewLayer] setAffineTransform:affineTransform];
    if (connection) {
        connection.videoScaleAndCropFactor = sender.value;
    }
    [CATransaction commit];
}

And it should do the trick.

Ideally, you shouldn't perform the connection.videoScaleAndCropFactor change in your Slider routine and should place the code in your original capture routine and set it only once with the momentary value of the slider, just before calling captureStillImageAsynchronouslyFromConnection method.

Hope it helps :)

First your code zooming only layer contents not CMSampleBuffer . Your next work around is to make a scale on CVPixelBuffer from CMSampleBuffer and save scaled CMSampleBuffer to AVWriter . You can use Accelerate.framework to scale CVPixelBuffer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM