使用HarmonyOS 的主体分割分割出来的图片是黑色背景?

使用主体分割,文档地址如下:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/core-vision-subjectsegmentation-api-V5

保存到沙箱文件中,保存的是png格式,图片背景是黑色的,不是透明背景,保存代码如下:

async savePhoto(pixmap: image.PixelMap): Promise<string> {
  return new Promise(async (resolve, reject) => {

  let context = getContext(this);
  let cacheDir = context.cacheDir;
  let imagePackerApi: image.ImagePacker = image.createImagePacker()
  let packOpts: image.PackingOption = {
    format: "image/jpeg", quality: 98
  };
  // 通过PixelMap进行编码。compressedImageData为打包获取到的图片文件流。
  let compressedImageData: ArrayBuffer = await imagePackerApi.packing(pixmap, packOpts);
  let file = fs.openSync(cacheDir + '/' + new Date().getTime() + 'imageeditor.png',
    fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
  let packOpts1: image.PackingOption = { format: "image/png", quality: 98 };
  let imagePackerApi1: image.ImagePacker = image.createImagePacker()
  const imageSourceApi: image.ImageSource = image.createImageSource(compressedImageData);
  imagePackerApi1.packToFile(imageSourceApi, file.fd, packOpts1).then(() => {
  // 直接打包进文件
  // fs.closeSync(file);
  resolve(file.path)
  // this.processValue = 70
  // resolve(file.path)
}).catch((error: BusinessError) => {
  console.error('Failed to pack the image. And the error is: ' + error);
})
})
}

主体分割代码如下:

export default class SegUtils {
  async subjectSegmentationTest(): Promise<subjectSegmentation.SubjectResult> {
    const TAG: string = "ImageSegmentationSample";
    let chooseImage: image.PixelMap | undefined = undefined;
    let maxNum: string = '1';

    // Select an image from the gallery
    let PhotoSelectOptions = new picker.PhotoSelectOptions();
    PhotoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
    PhotoSelectOptions.maxSelectNumber = 1;
    let photoPicker = new picker.PhotoViewPicker();
    let PhotoSelectResult = await photoPicker.select(PhotoSelectOptions);
    let uris = PhotoSelectResult.photoUris;

    if (uris.length !== 1) {
      hilog.info(0x0000, TAG, "Selected uris length is not 1");
      // return ;
    }

    // Convert the selected image to PixelMap
    let fileSource = await fileIo.open(uris[0], fileIo.OpenMode.READ_ONLY);
    let imageSource = image.createImageSource(fileSource.fd);
    chooseImage = await imageSource.createPixelMap();

    hilog.info(0x0000, TAG, `chooseImage=${chooseImage}`);

    if (!chooseImage) {
      hilog.info(0x0000, TAG, "chooseImage is undefined");
      // return;
    }

    // Call the image segmentation interface
    let visionInfo: subjectSegmentation.VisionInfo = {
      pixelMap: chooseImage,
    };
    let config: subjectSegmentation.SegmentationConfig = {
      maxCount: parseInt(maxNum),
      enableSubjectDetails: true,
      enableSubjectForegroundImage: true,
    };
    let data: subjectSegmentation.SegmentationResult = await subjectSegmentation.doSegmentation(visionInfo, config);
    let outputString = `Subject count: ${data.subjectCount}\n`;
    outputString += `Max subject count: ${config.maxCount}\n`;
    outputString += `Enable subject details: ${config.enableSubjectDetails ? 'Yes' : 'No'}\n\n`;
    let segBox: subjectSegmentation.Rectangle = data.fullSubject.subjectRectangle;
    let segBoxString =
      `Full subject box:\nLeft: ${segBox.left}, Top: ${segBox.top}, Width: ${segBox.width}, Height: ${segBox.height}\n\n`;
    outputString += segBoxString;
    if (config.enableSubjectDetails) {
      outputString += 'Individual subject boxes:\n';
      if (data.subjectDetails) {
        for (let i = 0; i < data.subjectDetails.length; i++) {
          let detailSegBox: subjectSegmentation.Rectangle = data.subjectDetails[i].subjectRectangle;
          outputString += `Subject ${i +
            1}:\nLeft: ${detailSegBox.left}, Top: ${detailSegBox.top}, Width: ${detailSegBox.width}, Height: ${detailSegBox.height}\n\n`;
        }
      }
    }

    hilog.info(0x0000, TAG, "Segmentation result: " + outputString);
    return data.fullSubject
  }
}
阅读 415
1 个回答

可以尝试png图片解码后生成pixelmap方式,参考链接:

https://developer.huawei.com/consumer/cn/doc/atomic-guides-V5/atomic-image-decoding-V5

全局单例示例参考:

import { image } from '@kit.ImageKit';

/*export default class Photourlparams {
  photourl:image.PixelMap;

  constructor(photourl:image.PixelMap) {
    this.photourl=photourl
  }
}*/

export class GlobalVariable {
  private static instance: GlobalVariable;
  //private audioRoutingManager: audio.AudioRoutingManager = audio.getAudioManager().getRoutingManager();

  private photourl:image.PixelMap;

  private constructor(photourl:image.PixelMap) {
    this.photourl = photourl;
  }

  public static getInstance(photourl:image.PixelMap): GlobalVariable {
    if (!GlobalVariable.instance) {
      GlobalVariable.instance = new GlobalVariable(photourl);
    }
    return GlobalVariable.instance;
  }

  public getPPP(): image.PixelMap {
    return this.photourl;
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
HarmonyOS
子站问答
访问
宣传栏