2014-08-30 126 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