相机人脸检测(FACE_DETECTION)

相机人脸检测(FACE_DETECTION)

HarmonyOS
2024-05-20 21:43:47
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
wingcky

功能场景描述及使用场景

相机人脸检测(FACE_DETECTION)

元数据(Metadata)是对相机返回的图像信息数据的描述和上下文,针对图像信息,提供的更详细的数据,如照片或视频中,识别人像的取景框坐标等信息。

Metadata主要是通过一个TAG(Key),去找对应的Data,用于传递参数和配置信息,减少内存拷贝操作。

使用的核心API

import camera from '@ohos.multimedia.camera';import { BusinessError } from '@ohos.base';

核心代码解释

this.mXComponentController.setXComponentSurfaceSize({surfaceWidth:1920,surfaceHeight:1080}); 
// 获取Surface ID 
this.surfaceId = this.mXComponentController.getXComponentSurfaceId(); 
if (this.cameraManager == null) { 
  this.res = "Get Camera failed" 
  return; 
} 
 
let cameraDevice:camera.CameraDevice|null = this.getFrontCameraDevices(this.cameraManager); 
this.getSupportedOutputCapability(cameraDevice,this.cameraManager).then((outputCapability:camera.CameraOutputCapability|undefined) => { 
  if(this.previewOt===undefined){ 
    this.previewOt = this.getPreviewOutput(this.cameraManager,outputCapability,this.surfaceId); 
  } 
 
  if(this.previewOt===undefined) { 
    return; 
  } 
 
  this.previewOt.on('frameStart', () => { 
    console.info('Preview frame started'); 
  }); 
  this.previewOt.on('frameEnd', () => { 
    console.info('Preview frame ended'); 
  }); 
  this.previewOt.on('error', (previewOutputError: BusinessError) => { 
    console.info(`Preview output error code: ${previewOutputError.code}`); 
  }); 
  this.getCaptureSession(this.cameraManager).then(()=>{ 
    this.startPreviewOutput(this.previewOt); 
  }).catch((error:BusinessError) =>{ 
    console.error("Failed to get getSupportedOutputCapability. error code: " + error.code); 
  }); 
 
}).catch((error:BusinessError) =>{ 
  console.error("Failed to get getSupportedOutputCapability. error code: " + error.code); 
}); 
 
}) 
.width('1080px') 
  .height('1920px') 
  .margin({left:40}) 
 
} 
} 
 
getPreviewOutput(cameraManager:camera.CameraManager|null, cameraOutputCapability: camera.CameraOutputCapability|undefined, surfaceId: string): camera.PreviewOutput | undefined { 
  let previewOutput: camera.PreviewOutput | undefined = undefined; 
 
  if((cameraManager == null) 
    ||(cameraOutputCapability === undefined)){ 
    return undefined; 
  } 
 
  let previewProfilesArray: Array<camera.Profile> = cameraOutputCapability.previewProfiles; 
 
  try { 
    previewOutput = cameraManager.createPreviewOutput(previewProfilesArray[0], surfaceId); 
  } catch (error) { 
    let err = error as BusinessError; 
    console.error("Failed to create the PreviewOutput instance. error code: " + err.code); 
  } 
  return previewOutput; 
} 
 
getCameraManager(context:Context):camera.CameraManager{ 
  let cameraManager:camera.CameraManager = camera.getCameraManager(context); 
  return cameraManager; 
} 
startPreviewOutput(previewOutput: camera.PreviewOutput|undefined): void { 
  if(previewOutput === undefined) { 
  return; 
} 
previewOutput.start().then(() => { 
  console.info('Callback returned with previewOutput started.'); 
}).catch((err: BusinessError) => { 
  console.info('Failed to previewOutput start '+ err.code); 
}); 
} 
 
