1

我正在寻找有关 AVFoundation 的帮助。目前我有一个关于如何从这个主题捕获静止图像的分步指南:如何将使用 AVFoundation 拍摄的照片保存到相册?

我的问题是,如何降低保存图像的质量以及使用 AVCaptureSessionPreset640x480。我想将图像质量降低一半,或者如果有另一种方法可以使保存的图像尽可能小——可能是 320x280——那么这可能比调整实际质量更好。

我不知道以前是否有人问过这个问题,但过去几天我一直在网上搜索,但找不到答案。下面是我的代码。

`

#import "ViewController.h"
#import <ImageIO/ImageIO.h>

@interface ViewController ()

@end

@implementation ViewController

@synthesize imagePreview;
@synthesize iImage;
@synthesize stillImageOutput;

-(IBAction)captureNow {
    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];
         //NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(image, 0.5f)]];
         UIImage *image = [[UIImage alloc] initWithData:imageData];

         self.iImage.image = image;

         UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
     }];
}

-(void)viewDidAppear:(BOOL)animated
{
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    session.sessionPreset = AVCaptureSessionPreset640x480;

    CALayer *viewLayer = self.imagePreview.layer;
    NSLog(@"viewLayer = %@", viewLayer);

    AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

    captureVideoPreviewLayer.frame = self.imagePreview.bounds;
    [self.imagePreview.layer addSublayer:captureVideoPreviewLayer];

    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    if (!input) {
        // Handle the error appropriately.
        NSLog(@"ERROR: trying to open camera: %@", error);
    }
    [session addInput:input];

    stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
    [stillImageOutput setOutputSettings:outputSettings];
    [session addOutput:stillImageOutput];

    [session startRunning];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

`

4

2 回答 2

3

尝试使用不同的预设,这将为您提供不同分辨率的图像。

根据 Apple 文档,对于 iPhone4(背面),您将获得该会话的以下预设的以下分辨率图像。

AVCaptureSessionPresetHigh : 1280x720

AVCaptureSessionPresetMedium : 480x360

AVCaptureSessionPresetLow : 192x144

AVCaptureSessionPreset640x480 : 640x480

AVCaptureSessionPreset1280x720 : 1280x720

AVCaptureSessionPresetPhoto : 2592x1936.This is not supported for video output

希望这会有所帮助。

于 2013-09-25T10:58:28.060 回答
2

您需要在对象上设置kCVPixelBufferWidthKeykCVPixelBufferHeightKey选项AVCaptureStillImageOutput来设置您选择的分辨率。此宽度/高度将覆盖会话预设宽度/高度。最小示例如下(添加错误检查)。

    _session = [[AVCaptureSession alloc] init];
    _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    NSError * error;
    _sessionInput = [AVCaptureDeviceInput deviceInputWithDevice:_device error:&error];
    _stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                              [NSNumber numberWithDouble:320.0], (id)kCVPixelBufferWidthKey,
                              [NSNumber numberWithDouble:280.0], (id)kCVPixelBufferHeightKey,
                              [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA], (id)kCVPixelBufferPixelFormatTypeKey,
                              nil];

    [_stillImageOutput setOutputSettings:options];

    [_session beginConfiguration ];
    [_session addInput:_sessionInput];
    [_session addOutput:_stillImageOutput];
    [_session setSessionPreset:AVCaptureSessionPresetPhoto];
     _avConnection = [_stillImageOutput connectionWithMediaType:AVMediaTypeVideo];
    [ _session commitConfiguration ];

.............

- (void) start
{
    [self.session startRunning];
}

.............

[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:self.avConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
 {
     CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(imageSampleBuffer);
     CVPixelBufferLockBaseAddress(imageBuffer, 0);
     size_t width = CVPixelBufferGetWidth(imageBuffer);
     size_t height = CVPixelBufferGetHeight(imageBuffer);
     NSLog(@"%d : %d", height, width);

 }];

注意:我只在 Mac 上试过这个。理想情况下,它也应该适用于 iOS。也尝试保持一些纵横比。

于 2014-05-23T04:58:03.703 回答