Cameras in secure mode are mainly provided for high-security applications like banking that require features such as biometric verification. To use cameras in secure mode, Crypto Architecture Kit and TrustedAppService in Device Security Kit are required in addition to Camera Kit.
The procedure for using a secure mode camera is as follows:
Through Camera Kit, an application enables the secure mode of a camera and obtains a camera serial number.
Through Device Security Kit, the application creates an attestation key (with the camera serial number passed as an input parameter) and initializes an attestation session. After attestation session initialization is complete, Device Security Kit returns an anonymous certificate chain.
Through Camera Kit, the application configures input and output streams of the camera in secure mode, focusing on configuring a secure data stream and registering a callback to listen for each frame of the secure image in the secure data stream.
The application parses each frame of the secure image in the secure data stream, and the server verifies the signature of the secure image.
This topic describes the steps carried out using Camera Kit. Steps related to attestation sessions are executed using Device Security Kit. For details, see Image Security Verification.

For details about the APIs, see Camera API Reference.
Import dependencies. Specifically, import the camera and image modules.
import { camera } from '@kit.CameraKit';
import { image } from '@kit.ImageKit'; Select a camera that supports the secure mode.
You can call getSupportedSceneModes in the CameraManager class to obtain all modes supported by the current camera. If the camera supports the secure mode, you can use it for subsequent operations.
Currently, the secure mode is available only for the front camera of mobile phones.
function isSecureCamera(cameraManager: camera.CameraManager, cameraDevice: camera.CameraDevice): boolean {
let sceneModes: Array<camera.SceneMode> = cameraManager.getSupportedSceneModes(cameraDevice);
const secureMode = sceneModes.find(mode => mode === camera.SceneMode.SECURE_PHOTO);
if (secureMode) {
console.info('current device support secure camera!');
return true;
} else {
console.info('current device not support secure camera!');
return false;
}
}
let secureCamera: camera.CameraDevice;
function getSecureCamera(cameraManager: camera.CameraManager): void {
let cameraArray: Array<camera.CameraDevice> = cameraManager.getSupportedCameras();
for (let index = 0; index < cameraArray.length; index++) {
if (isSecureCamera(cameraManager, cameraArray[index])) {
secureCamera = cameraArray[index];
}
}
} Check the output capability supported by the camera in secure mode.
You can call getSupportedOutputCapability of the CameraManager class to obtain the output capability supported by the camera in secure mode.
Currently, the camera in secure mode supports only the output of preview streams, with a recommended resolution of 640 × 480 for optimal performance.
function getSupportedOutputCapability(cameraManager: camera.CameraManager, secureCamera: camera.CameraDevice): void {
let outputCap: camera.CameraOutputCapability =
cameraManager.getSupportedOutputCapability(secureCamera, camera.SceneMode.SECURE_PHOTO);
let previewProfilesArray: Array<camera.Profile> = outputCap.previewProfiles;
} Create the input and output streams.
Two output streams need to be created for the camera in secure mode:
There is no independent data type for the secure data stream, which is a type of preview stream and has the same output capability as the preview stream, except that only the JPEG format is supported during the creation of the ImageReceiver instance.
async function createInputAndOutputs(cameraManager: camera.CameraManager,
secureCamera: camera.CameraDevice,
previewProfile: camera.Profile,
previewSurfaceId: string): Promise<void> {
// Create an input stream.
let cameraInput: camera.CameraInput = cameraManager.createCameraInput(secureCamera);
// Create a common preview output stream.
let previewOutput: camera.PreviewOutput = cameraManager.createPreviewOutput(previewProfile, previewSurfaceId);
// Create a secure data output stream.
const receiver: image.ImageReceiver =
image.createImageReceiver({ width: previewProfile.size.width, height: previewProfile.size.height },
image.ImageFormat.JPEG, 8);
const secureSurfaceId: string = await receiver.getReceivingSurfaceId();
let secureOutput: camera.PreviewOutput = cameraManager.createPreviewOutput(previewProfile, secureSurfaceId);
} Open the camera.
Call open(isSecureEnabled) of CameraInput to open the camera in secure mode and obtain the camera serial number, a mandatory parameter for the security module to create an attestation session.
The camera can be enabled in secure mode and a serial number can be returned only when isSecureEnabled is set to true.
async function openCamera(cameraInput: camera.CameraInput) {
const seqId: bigint = await cameraInput.open(true);
} Use the capabilities of Device Security Kit to create an attestation key and open an attestation session. For details, see Image Security Verification.
Create a secure mode session, configure the streams, and commit the configuration.
Create a secure mode session, add the input and output streams to the session, and mark the secure data stream as secure output using addSecureOutput of SecureSession.
async function openSession(cameraManager: camera.CameraManager,
cameraInput: camera.CameraInput,
previewOutput: camera.PreviewOutput,
secureOutput: camera.PreviewOutput): Promise<void> {
try {
let secureSession: camera.SecureSession = cameraManager.createSession(camera.SceneMode.SECURE_PHOTO);
if (secureSession === undefined) {
console.error('create secureSession failed!');
}
secureSession.beginConfig();
secureSession.addInput(cameraInput);
secureSession.addOutput(previewOutput);
secureSession.addOutput(secureOutput);
secureSession.addSecureOutput(secureOutput); // Mark secureOutput as secure output.
await secureSession.commitConfig();
await secureSession.start();
} catch (err) {
console.error('openSession failed!');
}
} After the secure mode session is started normally, the data within the preview stream and secure data stream is reported frame by frame. The data of each frame of the secure data stream can be obtained by registering the ImageReceiver callback in imageArrival.
function onBuffer(receiver: image.ImageReceiver): void {
receiver.on('imageArrival', () => {
// Read the next image from ImageReceiver.
receiver.readNextImage().then((img: image.Image) => {
// Obtain the component cache from the image.
img.getComponent(image.ComponentType.JPEG).then((component: image.Component) => {
// Secure data stream content. The application parses the buffer content to complete signature authentication.
const buffer = component.byteBuffer;
console.info('Succeeded in getting component byteBuffer.');
})
})
})
} Parse each frame of the image in the secure data stream, and verify the signature of the secure image on the server.
To verify the signature of image data or geographical location data on the device, follow the instructions provided in Verifying the Signature.
Release the camera in secure mode by calling release of Session.