getFrontCameraDevices(cameraManager: camera.CameraManager): camera.CameraDevice|null { 
  let cameraArray: Array<camera.CameraDevice> = cameraManager.getSupportedCameras(); 
  if (cameraArray != undefined && cameraArray.length <= 0) { 
    console.error("cameraManager.getSupportedCameras error"); 
    return null; 
  } 
  for (let index = 0; index < cameraArray.length; index++) { 
    console.info('cameraId : ' + cameraArray[index].cameraId);  // 获取相机ID 
    console.info('cameraPosition : ' + cameraArray[index].cameraPosition);  // 获取相机位置 
    console.info('cameraType : ' + cameraArray[index].cameraType);  // 获取相机类型 
    console.info('connectionType : ' + cameraArray[index].connectionType);  // 获取相机连接类型 
    if((cameraArray[index].cameraPosition == camera.CameraPosition.CAMERA_POSITION_FRONT)&& 
      (cameraArray[index].connectionType == camera.ConnectionType.CAMERA_CONNECTION_BUILT_IN)){ 
      return cameraArray[index]; 
    } 
  } 
 
  return null; 
 
} 
 
async getSupportedOutputCapability(cameraDevice: camera.CameraDevice|null, cameraManager: camera.CameraManager): Promise<camera.CameraOutputCapability | undefined> { 
  if(cameraDevice == null){ 
  return; 
} 
// 创建相机输入流 
try { 
  if(this.cameraInput === undefined){ 
    this.cameraInput = cameraManager.createCameraInput(cameraDevice); 
  } 
} catch (error) { 
  let err = error as BusinessError; 
  console.error('Failed to createCameraInput errorCode = ' + err.code); 
} 
if (this.cameraInput === undefined) { 
  return undefined; 
} 
// 监听cameraInput错误信息 
this.cameraInput.on('error', cameraDevice, (error: BusinessError) => { 
  console.info(`Camera input error code: ${error.code}`); 
}); 
// 打开相机 
await this.cameraInput.open(); 
// 获取相机设备支持的输出流能力 
this.cameraOutputCapability  = cameraManager.getSupportedOutputCapability(cameraDevice); 
if (!this.cameraOutputCapability) { 
  console.error("cameraManager.getSupportedOutputCapability error"); 
  return undefined; 
} 
console.info("outputCapability: " + JSON.stringify(this.cameraOutputCapability)); 
return this.cameraOutputCapability; 
} 
 
 
async getCaptureSession(cameraManager: camera.CameraManager|null): Promise<void> { 
  this.startMetadataOutput(cameraManager) 
  if((this.captureSession != undefined)||(cameraManager == null)){ 
  return; 
} 
try { 
  this.captureSession = cameraManager.createCaptureSession(); 
} catch (error) { 
  let err = error as BusinessError; 
  console.error(`Failed to create the CaptureSession instance. error: ${JSON.stringify(err)}`); 
} 
 
if(this.captureSession === undefined){ 
  return; 
} 
 
try { 
  this.captureSession.beginConfig(); 
} catch (error) { 
  let err = error as BusinessError; 
  console.error(`Failed to beginConfig. error: ${JSON.stringify(err)}`); 
} 
 
try { 
  if(this.cameraInput != undefined){ 
    this.captureSession.addInput(this.cameraInput); 
  } 
} catch (error) { 
  let err = error as BusinessError; 
  console.error(`Failed to addInput. error: ${JSON.stringify(err)}`); 
} 
 
try { 
  this.captureSession.addOutput(this.previewOt); 
} catch (error) { 
  let err = error as BusinessError; 
  console.error(`Failed to add previewOutput. error: ${JSON.stringify(err)}`); 
} 
 
try { 
  this.captureSession.addOutput(this.metadataOutput); 
} catch (error) { 
  let err = error as BusinessError; 
  console.error(`Failed to add metadataOutput. error: ${JSON.stringify(err)}`); 
} 
 
try { 
  await this.captureSession.commitConfig(); 
} catch (error) { 
  let err = error as BusinessError; 
  console.error(`Failed to commitConfig. error: ${JSON.stringify(err)}`); 
} 
 
try { 
  await this.captureSession.start() 
} catch (error) { 
  let err = error as BusinessError; 
  console.error(`Failed to start. error: ${JSON.stringify(err)}`); 
} 
} 
 
