1. 程式人生 > >Android識別文字編碼

Android識別文字編碼

Android識別文字編碼

一、使用方式Usage

  • 下載andnext_utils模組

下載地址:https://github.com/jicanghai37927/WhatsAndroid/tree/master/andnext_utils

  • CharsetUtils獲取文字編碼
String getCharset(@NonNull byte[] bytes, String defaultCharset)
String getCharset(@NonNull byte[] bytes, int length, String defaultCharset)
String getCharset(@NonNull byte[] bytes, int offset, int length, String defaultCharset)

二、 程式碼解析

實際的程式碼解析工作由UniversalDetector來完成。

static final String getCharset(UniversalDetector detector, byte[] bytes, int offset, int length) {
        detector.handleData(bytes, offset, length);
        detector.dataEnd();

        String encoding = detector.getDetectedCharset();

        detector.reset();

        return encoding;
    }

UniversalDetector是Mozilla的編碼識別庫。GitHub上可以找到Java的實現版本。

三、參考資料

  • juniversalchardet:

https://github.com/albfernandez/juniversalchardet

https://mvnrepository.com/artifact/com.github.albfernandez/juniversalchardet