2014-08-30 124 views
2

你好,我在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); 
} 
} 

我还想添加触摸对焦和闪光灯,但我不知道我要实现这里的代码是什么,我发现:

flash-

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

触摸对焦 -

ios AVFoundation tap to focus

+0

事实是,这是前置摄像头的默认行为。用前置摄像头拍照,观察手机的显示效果。也发生在Android手机上。前置摄像头模仿镜面以便您可以自然使用,因此输出图像会翻转。 – 2014-08-30 12:39:55

回答

2

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

+0

好吧,但当我更改过程映像中的翻转的东西:UIImage * flippedImage = [UIImage imageWithCGImage:smallImage.CGImage scale:smallImage.scale orientation:UIImageOrientationLeftMirrored]; UIImageOrientationLeftMirrored对结果不做任何处理,我只想让它翻转前置摄像头。 – Tavis 2014-08-30 13:02:42

+0

对不起,我从这里帮不了你。你可以尝试在github上查找许多摄像机存储库中的一个,然后潜入他们的代码中,看看他们是如何做到的。有几个解决了这个确切的问题。 – 2014-08-30 13:17:26

+0

以及我无法找到一个也许你可以提供一个链接 – Tavis 2014-08-30 14:42:32