简体   繁体   English

如何使用AVCapture会话/ AVCapture正确启动相机会话

[英]how to correctly start a camera session using AVCapture session/AVCapture

I want to make an iOS app in objective C. Right now I'm stuck on making the preview layer to the AVCapture preview output. 我想在目标C中制作一个iOS应用。现在,我只能将预览层制作为AVCapture预览输出。 Could someone please tell me how to successfully start an image capture session using the AVCapture camera session in iOS Objective C? 有人可以告诉我如何在iOS Objective C中使用AVCapture相机会话成功启动图像捕获会话吗? Any help is much appreciated. 任何帮助深表感谢。 Thank you. 谢谢。

I give you answer for AVCaptureSession 我给你AVCaptureSession的答案

-(void)capture
{ 
   NSError *error=nil;
   //Capture Session
   AVCaptureSession *session = [[AVCaptureSession alloc]init];
   session.sessionPreset = AVCaptureSessionPresetPhoto;

   //Add device
   AVCaptureDevice *inputDevice = nil;
   NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
   for(AVCaptureDevice *camera in devices)
   {
      if([camera position] == AVCaptureDevicePositionBack)  // is Back camera 
      { 
         inputDevice = camera;
         break;
      }
   }
   [session addInput:inputDevice];

    //Output
    AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
    [session addOutput:output];
    output.videoSettings = @{ (NSString *)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA) };

    //Preview Layer
    AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    previewLayer.frame = viewForCamera.bounds;
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [viewForCamera.layer addSublayer:previewLayer];

    //Start capture session
    [session startRunning];
}

Try this code to get camera id. 尝试使用此代码获取相机ID。

NSString *cameraID = nil;

NSArray *captureDeviceType = @[AVCaptureDeviceTypeBuiltInWideAngleCamera];
AVCaptureDeviceDiscoverySession *captureDevice = 
              [AVCaptureDeviceDiscoverySession 
                discoverySessionWithDeviceTypes:captureDeviceType 
                mediaType:AVMediaTypeVideo 
                position:AVCaptureDevicePositionUnspecified];

cameraID = [captureDevice.devices.lastObject localizedName];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM