iOS平台下ZXing类库GBK乱码解决办法

来源:互联网 发布:c#网络编程 pdf 编辑:程序博客网 时间:2024/04/29 16:41
最近遇到这个问题,找了一下资料,没有现成的方案,在这里分享一下心得,方便后来人。


ZXing在iOS平台下遇到GBK字符串产生乱码的原因是ZXing没有对GBK编码字符作判断,于是遇到GBK编码字符串时使用错误的编码,所以我们需要对源码作一些修改。


打开ZXingWidget工程
1.找到DecodedBitStreamParser.h 添加GBK编码变量

?
1
2
3
4
5
6
7
staticc*****t char*ASCII;
staticc*****t char*ISO88591;
staticc*****t char*UTF8;
staticc*****t char*SHIFT_JIS;
staticc*****t char*EUC_JP;
 
staticc*****t char*GBK; // 添加


2.在DecodedBitStreamParser.cpp中

?
1
2
3
4
5
6
7
c*****tchar*DecodedBitStreamParser::ASCII = "ASCII";
c*****tchar*DecodedBitStreamParser::ISO88591 = "ISO-8859-1";
c*****tchar*DecodedBitStreamParser::UTF8 = "UTF-8";
c*****tchar*DecodedBitStreamParser::SHIFT_JIS = "SHIFT_JIS";
c*****tchar*DecodedBitStreamParser::EUC_JP = "EUC-JP";
 
c*****tchar*DecodedBitStreamParser::GBK = "GB2312";// 添加


3.同样在DecodedBitStreamParser.cpp中,找到guessEncoding方法,添加“GBK stuff"代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
c*****tchar*
DecodedBitStreamParser::guessEncoding(unsignedchar*bytes, intlength) {
  c*****tboolASSUME_SHIFT_JIS = false;
  charc*****t* c*****t PLATFORM_DEFAULT_ENCODING="UTF-8";
 
  // Does it start with the UTF-8 byte order mark? then guess it's UTF-8
  if(length > 3 && bytes[0] == (unsigned char)0xEF && bytes[1] == (unsigned char)0xBB && bytes[2]
      == (unsigned char)0xBF) {
    returnUTF8;
  }
 
    // 添加
    // GBK stuff -- add by Ren JianSheng at 2012/2/29
     
    intidx = 0;
    for(idx = 0; idx < length; idx++) {
        intval = bytes[idx], val2;
        if(val < 128)
            continue;
        if(idx + 1 >= length)
            break;
        val2 = bytes[idx + 1];
        if(((val >= 0xA1 && val <= 0xA9) && (val2 >= 0xA1 && val2 <= 0xFE)) ||
            ((val >= 0xB0 && val <= 0xF7) && (val2 >= 0xA1 && val2 <= 0xFE)) ||
            ((val >= 0x81 && val <= 0xA0) && (val2 >= 0x40 && val2 <= 0xFE && val2 != 0x7F)) ||
            ((val >= 0xAA && val <= 0xFE) && (val2 >= 0x40 && val2 <= 0xA0 && val2 != 0x7F)) ||
            ((val >= 0xA8 && val <= 0xA9) && (val2 >= 0x40 && val2 <= 0xA0 && val2 != 0x7F)) ||
            ((val >= 0xAA && val <= 0xAF) && (val2 >= 0xA1 && val2 <= 0xFE)) ||
            ((val >= 0xF8 && val <= 0xFE) && (val2 >= 0xA1 && val2 <= 0xFE)) ||
            ((val >= 0xA1 && val <= 0xA7) && (val2 >= 0x40 && val2 <= 0xA0 && val2 != 0x7F))) {
            idx++;
        }else{
            break;
        }
    }
     
    if(idx == length) {
        printf("%s","GBK String!");
        returnGBK;
    }
    
   // GBK stuff end
...
}



然后重新编译,搞定!  

最后附上一个GBK二维码测试地址:http://info.ehome.hc360.com/2011/12/270829206343.shtml
原创粉丝点击