def encodeIn8BitMode(self, content): mode = '8-bit' leng, lml = len(content), self.getLengthMarkLen(mode) data = self.getTagCode(mode) + xbin(leng, lml) for i in content: data += xbin(ord(i), 8) assert len(data) == 4 + lml + 8 * leng return data
def encodeInDigitMode(self, content): mode, leng = 'digit', len(content) gumi, data = leng / 3, '' for i in xrange(gumi): i = i * 3 text = content[i:i + 3] data += xbin(text, 10) rest = leng % 3 if rest > 0: data += xbin(content[-rest:], 3 * rest + 1) # byte counter begin lml = self.getLengthMarkLen(mode) data = xbin(leng, lml) + data data = self.getTagCode(mode) + data assert len(data) == 4 + lml + 10 * gumi + 3 * rest + (1 if rest else 0) return data
def setFormatInfo(self): ''' 设置掩码格式、容错质量等信息 ''' if self.__error or not self.__matrix: return data = xbin(self.__quality, 2) + xbin(self.__mask, 3) bch = BCHCoder(15, 5, '10100110111') data += bch.encode(data) data = strXor(data, '101010000010010') data = map(char2bool, data) for i in xrange(8): self.__matrix[8][i if i < 6 else i + 1] = BBP if data[i] else NBP self.__matrix[self.__size - i - 1][8] = BBP if data[i] else NBP for i in xrange(7): i += 8 self.__matrix[15 - (i if i < 9 else i + 1)][8] = BBP if data[i] else NBP self.__matrix[8][self.__size - 15 + i] = BBP if data[i] else NBP self.__matrix[self.__size - 8][8] = BBP
def translate(char): ochar = char.encode('gbk') char = [ord(ochar[0]), ord(ochar[1])] if 0xA1 <= char[0] <= 0xAA and 0xA1 <= char[1] <= 0xFE: value = (char[0] - 0xA1) * 0x60 + (char[1] - 0xA1) elif 0xB0 <= char[0] <= 0xFA and 0xA1 <= char[1] <= 0xFE: value = (char[0] - 0xA6) * 0x60 + (char[1] - 0xA1) else: value = 64 return xbin(value, 13)
def setVersionInfo(self): """ setVersionInfo """ if self.__error or not self.__matrix: return if self.__version < 7: return data = xbin(self.__version, 6) bch = BCHCoder(18, 6, '1111100100101') chkcode = bch.encode(data) data = map(char2bool, (data + chkcode)[::-1]) startColumn = self.__size - 11 for i in xrange(18): self.__matrix[startColumn + i % 3][i / 3] = BBP if data[i] else NBP self.__matrix[i / 3][startColumn + i % 3] = BBP if data[i] else NBP
def encodeCheckCode(self, data, length): rates = QRDataEncoder.GF.getRates(length) buf = [0] * length ldata = len(data) // 8 for i in xrange(ldata): elem = eval('0b' + data[8 * i:8 * i + 8]) busbuf = buf[-1] ^ elem for j in xrange(length - 1, -1, -1): tmp = QRDataEncoder.GF.mul(busbuf, rates[j]) if j > 0: buf[j] = buf[j - 1] ^ tmp else: buf[j] = tmp return ''.join(map(lambda x: xbin(x, 8), buf[::-1]))
def encodeInAlphaDigitMode(self, content): def getADIndex(char): asc = ord(char) if 0x30 <= asc < 0x39: value = asc - 0x30 elif 0x41 <= asc <= 0x5A: value = asc - 0x37 #asc-0x41+10 else: value = ' $%*+-./'.index(char) + 36 return value mode, leng, content = 'alphadigit', len(content), content.upper() gumi, lml = leng / 2, self.getLengthMarkLen(mode) data = self.getTagCode(mode) + xbin(leng, lml) for i in xrange(gumi): i *= 2 text = content[i:i + 3] value = getADIndex(text[0]) * 45 + getADIndex(text[1]) data += xbin(value, 11) rest = leng % 2 if rest > 0: data += xbin(getADIndex(content[-1]), 6) assert len(data) == 4 + lml + 11 * gumi + 6 * rest return data
def encodeInKanjiMode(self, content): def translate(char): ochar = char.encode('gbk') char = [ord(ochar[0]), ord(ochar[1])] if 0xA1 <= char[0] <= 0xAA and 0xA1 <= char[1] <= 0xFE: value = (char[0] - 0xA1) * 0x60 + (char[1] - 0xA1) elif 0xB0 <= char[0] <= 0xFA and 0xA1 <= char[1] <= 0xFE: value = (char[0] - 0xA6) * 0x60 + (char[1] - 0xA1) else: value = 64 return xbin(value, 13) mode, content = 'kanji', content.decode(dataCoding) leng, lml = len(content), self.getLengthMarkLen(mode) data = self.getTagCode(mode) + '0001' + xbin(leng, lml) for i in content: data += translate(i) assert len(data) == 8 + lml + 13 * leng return data