简体   繁体   English

生成带编号的 UPC 条码和导轨

[英]Generate UPC Barcode with Number with rails

I'm able to generate barcode by using "Barby" gem with "EAN13 & UPCA" But I need to show numbers with the barcode and I have no idea, what I'm missing in code.我可以通过使用带有“EAN13 & UPCA”的“Barby” gem 来生成条形码,但我需要用条形码显示数字,但我不知道代码中缺少什么。

Current Generated Barcode当前生成的条码

Read out the barby gem documentation but no luck.阅读 barby gem 文档,但没有运气。

  @barcode_value = "123456789123"  
  full_path = "public/Barcodes/"+@barcode_value+".png"
  barcode = Barby::EAN13.new(@barcode_value)
  File.open(full_path, 'wb') { |f| f.write barcode.to_png(:margin => 3, :xdim => 2, :height => 50) }

Generate Barcode with Number.用数字生成条码。

Required Barcode所需条码

The code you generated is a Code 39. The barcode you marked as required is a Code EAN 13, not UPC (those are not identical).您生成的代码是 Code 39。您标记为必需的条形码是 Code EAN 13,而不是 UPC(它们不相同)。 One way to create such a code with Ruby would be using an API, eg https://mcapi.io/barcode/barcode-api-ruby.php使用 Ruby 创建此类代码的一种方法是使用 API,例如https://mcapi.io/barcode/barcode-api-ruby.php

The API is hosted on RapidAPI; API 托管在 RapidAPI 上; sample call to create your barcode:创建条形码的示例调用:

# Ruby
  
require 'uri'
require 'net/http'
require 'openssl'

url = URI("https://mcapi-barcode.p.rapidapi.com/")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request = Net::HTTP::Post.new(url)
request["content-type"] = 'application/json'
request["x-rapidapi-key"] = 'YOUR_API_KEY'
request["x-rapidapi-host"] = 'mcapi-barcode.p.rapidapi.com'
request.body = "{
    \"data\": \"9501101530003\",
    \"type\": 1,
    \"size\": 2,
    \"format\": \"pdf\"
}"

response = http.request(request)

The returned code will be in response.read_body as a base64 encoded PDF (posted as a screenshot since SO doesn't display PDFs):返回的代码将在 response.read_body 中作为 base64 编码的 PDF(作为屏幕截图发布,因为 SO 不显示 PDF):

EAN13 created with Ruby用 Ruby 创建的 EAN13

(Disclaimer: We are the developers.) (免责声明:我们是开发人员。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM