5

我遵循了一个教程,该教程指导了一种制作自定义但简单的相机应用程序的方法,几乎​​完全符合我想要的使用需求。我实际上有两个问题需要改变,但我现在将专注于第一个。

以下代码允许使用后置摄像头,但我基本上需要更改它以便我可以使用前置摄像头。我还将在此处链接我从中获取视频的视频以表扬他们,我按照其中一位评论者所说的关于使用前置摄像头的说法,但答案根本没有帮助。

https://www.youtube.com/watch?v=Xv1FfqVy-KM

我根本不擅长编码,但正在努力学习。任何帮助,将不胜感激!非常感谢。

@interface ViewController ()

@end

@implementation ViewController

AVCaptureSession *session;
AVCaptureStillImageOutput *StillImageOutput;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewWillAppear:(BOOL)animated {

    session = [[AVCaptureSession alloc] init];

    [session setSessionPreset:AVCaptureSessionPresetPhoto];

    AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    NSError *error;

    AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&error];



    if ([session canAddInput:deviceInput]) {

        [session addInput:deviceInput];

    }



    AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

    [previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

    CALayer *rootLayer = [[self view] layer];

    [rootLayer setMasksToBounds:YES];

    CGRect frame = frameforcapture.frame;



    [previewLayer setFrame:frame];



    [rootLayer insertSublayer:previewLayer atIndex:0];





    StillImageOutput = [[AVCaptureStillImageOutput alloc] init];

    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey, nil];

    [StillImageOutput setOutputSettings:outputSettings];



    [session addOutput:StillImageOutput];



    [session startRunning];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)takephoto:(id)sender {

    AVCaptureConnection *videoConnection = nil;

    for (AVCaptureConnection *connection in StillImageOutput.connections) {
        for (AVCaptureInputPort *port in [connection inputPorts ]) {
            if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
                videoConnection = connection;
                break;
            }
        }
    }
    [StillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
        if (imageDataSampleBuffer != NULL) {
            NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
            UIImage *image = [UIImage imageWithData:imageData];
            imageView.image = image;
        }
    }];
}

@end
4

2 回答 2

11

此代码返回给定媒体类型的默认设备的 AVCaptureDevice 实例。

AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

将此代码更改为

....
AVCaptureDevice *inputDevice = nil;
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for(AVCaptureDevice *camera in devices) {
    if([camera position] == AVCaptureDevicePositionFront) { // is front camera  
        inputDevice = camera;
        break;
    }
}
......
于 2015-06-15T03:26:30.660 回答
0
- (IBAction)btnCameraClicked:(id)sender {

    AVCaptureDevicePosition desiredPosition;
    if (isUsingFrontFacingCamera)
        desiredPosition = AVCaptureDevicePositionBack;
    else
        desiredPosition = AVCaptureDevicePositionFront;

    for (AVCaptureDevice *d in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
        if ([d position] == desiredPosition) {
            [[previewLayer session] beginConfiguration];
            AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:d error:nil];
            for (AVCaptureInput *oldInput in [[previewLayer session] inputs]) {
                [[previewLayer session] removeInput:oldInput];
            }
            [[previewLayer session] addInput:input];
            [[previewLayer session] commitConfiguration];
            break;
        }
    }
    isUsingFrontFacingCamera = !isUsingFrontFacingCamera;
}

//在.h文件中声明

AVCaptureVideoPreviewLayer *previewLayer;
BOOL isUsingFrontFacingCamera;
于 2015-06-15T04:54:34.850 回答