Skip to content

Commit

Permalink
Conversion of data into bit stream in one function
Browse files Browse the repository at this point in the history
  • Loading branch information
eolo999 committed May 1, 2011
1 parent fe83546 commit 19b1245
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 102 deletions.
42 changes: 4 additions & 38 deletions qrcode/qrcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
from qrdraw import make_array

from qrutils import (
convert_8bit,
convert_alphanumeric,
convert_numeric,
convert,
data_codewords_per_block,
determine_datatype,
determine_symbol_version,
Expand Down Expand Up @@ -70,7 +68,7 @@ def encode(self):
def save_image(self, path=None):
"""Saves the QR Code Symbol to the given 'path'."""
self.symbol_array = make_array(self)
image_path = make_image(self.symbol_array, path=path, zoom=5)
image_path, image = make_image(self.symbol_array, path=path, zoom=5)
return image_path


Expand Down Expand Up @@ -152,47 +150,15 @@ def _convert_input_string(self):
rules for the mode in force, as defined in ISO/IEC 18004 8.4.1 to
8.4.5.
"""
if self.data_mode == 'numeric':
self.code = "".join([self._insert_indicators(),
convert_numeric(self.input_string)])
assert self._validate_numeric_bitstream_length()
elif self.data_mode == 'alphanumeric':
self.code = "".join([self._insert_indicators(),
convert_alphanumeric(self.input_string)])
assert self._validate_alphanumeric_bitstream_length()
elif self.data_mode == '8bit':
self.code = "".join([self._insert_indicators(),
convert_8bit(self.input_string)])
self.code = "".join([self._insert_indicators(),
convert(self.input_string, self.data_mode)])

def _insert_indicators(self):
mode_bits = mode_indicators(self.data_mode)
indicators = "".join([mode_bits, to_binstring(
len(self.input_string), self.count_bits)])
return indicators

def _validate_numeric_bitstream_length(self):
r = len(self.input_string) % 3
if r != 0:
r = r * 3 + 1
return len(self.code) == (
4 + self.count_bits +
10 * (len(self.input_string) / 3) + r)

def _validate_alphanumeric_bitstream_length(self):
"""
B = 4 + C + 11(D DIV 2) + 6(D MOD 2)
where:
B = number of bits in bit stream
C = number of bits in Character Count Indicator ( from Table 3)
D = number of input data characters
first and last 4 are respectively for data mode indicator and
terminator sequence
"""
B = len(self.code)
C = self.count_bits
D = len(self.input_string)
return B == 4 + C + 11 * (D / 2) + 6 * (D % 2)


def _main():
global num, alnum
Expand Down
74 changes: 34 additions & 40 deletions qrcode/qrutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,46 +197,40 @@ def pad(bit_string, length):
return "".join([bit_string, '0' * zeroes])


def convert_numeric(input):
"""ISO/IEC 18004 8.4.2: The input data string is divided into groups of
three digits, and each group is converted to its 10 bit binary equivalent.
If the number of input digits is not an exact multiple of three, the final
one or two digits are converted to 4 or 7 bits respectively.
Returns the input respresentation as a bit string.
"""
splitted_input = split_numeric_input(input)
data_bit_stream = ''
for input in splitted_input:
data_bit_stream += to_binstring(int(input), (len(input) * 3) + 1)
return data_bit_stream


def convert_alphanumeric(input):
"""ISO/IEC 18004 8.4.3: Input data characters are divided into groups of
two characters which are encoded to 11-bit binary codes. The character
value of the first character is multiplied by 45 and the character value
of the second digit is added to the product. The sum is then converted to
an 11 bit binary number. If the number of input data characters is not a
multiple of two, the character value of the final character is encoded to
a 6-bit binary number.
"""
input = alphanumeric_codes(input)
splitted_input = split_alphanumeric_input(input)
data_bit_stream = ''
for input in splitted_input:
if len(input) == 2:
data_bit_stream += to_binstring(input[0] * 45 + input[1], 11)
else:
data_bit_stream += to_binstring(input[0], 6)
return data_bit_stream


def convert_8bit(input):
data_bit_stream = ''
for ch in input:
data_bit_stream += to_binstring(ord(ch), 8)
return data_bit_stream
def convert(input, data_mode):
if data_mode == 'numeric':
#: ISO/IEC 18004 8.4.2: The input data string is divided into groups of
# three digits, and each group is converted to its 10 bit binary equivalent.
# If the number of input digits is not an exact multiple of three, the final
# one or two digits are converted to 4 or 7 bits respectively.
# Returns the input respresentation as a bit string.
splitted_input = split_numeric_input(input)
data_bit_stream = ''
for input in splitted_input:
data_bit_stream += to_binstring(int(input), (len(input) * 3) + 1)
return data_bit_stream
#: """ISO/IEC 18004 8.4.3: Input data characters are divided into groups of
# two characters which are encoded to 11-bit binary codes. The character
# value of the first character is multiplied by 45 and the character value
# of the second digit is added to the product. The sum is then converted to
# an 11 bit binary number. If the number of input data characters is not a
# multiple of two, the character value of the final character is encoded to
# a 6-bit binary number.
elif data_mode == 'alphanumeric':
input = alphanumeric_codes(input)
splitted_input = split_alphanumeric_input(input)
data_bit_stream = ''
for input in splitted_input:
if len(input) == 2:
data_bit_stream += to_binstring(input[0] * 45 + input[1], 11)
else:
data_bit_stream += to_binstring(input[0], 6)
return data_bit_stream
elif data_mode == '8bit':
data_bit_stream = ''
for ch in input:
data_bit_stream += to_binstring(ord(ch), 8)
return data_bit_stream

def list_to_bin(coefficients_list):
"""Given a list of codewords represented as integers it returns a
Expand Down
22 changes: 0 additions & 22 deletions qrcode/test/test_draw.py

This file was deleted.

4 changes: 2 additions & 2 deletions qrcode/test/test_qrutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def test_split_alphanumeric_mode():
[23]])
assert(split_numeric_input('') == [])

def test_convert_numeric():
assert(convert_numeric('01234567') == '000000110001010110011000011')
def test_convert():
assert(convert('01234567', 'numeric') == '000000110001010110011000011')

def test_pad():
assert pad('0101', 5) == '01010'
Expand Down

0 comments on commit 19b1245

Please sign in to comment.