目录

How do I prevent preview stream distortion?

Obtain the width and height supported by the device, set the most appropriate preview stream resolution based on the width and height of the phone screen, and ensure that the aspect ratio of the surface is consistent with that of the XComponent. Refer to the code snippet below.

//The aspect ratio of the preview stream and the video output stream resolution should be consistent
let previewProfilesArray: Array<camera.Profile> = cameraOutputCap.previewProfiles;

let position: number = 0;
if (previewProfilesArray != null) {
  previewProfilesArray.forEach((value: camera.Profile,index: number) => {
    // View supported preview sizes
    console.info(TAG,
      `Supported preview size: [${value.size.width},${value.size.height},${value.size.width / value.size.height}]`);
    if (value.size.width === 2592 && value.size.height === 1200) {
      position = index;
    }
  })
} else {
  console.error(TAG,"createOutput photoProfilesArray == null || undefined");
}

let photoProfilesArray: Array<camera.Profile> = cameraOutputCap.photoProfiles;
if (!photoProfilesArray) {
  console.error(TAG,"createOutput photoProfilesArray == null || undefined");
}

this.xComponentWidth = previewProfilesArray[position].size.width;
this.xComponentHeight = previewProfilesArray[position].size.height;

this.mXComponentController.setXComponentSurfaceSize({
  surfaceWidth: this.xComponentWidth,
  surfaceHeight: this.xComponentHeight
});
// Create a preview output stream, where the parameter surfaceId refers to the XComponent component mentioned earlier,
// and the preview stream is the surface provided by the XComponent component
try {
  previewOutput = cameraManager.createPreviewOutput(previewProfilesArray[position],surfaceId);
} catch (error) {
  let err = error as BusinessError;
  console.error(TAG,`Failed to create the PreviewOutput instance. error code: ${err.code}`);
}
if (previewOutput === undefined) {
  return;
}

// Monitor preview output error message
previewOutput.on('error',(error: BusinessError) => {
  console.error(TAG,`Preview output error code: ${error.code}`);
});

// Create an ImageReceiver object and set photo parameters:
// The resolution size is set based on the current device's supported photo resolution size obtained from the previous photoProfilesArray
let size: image.Size = {
  height: 1200,
  width: 2592
}
let imageReceiver: image.ImageReceiver = image.createImageReceiver(size,4,8);