捕获的图像水平翻转

3
你好,我正在使用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 点击对焦

事实是,这是前置摄像头的默认行为。用前置摄像头拍照并观察手机如何显示它。安卓手机也会发生这种情况。前置摄像头模仿镜子,以便您可以自然地使用它,因此输出图像被翻转了。 - Reuben L.
1个回答

3

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


好的,但是当我在处理图像中更改翻转的属性时: UIImage *flippedImage = [UIImage imageWithCGImage:smallImage.CGImage scale:smallImage.scale orientation:UIImageOrientationLeftMirrored];UIImageOrientationLeftMirrored 对结果没有任何影响,我只想翻转前置摄像头。 - Tavis
很抱歉,我无法从这里为您提供帮助。不过您可以尝试在 Github 上查找其中一个相机存储库,并深入研究它们的代码,以了解它们是如何做到的。有多个存储库已经解决了这个确切的问题。 - Abhijay Bhatnagar
我找不到,也许你可以提供一个链接。 - Tavis
编译完成,确认前置模式不会翻转图像。 - Abhijay Bhatnagar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接