文档管理中心
FAQ媒体开发拍照和图片相机开发(Camera)如何避免预览流产生畸变

如何避免预览流产生畸变

使用下列代码获取设备支持的宽和高,然后根据手机屏幕的宽高设置最合适的预览流分辨率,并使surface和XComponent的宽高一致。

收起
自动换行
深色代码主题
复制
  1. //The aspect ratio of the preview stream and the video output stream resolution should be consistent
  2. let previewProfilesArray: Array<camera.Profile> = cameraOutputCap.previewProfiles;
  3. let position: number = 0;
  4. if (previewProfilesArray != null) {
  5. previewProfilesArray.forEach((value: camera.Profile,index: number) => {
  6. // View supported preview sizes
  7. console.info(TAG,
  8. `支持的预览尺寸: [${value.size.width},${value.size.height},${value.size.width / value.size.height}]`);
  9. if (value.size.width === 2592 && value.size.height === 1200) {
  10. position = index;
  11. }
  12. })
  13. } else {
  14. console.error(TAG,"createOutput photoProfilesArray == null || undefined");
  15. }
  16. let photoProfilesArray: Array<camera.Profile> = cameraOutputCap.photoProfiles;
  17. if (!photoProfilesArray) {
  18. console.error(TAG,"createOutput photoProfilesArray == null || undefined");
  19. }
  20. this.xComponentWidth = previewProfilesArray[position].size.width;
  21. this.xComponentHeight = previewProfilesArray[position].size.height;
  22. this.mXComponentController.setXComponentSurfaceSize({
  23. surfaceWidth: this.xComponentWidth,
  24. surfaceHeight: this.xComponentHeight
  25. });
  26. // Create a preview output stream, where the parameter surfaceId refers to the XComponent component mentioned earlier,
  27. // and the preview stream is the surface provided by the XComponent component
  28. try {
  29. previewOutput = cameraManager.createPreviewOutput(previewProfilesArray[position],surfaceId);
  30. } catch (error) {
  31. let err = error as BusinessError;
  32. console.error(TAG,`Failed to create the PreviewOutput instance. error code: ${err.code}`);
  33. }
  34. if (previewOutput === undefined) {
  35. return;
  36. }
  37. // Monitor preview output error message
  38. previewOutput.on('error',(error: BusinessError) => {
  39. console.error(TAG,`Preview output error code: ${error.code}`);
  40. });
  41. // Create an ImageReceiver object and set photo parameters:
  42. // The resolution size is set based on the current device's supported photo resolution size obtained from the previous photoProfilesArray
  43. let size: image.Size = {
  44. height: 1200,
  45. width: 2592
  46. }
  47. let imageReceiver: image.ImageReceiver = image.createImageReceiver(size,4,8);
在 FAQ 中进行搜索
请输入您想要搜索的关键词