如何打开/关闭闪光灯,captureSession的使用流程是什么?

如何打开/关闭闪光灯,captureSession的使用流程是什么

阅读 515
avatarAI BotBETA

如何打开/关闭闪光灯:

在iOS中,闪光灯的开关是通过AVFoundation框架中的AVCaptureDevice类控制的。你可以通过以下步骤打开或关闭闪光灯:

  1. 首先,你需要获取设备的默认相机。这可以通过调用AVCaptureDevice.defaultDevice(withDeviceType:AVMediaTypeVideo)方法实现。
  2. 然后,你需要检查闪光灯是否可用。这可以通过调用device.hasTorchdevice.torchMode属性实现。
  3. 如果闪光灯可用,你可以通过调用device.lockForConfiguration()方法来锁定设备,然后设置device.torchMode = .on来打开闪光灯,或者设置device.torchMode = .off来关闭闪光灯。
  4. 最后,你需要通过调用device.unlockForConfiguration()方法来解锁设备。

以下是一个简单的示例代码:

let device = AVCaptureDevice.defaultDevice(withDeviceType: AVMediaTypeVideo)

if device.hasTorch {
    do {
        try device.lockForConfiguration()
        device.torchMode = .on
    } catch {
        print("Error locking device for configuration")
    } finally {
        device.unlockForConfiguration()
    }
} else {
    print("Torch not available")
}

captureSession的使用流程:

AVCaptureSession是AVFoundation框架中用于管理视频和音频捕获的类。使用captureSession的基本流程如下:

  1. 首先,你需要创建一个AVCaptureSession对象。这可以通过调用AVCaptureSession.init()方法实现。
  2. 然后,你需要配置captureSession的输入设备。这可以通过调用captureSession.addInput(input)方法实现,其中input是你想要使用的AVCaptureInput对象(例如,AVCaptureDeviceInput)。
  3. 接下来,你需要配置captureSession的输出。这可以通过调用captureSession.addOutput(output)方法实现,其中output是你想要使用的AVCaptureOutput对象(例如,AVCaptureVideoPreviewOutput或AVAssetWriterOutput)。
  4. 最后,你需要开始捕获会话。这可以通过调用captureSession.startRunning()方法实现。
  5. 在捕获过程中,你可以通过调用captureSession.stopRunning()方法来停止捕获会话。
1 个回答

步骤 1
在module.json5添加权限

ohos.permission.MICROPHONE 
ohos.permission.CAMERA 
ohos.permission.MEDIA_LOCATION 
ohos.permission.WRITE_MEDIA

步骤 2
动态授权:

aboutToAppear() { 
  let context = getContext() as common.UIAbilityContext; 
  abilityAccessCtrl.createAtManager().requestPermissionsFromUser(context, [ 
    'ohos.permission.MICROPHONE', 
    'ohos.permission.CAMERA', 
    'ohos.permission.MEDIA_LOCATION', 
    'ohos.permission.WRITE_MEDIA']).then(()=>{ 
  }); 
  console.info(`surfaceId=${this.surfaceId}`); 
}

步骤 3
builder函数:

build() { 
  Column() { 
    // 创建XComponent 
    XComponent({ 
      id: '', 
      type: 'surface', 
      libraryname: '', 
      controller: this.mXComponentController 
    }) 
    .onLoad(() => { 
      // 设置Surface宽高(1920*1080),预览尺寸设置参考前面 previewProfilesArray 获取的当前设备所支持的预览分辨率大小去设置 
      this.mXComponentController.setXComponentSurfaceSize({surfaceWidth:1920,surfaceHeight:1080}); 
      // 获取Surface ID 
      this.surfaceId = this.mXComponentController.getXComponentSurfaceId(); 
      this.createDualChannelPreview(this.surfaceId); 
    }) 
    .width('1920px') 
    .height('1080px') 
    Button('打开闪光灯').width(50).height(50).onClick(()=>{ 
      this.setFlash(camera.FlashMode.FLASH_MODE_ALWAYS_OPEN) 
    }) 
    Button('关闭闪光灯').width(50).height(50).onClick(()=>{ 
      this.setFlash(camera.FlashMode.FLASH_MODE_CLOSE) 
    }) 
  } 
}

步骤 4
创建相机会话:

async createDualChannelPreview(XComponentSurfaceId: string): Promise<void> { 
  let cameraManager = camera.getCameraManager(this.context); 
  let camerasDevices: Array<camera.CameraDevice> = cameraManager.getSupportedCameras(); 
  let profiles: camera.CameraOutputCapability = cameraManager.getSupportedOutputCapability(camerasDevices[0]); // 获取对应相机设备profiles 
  let previewProfiles: Array<camera.Profile> = profiles.previewProfiles; 
  let previewProfilesObj: camera.Profile = previewProfiles[0]; 
  let previewOutput: camera.PreviewOutput = cameraManager.createPreviewOutput(previewProfilesObj, XComponentSurfaceId); 
  let cameraInput: camera.CameraInput = cameraManager.createCameraInput(camerasDevices[0]); 
  await cameraInput.open(); 
  this.captureSession = cameraManager.createCaptureSession(); 
  this.captureSession?.beginConfig(); 
  this.captureSession?.addInput(cameraInput); 
  this.captureSession?.addOutput(previewOutput); 
  await this.captureSession?.commitConfig(); 
  await this.captureSession?.start(); 
}

步骤 5
开启闪光灯:

setFlash(flashMode: camera.FlashMode) { 
  if (this.captureSession != null) { 
    let focusModeStatus: boolean = this.captureSession?.isFlashModeSupported(flashMode); 
    if (focusModeStatus) { 
      this.captureSession?.setFlashMode(flashMode); 
    } 
  } 
}

----结束

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进