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

1、如何编译Tensorflow lite库?

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

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

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;  
}
相关推荐
Huang兄8 小时前
鸿蒙-List和Grid拖拽排序:仿微信小程序删除效果
harmonyos·arkts·arkui
anyup1 天前
🔥2026最推荐的跨平台方案:H5/小程序/App/鸿蒙,一套代码搞定
前端·uni-app·harmonyos
Ranger09291 天前
鸿蒙开发新范式:Gpui
rust·harmonyos
Huang兄1 天前
鸿蒙-深色模式适配
harmonyos·arkts·arkui
SummerKaze3 天前
为鸿蒙开发者写一个 nvm:hmvm 的设计与实现
harmonyos
在人间耕耘5 天前
HarmonyOS Vision Kit 视觉AI实战:把官方 Demo 改造成一套能长期复用的组件库
人工智能·深度学习·harmonyos
REDcker5 天前
WebCodecs VideoDecoder 的 hardwareAcceleration 使用
前端·音视频·实时音视频·直播·webcodecs·videodecoder
gihigo19985 天前
基于TCP协议实现视频采集与通信
网络协议·tcp/ip·音视频
王码码20355 天前
Flutter for OpenHarmony:socket_io_client 实时通信的事实标准(Node.js 后端的最佳拍档) 深度解析与鸿蒙适配指南
android·flutter·ui·华为·node.js·harmonyos
HarmonyOS_SDK5 天前
【FAQ】HarmonyOS SDK 闭源开放能力 — Ads Kit
harmonyos