# juniversalchardet **Repository Path**: zhangjun93/juniversalchardet ## Basic Information - **Project Name**: juniversalchardet - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 16 - **Created**: 2022-06-07 - **Last Updated**: 2022-07-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # juniversalchardet ## 简介 >juniversalchardet 是一个字符编码高效识别检测库 ![动画](./动画.gif) ## 下载安装 ``` npm install @ohos/juniversalchardet --save ``` OpenHarmony npm环境配置等更多内容,请参考 [如何安装OpenHarmony npm包](https://gitee.com/openharmony-tpc/docs/blob/master/OpenHarmony_npm_usage.md) 。 ## 使用说明 ### 文本编码检测 提供两种方法检测 - 方法一 通过文件路径检测编码 UniversalDetector.detectCharset(path); ```ts // 传入文件路径参数 let filePath = getPath() + file; UniversalDetector.detectCharset(filePath).then((encoing) => { // encoding 编码名称 eg:UTF-8 }).catch(err=>{ // 异常信息 }); ``` - 方法二 通过数据检测编码 UniversalDetector.handleData(buf, offset, length); ```ts detect(data: ArrayBuffer): string { //创建检测对象 let detector: UniversalDetector = new UniversalDetector(); // 传入检测数据并实时判断编码格式 detector.handleData(data, 0, data.byteLength); // 标记检测数据 读取结束 detector.dataEnd(); // 获取检测结果 let detected: string = detector.getDetectedCharset(); // 释放资源 detector.reset(); return detected; } ``` ## 接口说明 `let detector: UniversalDetector = new UniversalDetector();` 1. 向检测器输入数据 `detector.handleData()` 2. 从InputStream获取内容的符号集 `UniversalDetector.detectCharset()` 3. 读取结束 `detector.dataEnd()` 4. 检测到的编码 `detector.getDetectedCharset()` 5. 重置 `detector.reset()` ## 兼容性 支持 OpenHarmony API version 8 及以上版本。 ## 目录结构 ``` |---- juniversalchardet | |---- entry # 示例代码文件夹 | |---- juniversalchardet # juniversalchardet库文件夹 | |----src | |----main | |----ets | |----prober # 检测器文件 | |----constextanalysis # 语境分析 | |----distributionanalysis # 分布分析 | |----statemachine # 业务处理 | |---- index.ets # 对外接口 | |---- README.md # 安装使用方法 ``` ## 贡献代码 使用过程中发现任何问题都可以提 [Issue](https://gitee.com/openharmony-sig/juniversalchardet/issues) 给我们,当然,我们也非常欢迎你给我们发 [PR](https://gitee.com/openharmony-sig/juniversalchardet/pulls) 。 ## 开源协议 本项目基于 [MPL-1.1 License]( https://gitee.com/openharmony-sig/juniversalchardet/blob/master/LICENSE ) ,请自由地享受和参与开源。