3

Jchardet——支持检测并输出文件编码方式的组件

 1 year ago
source link: https://blog.51cto.com/OpenHarmony/5749371
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Jchardet——支持检测并输出文件编码方式的组件​

Jchardet是OpenAtom OpenHarmony(以下简称“OpenHarmony”)系统的一款检测文本编码的组件。当上传一个文件时,组件可以检测并输出该文件中文本使用的编码方式。

支持的编码格式有:ASCII编码、UTF-8编码、UTF-16LE编码、Shift-JIS编码、ISO-2022-JP编码、ISO-2022-CN编码、ISO-2022-KR编码、Big5编码、UTF-16BE编码、x-euc-tw编码、EUC-KR编码、ECU-JP编码、GB18030编码、HZ-GB-2312编码、Windows-1252编码、GB2312编码。

Jchardet——支持检测并输出文件编码方式的组件_OpenHarmony

开发环境​

安装IDE:支持DevEco Studio 3.0 Release(Build Version 3.0.0.993)版本。

安装SDK:OpenHarmony API version 8及以上版本。

使用方法​

需要先配置OpenHarmony npm仓,请参考安装教程

 ​https://gitee.com/openharmony-tpc/docs/blob/master/OpenHarmony_npm_usage.md​

1. 下载jchardet组件

npm install @ohos/jchardet –-save

2. 在page页面导入。

import {nsICharsetDetectionObserver,
nsDetector,nsPSMDetector} from '@ohos/jchardet/

3. 初始化编码检测器。

//lang为输入的语言种类
//0 => All
//1 => Japanese
//2 => Chinese
//3 => Simplified Chinese
//4 => Traditional Chinese
//5 => Korean
//6 => Dont know (default)
let det: nsDetector = new nsDetector(lang);

4. 在检测器的初始化函数中定义回调函数,接受编码检测结果。

det.Init(new class implements nsICharsetDetectionObserver {
Notify(charset: string): void {
console.log("CHARSET = " + charset);
}
})

4.1 向编码检测器提供数据,首选判断是否Ascii编码,如果是Ascii编码,则程序结束。

let byteData = new Int8Array(buf); //buf为文件的ArrayBuffer
let isAscii:boolean = det.isAscii(byteData);

4.2 如果检测到某一种编码,则进去初始化时定义的回调,程序结束。

let done:boolean = det.DoIt(byteData, false);

4.3 如果检测到多种编码,则获取编码列表,程序结束。

let prob: Array<string> = det.getProbableCharsets();

通过本篇文章介绍,您对OpenHarmony Jchardet组件应该有了初步的了解。我们所有的源码和指导文档都已经开源,如果您对本篇文章内容以及所实现的Demo感兴趣,可以根据本篇文章介绍自行下载OpenHarmony Jchardet源码(https://gitee.com/openharmony-sig/jchardet)进行研究和使用。同时也欢迎更多开发者与我们共享开发成果,分享技术解读与经验心得。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK