捕获的图像水平翻转

时间:2014-08-30 12:14:14

标签: ios xcode camera uiimageview avcapturesession

您好我正在使用xcode中的avcapturesession制作实时相机屏幕,以便我可以拍照(类似于Snapchat设置)。相机功能齐全,我已经设置好了,所以我可以使用前置或后置摄像头。后置摄像头工作正常,我可以捕捉图像,它预览我的需要,但前置摄像头预览好,但是当捕获图像时,在预览中它被翻转,我无法看到这发生的位置

这是会议的代码:

- (void) initializeCamera {
AVCaptureSession *session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetHigh;

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

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

UIView *view = [self imagePreview];
CALayer *viewLayer = [view layer];
[viewLayer setMasksToBounds:YES];

CGRect bounds = [view bounds];
[captureVideoPreviewLayer setFrame:bounds];

NSArray *devices = [AVCaptureDevice devices];
AVCaptureDevice *frontCamera = nil;
AVCaptureDevice *backCamera = nil;

for (AVCaptureDevice *device in devices) {

    NSLog(@"Device name: %@", [device localizedName]);

    if ([device hasMediaType:AVMediaTypeVideo]) {

        if ([device position] == AVCaptureDevicePositionBack) {
            NSLog(@"Device position : back");
            backCamera = device;
        }
        else {
            NSLog(@"Device position : front");
            frontCamera = device;
        }
    }
}

if (!FrontCamera) {
    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:backCamera error:&error];
    if (!input) {
        NSLog(@"ERROR: trying to open camera: %@", error);
    }
    [session addInput:input];
}

if (FrontCamera) {
    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:frontCamera error:&error];
    if (!input) {
        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) capImage {
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) {

    if (imageSampleBuffer != NULL) {
        NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
        [self processImage:[UIImage imageWithData:imageData]];
    }
}];
}

- (void) processImage:(UIImage *)image {
haveImage = YES;

if([UIDevice currentDevice].userInterfaceIdiom==UIUserInterfaceIdiomPad) { //Device is ipad
    UIGraphicsBeginImageContext(CGSizeMake(3072, 4088));
    [image drawInRect: CGRectMake(0, 0, 3072, 4088)];
    UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    CGRect cropRect = CGRectMake(0, 0, 3072, 4088);
    CGImageRef imageRef = CGImageCreateWithImageInRect([smallImage CGImage], cropRect);

    [captureImage setImage:[UIImage imageWithCGImage:imageRef]];

    CGImageRelease(imageRef);

}else{ //Device is iphone
    UIGraphicsBeginImageContext(CGSizeMake(1280, 2272));
    [image drawInRect: CGRectMake(0, 0, 1280, 2272)];
    UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIImage * flippedImage = [UIImage imageWithCGImage:smallImage.CGImage scale:smallImage.scale orientation:UIImageOrientationLeftMirrored];

    smallImage = flippedImage;

    CGRect cropRect = CGRectMake(0, 0, 1280, 2272);
    CGImageRef imageRef = CGImageCreateWithImageInRect([smallImage CGImage], cropRect);


    [captureImage setImage:[UIImage imageWithCGImage:imageRef]];

    CGImageRelease(imageRef);
}
}

我也希望为焦点和闪光添加触摸功能,但我不知道在哪里实现代码就是我找到的:

闪光 -

对于闪光灯而言,我所能找到的关于火炬的是一个切换。我无法找到一种方法让它只是出现并像苹果相机应用程序闪存一样工作。

点按以关注 -

ios AVFoundation tap to focus

1 个答案:

答案 0 :(得分:3)

我认为这是前置摄像头的默认行为。尝试在输出图像显示之前手动翻转它。