【每日学点鸿蒙知识】tensorflowlite编译、音频编码线程、沉浸式状态栏、TextArea最大字节数限制等

1、如何编译Tensorflow lite库?

之前项目基于tflite推理引擎做人脸识别的功能,鸿蒙侧如何复用tflite模型?

tflite对Android和iOS本身支配了GPU支持,但是鸿蒙侧目前并没有,鸿蒙提供了自己的推理引擎,而且支持将tflite模型转换为鸿蒙推理引擎模型,建议使用鸿蒙官方方案。

  • 模型转换文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/hiaifoundation-model-conversion-V5
  • 端侧推理文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/hiaifoundation-on-device-deployment-V5

2、音频编码需要单独设置线程吗?

编码可以不另外起线程,系统的编解码会起线程。写文件是应用自己根据自身需求另外起线程处理。

3、创建音频编码器配置参数报错

// 通过 codecname 创建编码器
OH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_AUDIO_MPEG, true);
const char *name = OH_AVCapability_GetName(capability);
audioEnc_ = OH_AudioCodec_CreateByName(name);

 // 配置最大输入长度, 每帧音频数据的大小(可选)
    uint32_t DEFAULT_MAX_INPUT_SIZE = inSamplerate * TIME_PER_FRAME * inChannel * sizeof(short); // aac
    OH_AVFormat *format = OH_AVFormat_Create();
    // 写入format
    OH_AVFormat_SetIntValue(format,OH_MD_KEY_AUD_CHANNEL_COUNT, inChannel);
    OH_AVFormat_SetIntValue(format,OH_MD_KEY_AUD_SAMPLE_RATE, inSamplerate);
    OH_AVFormat_SetLongValue(format,OH_MD_KEY_BITRATE, outBitrate);
    OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_FORMAT);
    OH_AVFormat_SetLongValue(format,OH_MD_KEY_CHANNEL_LAYOUT, CHANNEL_LAYOUT);
//     OH_AVFormat_SetIntValue(format,OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
    // 配置编码器
    ret = OH_AudioCodec_Configure(audioEnc_, format);
    if (ret != AV_ERR_OK) {
        printLog("configure failed");
    }

传入参数:采样率48000, 声道数2, 码率128,ret返回为3:

/**  
 * @error invalid argument. */
AV_ERR_INVALID_VAL = 3,

输出码率设置太低,改成128000。

4、如何设置沉浸式状态栏?

在UIAbility onWindowStageCreate方法中设置沉浸式状态栏:

async function enterImmersion(windowClass: window.Window) {  
  // 设置窗口布局为沉浸式布局  
  await windowClass.setWindowLayoutFullScreen(true)  
}

async onWindowStageCreate(windowStage: window.WindowStage): Promise<void> {  
  await enterImmersion(windowClass)  
}

5、如何设置TextArea最大允许输入字节数?

可以在onChange回调中,自行计算当前输入的字节长度(参考示例如下),再与服务端的最大字节长度做对比,若超出则禁止输入:

export function CalUtf8BytesLen(str: string): number {  
  let length = 0;  
  for (const char of str) {  
    let code = char.charCodeAt(0);  
    if (code <= 0x7f) {  
      length += 1; // 0xxxxxxx  
    } else if (code <= 0x7ff) {  
      length += 2; // 110xxxxx 10xxxxxx  
    } else if (code >= 0xd800 && code <= 0xdfff) {  
      // surrogate pair  
      if (char.length === 2) {  
        code = (char.charCodeAt(0) - 0xd800) * 0x400 + char.charCodeAt(1) - 0xdc00 + 0x10000;  
        length += 4; // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx  
      } else {  
        throw new Error('Invalid surrogate pair');  
      }  
    } else {  
      length += 3; // 1110xxxx 10xxxxxx 10xxxxxx  
    }  
  }  return length;  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

轻口味

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值