def decode_frames(barcode_width, barcode_height, input_directory='.',
                  path_to_zxing='zxing-read-only'):
  """Decodes the barcodes overlaid in each frame.

  The function uses the example Java command-line tool from the Zxing
  distribution to decode the barcode in every PNG frame from the input
  directory. The frames should be named frame_xxxx.png, where xxxx is the frame
  number. The frame numbers should be consecutive and should start from 0001.
  The decoding results in a frame_xxxx.txt file for every successfully decoded
  barcode. This file contains the decoded barcode as 12-digit string (UPC-A
  format: 11 digits content + one check digit).

  Args:
    barcode_width(int): Width of the barcode.
    barcode_height(int): Height of the barcode.
    input_directory(string): The input directory from where the PNG frames are
      read.
    path_to_zxing(string): The path to Zxing.
  Return:
    (bool): True if the decoding went without errors.
  """
  jars = helper_functions.form_jars_string(path_to_zxing)
  command_line_decoder ='com.google.zxing.client.j2se.CommandLineRunner'
  return helper_functions.perform_action_on_all_files(
      directory=input_directory, file_pattern='frame_',
      file_extension='png', start_number=1, action=_decode_barcode_in_file,
      barcode_width=barcode_width, barcode_height=barcode_height, jars=jars,
      command_line_decoder=command_line_decoder)
Example #2
0
def decode_frames(barcode_width,
                  barcode_height,
                  input_directory='.',
                  path_to_zxing='zxing-read-only'):
    """Decodes the barcodes overlaid in each frame.

  The function uses the example Java command-line tool from the Zxing
  distribution to decode the barcode in every PNG frame from the input
  directory. The frames should be named frame_xxxx.png, where xxxx is the frame
  number. The frame numbers should be consecutive and should start from 0001.
  The decoding results in a frame_xxxx.txt file for every successfully decoded
  barcode. This file contains the decoded barcode as 12-digit string (UPC-A
  format: 11 digits content + one check digit).

  Args:
    barcode_width(int): Width of the barcode.
    barcode_height(int): Height of the barcode.
    input_directory(string): The input directory from where the PNG frames are
      read.
    path_to_zxing(string): The path to Zxing.
  Return:
    (bool): True if the decoding went without errors.
  """
    jars = helper_functions.form_jars_string(path_to_zxing)
    command_line_decoder = 'com.google.zxing.client.j2se.CommandLineRunner'
    return helper_functions.perform_action_on_all_files(
        directory=input_directory,
        file_pattern='frame_',
        file_extension='png',
        start_number=1,
        action=_decode_barcode_in_file,
        barcode_width=barcode_width,
        barcode_height=barcode_height,
        jars=jars,
        command_line_decoder=command_line_decoder)
Example #3
0
def generate_upca_barcodes(number_of_barcodes,
                           barcode_width,
                           barcode_height,
                           output_directory='.',
                           path_to_zxing='zxing-read-only'):
    """Generates UPC-A barcodes.

  This function generates a number_of_barcodes UPC-A barcodes. The function
  calls an example Java encoder from the Zxing library. The barcodes are
  generated as PNG images. The width of the barcodes shouldn't be less than 102
  pixels because otherwise Zxing can't properly generate the barcodes.

  Args:
    number_of_barcodes(int): The number of barcodes to generate.
    barcode_width(int): Width of barcode in pixels.
    barcode_height(int): Height of barcode in pixels.
    output_directory(string): Output directory where to store generated
      barcodes.
    path_to_zxing(string): The path to Zxing.

  Return:
    (bool): True if the conversion is successful.
  """
    base_file_name = os.path.join(output_directory, "barcode_")
    jars = helper_functions.form_jars_string(path_to_zxing)
    command_line_encoder = 'com.google.zxing.client.j2se.CommandLineEncoder'
    barcode_width = str(barcode_width)
    barcode_height = str(barcode_height)

    errors = False
    for i in range(number_of_barcodes):
        suffix = helper_functions.zero_pad(i)
        # Barcodes starting from 0
        content = helper_functions.zero_pad(i, 11)
        output_file_name = base_file_name + suffix + ".png"

        command = [
            "java", "-cp", jars, command_line_encoder,
            "--barcode_format=UPC_A",
            "--height=%s" % barcode_height,
            "--width=%s" % barcode_width,
            "--output=%s" % (output_file_name),
            "%s" % (content)
        ]
        try:
            helper_functions.run_shell_command(
                command, msg=('Error during barcode %s generation' % content))
        except helper_functions.HelperError, err:
            print err
            errors = True
Example #4
0
def generate_upca_barcodes(number_of_barcodes, barcode_width, barcode_height,
                           output_directory='.',
                           path_to_zxing='zxing-read-only'):
  """Generates UPC-A barcodes.

  This function generates a number_of_barcodes UPC-A barcodes. The function
  calls an example Java encoder from the Zxing library. The barcodes are
  generated as PNG images. The width of the barcodes shouldn't be less than 102
  pixels because otherwise Zxing can't properly generate the barcodes.

  Args:
    number_of_barcodes(int): The number of barcodes to generate.
    barcode_width(int): Width of barcode in pixels.
    barcode_height(int): Height of barcode in pixels.
    output_directory(string): Output directory where to store generated
      barcodes.
    path_to_zxing(string): The path to Zxing.

  Return:
    (bool): True if the conversion is successful.
  """
  base_file_name = os.path.join(output_directory, "barcode_")
  jars = helper_functions.form_jars_string(path_to_zxing)
  command_line_encoder ='com.google.zxing.client.j2se.CommandLineEncoder'
  barcode_width = str(barcode_width)
  barcode_height = str(barcode_height)

  errors = False
  for i in range(number_of_barcodes):
    suffix = helper_functions.zero_pad(i)
    # Barcodes starting from 0
    content = helper_functions.zero_pad(i, 11)
    output_file_name = base_file_name + suffix + ".png"

    command = ["java", "-cp", jars, command_line_encoder,
               "--barcode_format=UPC_A", "--height=%s" % barcode_height,
               "--width=%s" % barcode_width,
               "--output=%s" % (output_file_name), "%s" % (content)]
    try:
      helper_functions.run_shell_command(
          command, msg=('Error during barcode %s generation' % content))
    except helper_functions.HelperError, err:
      print >> sys.stderr, err
      errors = True