1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { camera } from '@kit.CameraKit'; 17import Logger from './Logger'; 18import { CameraDataModel } from '../../model/CameraDateModel'; 19import { CommonConstants as Const } from '../CommonConstants'; 20 21const TAG = 'CAMERA_CHECK'; 22 23function getPreviewProfile(previewProfiles: camera.Profile[], size: camera.Size, 24 isHDRVivid: number): undefined | camera.Profile { 25 let previewProfile: undefined | camera.Profile = previewProfiles.find((profile: camera.Profile) => { 26 const resolutionMatch = profile.size.width === size.width && profile.size.height === size.height; 27 28 if (!resolutionMatch) { 29 return; 30 } 31 if (isHDRVivid) { 32 return profile.format === camera.CameraFormat.CAMERA_FORMAT_YCRCB_P010 && resolutionMatch; 33 } else { 34 return profile.format === camera.CameraFormat.CAMERA_FORMAT_YUV_420_SP && resolutionMatch; 35 } 36 }); 37 return previewProfile; 38} 39 40function setFirstPreviewProfile(previewProfiles: camera.Profile[]) { 41 return previewProfiles[0]; 42} 43 44export function previewProfileCameraCheck(cameraManager: camera.CameraManager, 45 cameraData: CameraDataModel): undefined | camera.Profile { 46 let cameraDevices = cameraManager.getSupportedCameras(); 47 if (cameraDevices !== undefined && cameraDevices.length <= 0) { 48 Logger.error(TAG, 'cameraManager.getSupportedCameras error!'); 49 return; 50 } 51 52 let profiles: camera.CameraOutputCapability = 53 cameraManager.getSupportedOutputCapability(cameraDevices[0], camera.SceneMode.NORMAL_VIDEO); 54 if (!profiles) { 55 Logger.error(TAG, 'cameraManager.getSupportedOutputCapability error!'); 56 return; 57 } 58 59 let previewProfilesArray: camera.Profile[] = profiles.previewProfiles; 60 if (!previewProfilesArray) { 61 Logger.error('createOutput previewProfilesArray == null || undefined'); 62 return; 63 } 64 65 let videoSize: camera.Size = { 66 width: 1920, 67 height: 1080 68 } 69 let previewProfile: undefined | camera.Profile = 70 getPreviewProfile(previewProfilesArray, videoSize, cameraData.isHDRVivid); 71 if (!previewProfile) { 72 Logger.error('previewProfile is not found, set to first profile'); 73 previewProfile = setFirstPreviewProfile(previewProfilesArray); 74 return previewProfile; 75 } 76 return previewProfile; 77} 78 79export function setFirstCameraProfile(cameraManager: camera.CameraManager) { 80 let cameraDevices = cameraManager.getSupportedCameras(); 81 if (cameraDevices !== undefined && cameraDevices.length <= 0) { 82 Logger.error(TAG, 'cameraManager.getSupportedCameras error!'); 83 return; 84 } 85 86 let profiles: camera.CameraOutputCapability = 87 cameraManager.getSupportedOutputCapability(cameraDevices[0], camera.SceneMode.NORMAL_VIDEO); 88 if (!profiles) { 89 Logger.error(TAG, 'cameraManager.getSupportedOutputCapability error!'); 90 return; 91 } 92 93 let encoderProfiles: camera.VideoProfile[] = profiles.videoProfiles; 94 if (!encoderProfiles) { 95 Logger.error(TAG, 'Get encoderProfiles error!'); 96 return; 97 } 98 return encoderProfiles[0]; 99} 100 101export function encoderProfileCameraCheck(cameraManager: camera.CameraManager, 102 cameraData: CameraDataModel): undefined | camera.VideoProfile { 103 let cameraDevices = cameraManager.getSupportedCameras(); 104 if (cameraDevices !== undefined && cameraDevices.length <= 0) { 105 Logger.error(TAG, 'cameraManager.getSupportedCameras error!'); 106 return; 107 } 108 109 let profiles: camera.CameraOutputCapability = 110 cameraManager.getSupportedOutputCapability(cameraDevices[0], camera.SceneMode.NORMAL_VIDEO); 111 if (!profiles) { 112 Logger.error(TAG, 'cameraManager.getSupportedOutputCapability error!'); 113 return; 114 } 115 116 let encoderProfiles: camera.VideoProfile[] = profiles.videoProfiles; 117 if (!encoderProfiles) { 118 Logger.error(TAG, 'Get encoderProfiles error!'); 119 return; 120 } 121 if (cameraData.format == camera.CameraFormat.CAMERA_FORMAT_RGBA_8888) { 122 return encoderProfiles[0]; 123 } 124 let encoderProfile: undefined | camera.VideoProfile = encoderProfiles.find((profile: camera.VideoProfile) => { 125 const resolutionMatch = 126 profile.size.width === cameraData.cameraWidth && 127 profile.size.height === cameraData.cameraHeight; 128 129 if (!resolutionMatch) { 130 return; 131 } 132 if (cameraData.isHDRVivid) { 133 if (cameraData.frameRate === Const.FRAMERATE_VIDEO_30FPS) { 134 return resolutionMatch && 135 profile.format === camera.CameraFormat.CAMERA_FORMAT_YCBCR_P010 && 136 profile.frameRateRange.min === 1 && 137 profile.frameRateRange.max === 30; 138 } else { 139 return resolutionMatch && 140 profile.format === camera.CameraFormat.CAMERA_FORMAT_YCBCR_P010 && 141 profile.frameRateRange.min === cameraData.frameRate && 142 profile.frameRateRange.max === cameraData.frameRate; 143 } 144 } else { 145 if (cameraData.frameRate === Const.FRAMERATE_VIDEO_30FPS) { 146 return resolutionMatch && 147 profile.format === camera.CameraFormat.CAMERA_FORMAT_YUV_420_SP && 148 profile.frameRateRange.min === 1 && 149 profile.frameRateRange.max === 30; 150 } else { 151 return resolutionMatch && 152 profile.format === camera.CameraFormat.CAMERA_FORMAT_YUV_420_SP && 153 profile.frameRateRange.min === cameraData.frameRate && 154 profile.frameRateRange.max === cameraData.frameRate; 155 } 156 } 157 }); 158 return encoderProfile; 159}