Starting from API version 20, white balance effects are supported. White balance is a camera color correction technique that produces different effects in photos by adjusting the white balance settings. Currently, white balance effects are supported for photo capture (PhotoSession), video recording (VideoSession), and secure camera mode (SecureSession).
Read Camera for the API reference.
Import the camera module, which provides camera-related properties and methods.
import { camera } from '@kit.CameraKit';
import { BusinessError } from '@kit.BasicServicesKit'; Set white balance. Two methods are provided for setting white balance.
Method 1: Call isWhiteBalanceModeSupported to check whether a specific white balance mode is supported. Then, call setWhiteBalanceMode and getWhiteBalanceMode to set and obtain the white balance mode. Note that you can only view the currently set white balance mode. The available white balance modes include automatic, manual, cloudy, incandescent, fluorescent, daylight, and locked. If both a white balance mode and a white balance value are set, only one takes effect, with the white balance mode taking precedence by default.
function isWhiteBalanceModeSupported(session: camera.PhotoSession | camera.VideoSession): boolean {
let status: boolean = false;
let whiteBalanceMode: camera.WhiteBalanceMode | undefined = undefined;
try {
let mode: camera.WhiteBalanceMode = camera.WhiteBalanceMode.DAYLIGHT;
status = session.isWhiteBalanceModeSupported(mode);
if(status){
session.setWhiteBalanceMode(mode);
}
whiteBalanceMode = session.getWhiteBalanceMode();
} catch (error) {
let err = error as BusinessError;
console.error(`The isWhiteBalanceModeSupported call failed. error code: ${err.code}`);
}
return status;
} Method 2: Call getWhiteBalanceRange to obtain the range of white balance values supported by the device. Then, call setWhiteBalance and getWhiteBalance to set and obtain the white balance value. Note that you can only view the currently set white balance value.
function getWhiteBalanceRange(session: camera.PhotoSession | camera.VideoSession): Array<number> {
let range: Array<number> = [];
try {
range = session.getWhiteBalanceRange();
let whiteBalance: number = 3000;
if(whiteBalance >= range[0] && whiteBalance <= range[1]) {
session.setWhiteBalance(whiteBalance);
}
whiteBalance = session.getWhiteBalance();
} catch (error) {
let err = error as BusinessError;
console.error(`The getWhiteBalanceRange call failed. error code: ${err.code}`);
}
return range;
}