startMetadataOutput(cameraManager: camera.CameraManager|null): void { 
  if (this.cameraOutputCapability === undefined){ 
  return; 
} 
if(cameraManager == null) { 
  return; 
} 
let metadataObjectTypes: Array<camera.MetadataObjectType> = this.cameraOutputCapability.supportedMetadataObjectTypes; 
try { 
  this.metadataOutput = cameraManager.createMetadataOutput(metadataObjectTypes); 
} catch (error) { 
  let err = error as BusinessError; 
  console.info('Failed to createMetadataOutput, error code: '+ err.code); 
} 
if(this.metadataOutput === undefined){ 
  return; 
} 
this.metadataOutput.start().then(() => { 
  console.info('Callback returned with metadataOutput started.'); 
}).catch((err: BusinessError) => { 
  console.info('Failed to metadataOutput start, error code: '+ err.code); 
}); 
this.metadataOutput.on('metadataObjectsAvailable', (err: BusinessError, metadataObjectArr: Array<camera.MetadataObject>) => { 
  console.info(`metadata output metadataObjectsAvailable`); 
  this.res="Face Detected"; 
  // this.stopMetadataOutput(); 
  console.info("metadataObjectArr topLeftX",metadataObjectArr[0].boundingBox.topLeftX) 
  console.info("metadataObjectArr topLeftY",metadataObjectArr[0].boundingBox.topLeftY) 
  console.info("metadataObjectArr width",metadataObjectArr[0].boundingBox.width) 
  console.info("metadataObjectArr height",metadataObjectArr[0].boundingBox.height) 
}); 
this.metadataOutput.on('error', (metadataOutputError: BusinessError) => { 
  console.info(`Metadata output error code: ${metadataOutputError.code}`); 
}); 
 
} 
 
stopMetadataOutput(): void { 
  if(this.metadataOutput === undefined){ 
  return; 
} 
this.metadataOutput.stop().then(() => { 
  console.info('Callback returned with metadataOutput stopped.'); 
 
}).catch((err: BusinessError) => { 
  console.info('Failed to metadataOutput stop '+ err.code); 
}); 
} 
aboutToDisappear(){ 
  if(this.captureSession != undefined){ 
    this.captureSession.release().then(()=>{ 
      console.error("release session"); 
    }) 
  } 
 
  if(this.previewOt != undefined){ 
    this.previewOt.release().then(()=>{ 
      console.error("release previewOt"); 
    }) 
  } 
 
  if(this.cameraInput != undefined) { 
    this.cameraInput.close().then(()=>{ 
      console.error("release cameraInput"); 
    }) 
  } 
 
}

注明适配的版本信息

本示例为Stage模型,支持API version 10。

本示例需要使用DevEco Studio 4.0 Release版本进行编译运行。

分享
微博
QQ
微信
回复
2024-05-22 15:31:42
相关问题
如何检测当前相机服务的状态
762浏览 • 1回复 待解决
DevEco Studio检测不到设备
5709浏览 • 1回复 待解决
打开相机:直接使用相机拍照能力
388浏览 • 1回复 待解决
无法检测到hpm!求助各位大佬
5947浏览 • 2回复 已解决
如何调用系统相机拍照?
404浏览 • 1回复 待解决
使用js语言如何开发相机
1600浏览 • 1回复 待解决
如何简单实现相机关闭
138浏览 • 1回复 待解决
基于CameraKit对相机进行操作
182浏览 • 1回复 待解决
请教arkts可以调用相机了吗?
3290浏览 • 1回复 待解决
我的p40PRO检测不到鸿蒙系统
10241浏览 • 4回复 待解决
我的nova7pro咋检测不到鸿蒙技术
7243浏览 • 3回复 待解决
相机有没有HDR模式的采集
526浏览 • 1回复 待解决
如何从app跳转到系统相机
4130浏览 • 1回复 已解决