def setupTypeInfo(self, test, maskPattern): data = (self.errorCorrectLevel << 3) | maskPattern bits = QRUtil.getBCHTypeInfo(data) # vertical for i in range(15): mod = (not test and ((bits >> i) & 1) == 1) if (i < 6): self.modules[i][8] = mod elif (i < 8): self.modules[i + 1][8] = mod else: self.modules[self.moduleCount - 15 + i][8] = mod # horizontal for i in range(15): mod = (not test and ((bits >> i) & 1) == 1) if (i < 8): self.modules[8][self.moduleCount - i - 1] = mod elif (i < 9): self.modules[8][15 - i - 1 + 1] = mod else: self.modules[8][15 - i - 1] = mod # fixed module self.modules[self.moduleCount - 8][8] = (not test)
def setupTypeNumber(self, test): bits = QRUtil.getBCHTypeNumber(self.typeNumber) for i in range(18): mod = (not test and ((bits >> i) & 1) == 1) self.modules[i // 3][i % 3 + self.moduleCount - 8 - 3] = mod for i in range(18): mod = (not test and ((bits >> i) & 1) == 1) self.modules[i % 3 + self.moduleCount - 8 - 3][i // 3] = mod
def createBytes(buffer, rsBlocks): offset = 0 maxDcCount = 0 maxEcCount = 0 dcdata = [0 for x in range(len(rsBlocks))] ecdata = [0 for x in range(len(rsBlocks))] for r in range(len(rsBlocks)): dcCount = rsBlocks[r].dataCount ecCount = rsBlocks[r].totalCount - dcCount maxDcCount = max(maxDcCount, dcCount) maxEcCount = max(maxEcCount, ecCount) dcdata[r] = [0 for x in range(dcCount)] for i in range(len(dcdata[r])): dcdata[r][i] = 0xff & buffer.buffer[i + offset] offset += dcCount rsPoly = QRUtil.getErrorCorrectPolynomial(ecCount) rawPoly = QRPolynomial(dcdata[r], rsPoly.getLength() - 1) modPoly = rawPoly.mod(rsPoly) ecdata[r] = [0 for x in range(rsPoly.getLength() - 1)] for i in range(len(ecdata[r])): modIndex = i + modPoly.getLength() - len(ecdata[r]) if (modIndex >= 0): ecdata[r][i] = modPoly.get(modIndex) else: ecdata[r][i] = 0 totalCodeCount = 0 for i in range(len(rsBlocks)): totalCodeCount += rsBlocks[i].totalCount data = [None for x in range(totalCodeCount)] index = 0 for i in range(maxDcCount): for r in range(len(rsBlocks)): if (i < len(dcdata[r])): data[index] = dcdata[r][i] index += 1 for i in range(maxEcCount): for r in range(len(rsBlocks)): if (i < len(ecdata[r])): data[index] = ecdata[r][i] index += 1 return data
def getBestMaskPattern(self): minLostPoint = 0 pattern = 0 for i in range(8): self.makeImpl(True, i) lostPoint = QRUtil.getLostPoint(self) if (i == 0 or minLostPoint > lostPoint): minLostPoint = lostPoint pattern = i return pattern
def createData(typeNumber, errorCorrectLevel, dataList): rsBlocks = QRRSBlock.getRSBlocks(typeNumber, errorCorrectLevel) buffer = QRBitBuffer() for i in range(len(dataList)): data = dataList[i] buffer.put(data.mode, 4) buffer.put(data.getLength(), QRUtil.getLengthInBits(data.mode, typeNumber)) data.write(buffer) # calc num max data. totalDataCount = 0 for i in range(len(rsBlocks)): totalDataCount += rsBlocks[i].dataCount if (buffer.getLengthInBits() > totalDataCount * 8): raise DataOverflowError("Code length overflow. Data size (%s) > " "size available (%s)" % (buffer.getLengthInBits(), totalDataCount * 8)) # end code if (buffer.getLengthInBits() + 4 <= totalDataCount * 8): buffer.put(0, 4) # padding while (buffer.getLengthInBits() % 8 != 0): buffer.putBit(False) # padding while True: if (buffer.getLengthInBits() >= totalDataCount * 8): break buffer.put(QRCode.PAD0, 8) if (buffer.getLengthInBits() >= totalDataCount * 8): break buffer.put(QRCode.PAD1, 8) return QRCode.createBytes(buffer, rsBlocks)
def mapData(self, data, maskPattern): inc = -1 row = self.moduleCount - 1 bitIndex = 7 byteIndex = 0 for col in range(self.moduleCount - 1, 0, -2): if (col == 6): col -= 1 while (True): for c in range(2): if (self.modules[row][col - c] == None): dark = False if (byteIndex < len(data)): dark = (((data[byteIndex] >> bitIndex) & 1) == 1) mask = QRUtil.getMask(maskPattern, row, col - c) if (mask): dark = not dark self.modules[row][col - c] = dark bitIndex -= 1 if (bitIndex == -1): byteIndex += 1 bitIndex = 7 row += inc if (row < 0 or self.moduleCount <= row): row -= inc inc = -inc break
def setupPositionAdjustPattern(self): pos = QRUtil.getPatternPosition(self.typeNumber) for i in range(len(pos)): for j in range(len(pos)): row = pos[i] col = pos[j] if (self.modules[row][col] != None): continue for r in range(-2, 3): for c in range(-2, 3): if (r == -2 or r == 2 or c == -2 or c == 2 or (r == 0 and c == 0)): self.modules[row + r][col + c] = True else: self.modules[row + r][col + c] = False