最近遇到这个
问题
,找了一下资料,没有现成的方案,在这里
分享
一下心得,方便后来人。
ZXing 在iOS平台下遇到GBK字符串产生乱码的原因是 ZXing 没有对GBK编码字符作判断,于是遇到GBK编码字符串时 使用 了 错误 的编码,所以我们需要对源码作一些修改。
打开ZXingWidget工程
1.找到DecodedBitStreamParser.h 添加GBK编码变量
2.在DecodedBitStreamParser.cpp中
3.同样在DecodedBitStreamParser.cpp中,找到guessEncoding方法,添加“GBK stuff"代码
然后重新编译,搞定!
最后附上一个GBK二维码 测试 地址: http://info.ehome.hc360.com/2011/12/270829206343.shtml
ZXing 在iOS平台下遇到GBK字符串产生乱码的原因是 ZXing 没有对GBK编码字符作判断,于是遇到GBK编码字符串时 使用 了 错误 的编码,所以我们需要对源码作一些修改。
打开ZXingWidget工程
1.找到DecodedBitStreamParser.h 添加GBK编码变量
1
2
3
4
5
6
7
|
static
c*****t
char
*ASCII;
static
c*****t
char
*ISO88591;
static
c*****t
char
*UTF8;
static
c*****t
char
*SHIFT_JIS;
static
c*****t
char
*EUC_JP;
static
c*****t
char
*GBK;
// 添加
|
2.在DecodedBitStreamParser.cpp中
1
2
3
4
5
6
7
|
c*****t
char
*DecodedBitStreamParser::ASCII =
"ASCII"
;
c*****t
char
*DecodedBitStreamParser::ISO88591 =
"ISO-8859-1"
;
c*****t
char
*DecodedBitStreamParser::UTF8 =
"UTF-8"
;
c*****t
char
*DecodedBitStreamParser::SHIFT_JIS =
"SHIFT_JIS"
;
c*****t
char
*DecodedBitStreamParser::EUC_JP =
"EUC-JP"
;
c*****t
char
*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*****t
char
*
DecodedBitStreamParser::guessEncoding(unsigned
char
*bytes,
int
length) {
c*****t
bool
ASSUME_SHIFT_JIS =
false
;
char
c*****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) {
return
UTF8;
}
// 添加
// GBK stuff -- add by Ren JianSheng at 2012/2/29
int
idx = 0;
for
(idx = 0; idx < length; idx++) {
int
val = 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!"
);
return
GBK;
}
// GBK stuff end
...
}
|
然后重新编译,搞定!
最后附上一个GBK二维码 测试 地址: http://info.ehome.hc360.com/2011/12/270829206343.shtml