【问题标题】:How to generate code_128 barcode for string with Barby如何使用 Barby 为字符串生成 code_128 条形码
【发布时间】:2012-12-06 00:38:58
【问题描述】:

我目前正在使用带有包装器 gem has_barcode 的 Barby gem 来为我拥有的字符串生成条形码:j5xvvcz。

Gemfile gems(主要是为那些不熟悉这个解决方案的人)

#for barcode generation
gem "barby", "~> 0.5.0"
gem "has_barcode", "~> 0.2.0"
gem "rqrcode", "~> 0.4.2" #for qr code support

我的模型中有代码

include HasBarcode

has_barcode :barcode,
            :outputter => :svg,
            :type => :code_93,
            :value => Proc.new { |p| "#{p.number}" }

我将其渲染到屏幕的位置:

如果我尝试生成 qr_code 或 code_93,一切正常,但 code_128 或 code_39 也不起作用,收到 data not valid 消息。 我担心的是 code_93 不会在某些设备中得到认可,因为它似乎没有被广泛采用(从我读到的here 128 将是最好的解决方案)

这似乎是我可能做错的事情,因为代码显然对 code_128 有效,因为我测试了它here

有人知道我的方法可能有什么问题吗?

【问题讨论】:

    标签: ruby-on-rails barcode


    【解决方案1】:

    显然,code_128 的“A”模式不适合带有 Barby 的小型股。

    所以我必须让模式 B 工作。

    当前发布的 gem 版本的 has_barcode 强制模式 'A',所以我添加了一点“home-patch”,同时向 github 添加了一个建议。

    这是我最终的 has_barcode.rb 文件:

    require "rubygems"
    require "i18n"
    require "active_support"
    require "active_support/hash_with_indifferent_access.rb"
    require "active_support/inflector.rb"
    require "barby"
    
    require "has_barcode/configuration"
    
    module HasBarcode
      def self.included(base)
        base.send(:extend, ClassMethods)
      end
    
      module ClassMethods
        def has_barcode(*args)
          options = args.extract_options!
          @@barcode_configurations ||= {}
          @@barcode_configurations[args.first] = HasBarcode::Configuration.new(options)
    
          define_method args.first do
            if options[:type] == :code_128
              @@barcode_configurations[args.first].barcode_class.new(options[:value].call(self), options[:code128])
            else
              @@barcode_configurations[args.first].barcode_class.new(options[:value].call(self))
            end
          end
    
          define_method "#{args.first}_data" do |opts|
            if opts
              send(args.first).send("to_#{options[:outputter]}", opts)
            else
              send(args.first).send("to_#{options[:outputter]}")
            end
          end
    
        end
    
        def barcode_configurations
          @@barcode_configurations
        end
      end
    
    end
    

    我的模特:

    has_barcode :barcode,
                  outputter: :svg,
                  type: :code_128,
                  code128: 'B',
                  value: Proc.new { |p| "#{p.number}" }
    

    我的看法:

    <%= my_model.barcode_data(xdim:2, height:60).html_safe %>
    

    请注意,在当前日期 (2012-12-05),当前 gem 未更新为允许传递 xdim 和 height 等输出器参数的最新更改。

    这个答案是基于最新的代码更新(问题相关)和我自己的建议,可以找到here

    虽然这个解决方案没有嵌入到 has_barcode gem 中,但我将直接使用 Barby:

    在我的模型中添加:

    def get_barcode(number)
         require 'barby'
         require 'barby/barcode/qr_code'
         require 'barby/outputter/svg_outputter'
    
         barcode = Barby::Code128B.new("#{number}")
    
         barcode.to_svg(xdim:2, height:60)
      end
    

    在视图中:

    <%= my_model.get_barcode(my_model.number).html_safe %>
    

    【讨论】:

    • 这显示条形码,我想在条形码下方显示人类可读的文本,我该怎么做?
    猜你喜欢
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2015-04-17
    • 1970-01-01
    相关资源
    最近更新 更多