直接看代码:
if (mCameraRenderer.getFUCamera().getCameraFacing() == CameraFacingEnum.CAMERA_BACK){
if (mCameraRenderer.getFUCamera().getmFaceUnityCamera().getCamera() != null){
Camera.Parameters mParameters = mCameraRenderer.getFUCamera().getmFaceUnityCamera().getCamera().getParameters();
if (mParameters.getFlashMode().equals(Camera.Parameters.FLASH_MODE_OFF)){
mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
iv_photo_flash_change.setImageResource(R.mipmap.ic_photo_flash_open);
} else {
mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
iv_photo_flash_change.setImageResource(R.mipmap.ic_photo_flash_close);
}
mCameraRenderer.getFUCamera().getmFaceUnityCamera().getCamera().setParameters(mParameters);
}
}
关键代码
- 打开手电筒mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
- 关闭手电筒mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
我上面的Camera用的是相芯美颜的,不过用不用第三方都一样的,我看相芯里面代码也是用的是手机Camera。
生成Camera代码如下:
fun openCamera() {
if (mCamera != null) {
return
}
try {
val isFront = mCameraFacing == CameraFacingEnum.CAMERA_FRONT
val cameraId = if (isFront) mFrontCameraId else mBackCameraId
mCamera = Camera.open(cameraId)
if (mCamera == null) {
throw java.lang.RuntimeException("No camera")
}
mExposureCompensation = EXPOSURE_COMPENSATION
CameraUtils.setCameraDisplayOrientation(mContext, cameraId, mCamera!!)
val parameters: Camera.Parameters = mCamera!!.parameters
CameraUtils.setFocusModes(parameters)
CameraUtils.chooseFrameRate(parameters,mIsHighestRate )
val size: IntArray = CameraUtils.choosePreviewSize(parameters, mCameraWidth, mCameraHeight)
mCameraWidth = size[0]
mCameraHeight = size[1]
parameters.previewFormat = ImageFormat.NV21
CameraUtils.setParameters(mCamera, parameters)
// log camera all parameters
logCameraParameters()
startPreview()
} catch (e: Exception) {
e.printStackTrace()
FULogger.e(TAG, "openCamera:${e.message}")
}
}
网友评论