ios - 拍摄的图像水平翻转

标签 ios xcode camera uiimageview avcapturesession

你好,我在 xcode 中使用 avcapturesession 来制作实时相机屏幕,这样我就可以拍照(类似于 snapchat 设置)。摄像头功能齐全,我已经设置好了,所以我可以使用前置或后置摄像头。后置摄像头工作正常,我可以拍摄图像并预览我需要的方式,但是前置摄像头预览正常,但是当拍摄图像时,在预览中它被翻转了,我看不到发生这种情况的位置。

这是我的 session 代码:

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

最佳答案

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

关于ios - 拍摄的图像水平翻转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25582241/

相关文章:

ios - 如何在 iOS 应用程序中评论 instagram 提要

ios - NSMutableAttributedString paragraphStyle 不适用

ios - Testflight 的导出方法是什么?

java - 媒体扫描仪正在扫描,但不刷新 (Android)

android - 尝试从空对象引用的字段 'int android.media.CamcorderProfile.fileFormat' 读取

ios - 隐藏音量 HUD(避免使用加载 View )

ios - UIView overrideUserInterfaceStyle 不起作用

ios - 在 iOS 上从不同的应用程序读取文档

ios - 如何预览在 .xib 文件中以编程方式创建的项目?

reactjs - Expo 相机仅通过 React Navigation 打开一次