Coder Social home page Coder Social logo

barcoders's Introduction

Build Status Coverage Status MIT licensed Crates.io Algorithmia

BARCODERS

Barcoders is a barcode-encoding library for the Rust programming language.

Barcoders allows you to encode valid data for a chosen barcode symbology into a Vec<u8> representation of the underlying binary structure. From here, you can take advantage of one of the optional builtin generators (for exporting to SVG, GIF, PNG, etc) or build your own.

Installation

For encode-only functionality (e.g if you just want to translate a String into a Vec<u8> of binary digits):

[dependencies]
barcoders = "2.0.0"

If you want to generate barcodes into a particular format, turn on the appropriate feature(s):

[dependencies]
barcoders = {version = "2.0.0", features = ["image", "ascii", "svg", "json"]}

Each generator is an optional feature so you only need to compile what you want to use. See below for the feature associated to the generation functionality you desire.

Documentation

Documentation and examples are available here.

Current Support

The ultimate goal of Barcoders is to provide encoding support for all major (and many not-so-major) symbologies.

Symbologies

  • EAN-13
    • UPC-A
    • JAN
    • Bookland
  • EAN-8
  • EAN Supplementals
    • EAN-2
    • EAN-5
  • Code11
    • USD-8
  • Code39
  • Code93
  • Code128 (A, B, C)
  • Two-Of-Five
    • Interleaved (ITF)
    • Standard (STF)
  • Codabar
  • More coming!

Generators

  • ASCII (feature: ascii)
  • JSON (feature: json)
  • SVG (feature: svg)
  • PNG (feature: image)
  • GIF (feature: image)
  • WEBP (feature: image)
  • Image Buffer (feature: image)
  • Or add your own

Examples

Encoding

extern crate barcoders;

use barcoders::sym::ean13::*;

// Each encoder accepts a String to be encoded. Valid data is barcode-specific
// and thus constructors return an Result<T, barcoders::error::Error>.
let barcode = EAN13::new("593456661897").unwrap();

// The `encode` method returns a Vec<u8> of the binary representation of the
// generated barcode. This is useful if you want to add your own generator.
let encoded: Vec<u8> = barcode.encode();

Image (GIF, WEBP, PNG) generation

extern crate barcoders;

use barcoders::sym::code39::*;
use barcoders::generators::image::*;
use std::io::prelude::*;
use std::io::BufWriter;
use std::fs::File;
use std::path::Path;

let barcode = Code39::new("1ISTHELONELIESTNUMBER").unwrap();
let png = Image::png(80); // You must specify the height in pixels.
let encoded = barcode.encode();

// Image generators return a Result<Vec<u8>, barcoders::error::Error) of encoded bytes.
let bytes = png.generate(&encoded[..]).unwrap();

// Which you can then save to disk.
let file = File::create(&Path::new("my_barcode.png")).unwrap();
let mut writer = BufWriter::new(file);
writer.write(&bytes[..]).unwrap();

// Generated file ↓ ↓ ↓

Code 39: 1ISTHELONELIESTNUMBER

You can also request an image::RgbaImage, which you can manipulate yourself:

let barcode = Code39::new("BEELZEBUB").unwrap();
let buffer = Image::image_buffer(100);
let encoded = barcode.encode();
let img = buffer.generate_buffer(&encoded[..]).unwrap();

// Manipulate and save the image here...

You may also specify the barcode x-dimension, rotation, background/foreground colors and opacity by specifying the struct fields:

let gif = Image::GIF{height: 80,
                     xdim: 1,
                     rotation: Rotation::Zero,
                     // Using non black/white colors is generally not recommended by most vendors, but barcoders makes it possible.
                     foreground: Color::new([255, 0, 0, 255]),
                     background: Color::new([0, 255, 20, 255])};

SVG generation

SVG is similar to the other image types, but I've supplied it as a separate feature as it doesn't require third-party dependencies.

extern crate barcoders;

use barcoders::sym::code39::*;
use barcoders::generators::svg::*;
use std::io::prelude::*;
use std::io::BufWriter;
use std::fs::File;
use std::path::Path;

let barcode = Code39::new("56DFU4A777H").unwrap();
let svg = SVG::new(200); // You must specify the height in pixels.
let encoded = barcode.encode();
let data: String = svg.generate(&encoded).unwrap();

let file = File::create(&Path::new("my_barcode.svg")).unwrap();
let mut writer = BufWriter::new(file);
writer.write(data.as_bytes()).unwrap();

You may also specify the barcode x-dimension, background/foreground colors and opacity by specifying the struct fields:

let svg = SVG{height: 80,
              xdim: 1,
              // Using non black/white colors is generally not recommended by most vendors, but barcoders makes it possible.
              foreground: Color::black(),
              background: Color::new([0, 255, 20, 255])};

ASCII generation

The ASCII generator is useful for testing purposes.

extern crate barcoders;

use barcoders::sym::ean13::*;
use barcoders::generators::ascii::*;

let barcode = EAN13::new("750103131130").unwrap();
let encoded = barcode.encode();

let ascii = ASCII::new();
ascii.generate(&encoded[..]);

assert_eq!(ascii.unwrap(),
"
# # ##   # #  ###  ##  # #  ### #### # ##  ## # # #    # ##  ## ##  ## #    # ###  # ### #  # #
# # ##   # #  ###  ##  # #  ### #### # ##  ## # # #    # ##  ## ##  ## #    # ###  # ### #  # #
# # ##   # #  ###  ##  # #  ### #### # ##  ## # # #    # ##  ## ##  ## #    # ###  # ### #  # #
# # ##   # #  ###  ##  # #  ### #### # ##  ## # # #    # ##  ## ##  ## #    # ###  # ### #  # #
# # ##   # #  ###  ##  # #  ### #### # ##  ## # # #    # ##  ## ##  ## #    # ###  # ### #  # #
# # ##   # #  ###  ##  # #  ### #### # ##  ## # # #    # ##  ## ##  ## #    # ###  # ### #  # #
# # ##   # #  ###  ##  # #  ### #### # ##  ## # # #    # ##  ## ##  ## #    # ###  # ### #  # #
# # ##   # #  ###  ##  # #  ### #### # ##  ## # # #    # ##  ## ##  ## #    # ###  # ### #  # #
# # ##   # #  ###  ##  # #  ### #### # ##  ## # # #    # ##  ## ##  ## #    # ###  # ### #  # #
# # ##   # #  ###  ##  # #  ### #### # ##  ## # # #    # ##  ## ##  ## #    # ###  # ### #  # #
".trim());

JSON generation

This may be useful for passing encoded data to third-party systems in a conventional format.

extern crate barcoders;

use barcoders::sym::codabar::*;
use barcoders::generators::json::*;

let codabar = Codabar::new("A98B").unwrap();
let json = JSON::new();
let generated = json.generate(&codabar.encode()[..]);

assert_eq!(generated.unwrap(),
"
{
 \"height\": 10,
 \"xdim\": 1,
 \"encoding\": [1,0,1,1,0,0,1,0,0,1,0,1,1,0,1,0,0,1,0,1,0,1,0,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0,1,1]
}
"

Tests

Note, if you want to output actual image/svg files to the filesystem for visual confirmation, set the WRITE_TO_FILE variable in the appropriate test modules.

Full suite:

$ cargo test --features="image svg ascii json"

Encoding only:

$ cargo test

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

barcoders's People

Contributors

adam-alj avatar archer884 avatar atomicgamer9523 avatar bbirec avatar buntine avatar chrisp60 avatar citizen-stig avatar danue1 avatar emberian avatar janriemer avatar jrmuizel avatar justin-formlogic avatar killercup avatar vam-jam avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

barcoders's Issues

Plans for decoding?

I ran across barcoders while searching for open source libraries that could be adapted to scanning barcodes using a smart phone. Do you have any plans for adding decoding support from images that a phone can take?

Code set escapes as data and GS1-128 support for Code-128

Hello. I hope this is the correct location to ask some questions. I read the barcoders' documentation on doc.rs but could not any mention of how to specify FNC1 as part of the input to Code128::new. Is it possible currently?

On a slightly related note, is it possible to specifyU+C0 as data to be encoded rather than an encoding flag?

docs: `Code128` documentation and example improvements

I feel describing the character sets for Code128 may help other users deal with issues relating to its API.

source

128A (Code Set A) – ASCII characters 00 to 95 (0–9, A–Z and control codes), special characters, and FNC 1–4
128B (Code Set B) – ASCII characters 32 to 127 (0–9, A–Z, a–z), special characters, and FNC 1–4
128C (Code Set C) – 00–99 (encodes two digits with a single code point) and FNC1

I would be happy to make a PR with an excerpt added to the docs and possibly a more descriptive error message.

This is definitely not the responsibility of the library to educate people on the character sets, but also I do not think it would hurt to include.

possibly tangentially related to #20.

Add PDF output

I think it could be worthwhile having a PDF output. I am looking at possibly writing a library for generating PDF output and I think barcoders could be a good first target (as it's simple, and useful). I wouldn't mind writing it, but I won't be able to in the near future (late summer is the earliest). I just wanted to open an issue to remind myself.

Does this sound fine to you?

Example code outputting to an image::RgbImage

It would be useful to be able to output the barcode to a image::RgbImage variable.

I assume that this is quite easy to do, but it would be easier to work out if there is an example that shows the operation.

Invalid EAN13 image generation?

I have a barcode 2000926398005. I tried to generate an image from it

Same as example

let barcode = EAN13::new("2000926398005").unwrap();
let png = Image::png(50);
let encoded = barcode.encode();

let bytes = png.generate(&encoded[..]).unwrap();

let file = File::create(&Path::new("my_barcode.png")).unwrap();
let mut writer = BufWriter::new(file);
writer.write(&bytes[..]).unwrap();

Expected image:
2000926398005
Last - 1 fat, 2 thin

Barcoders generated image:
my_barcode
Last - 2 fat, 3 thin

Barcoders generated images is not scannable

Serialize arbitrary ASCII string into Code128 barcode?

Currently Code128::new constructor expects valid Code128 byte data, where user must manually specify all the control codes to switch character sets as appropriate.

As a user, I'd rather leverage this conversion onto the library itself. I.e., I expect that I could pass any ASCII string to the constructor, and let it do the magic it needs. Unfortunately, I don't see any such functionality in this crate.

Alternatives

  • JsBarcode has auto-detection mode for Code128 input string.

Release new version

Since image dependency has been updated, it would've been good to release new version.

Also, if package is considered stable and there won't be major API changes, it can be 1.* version.

Hosted service

I spotted barcoders on reddit and thought it looked solid, so I quickly hacked together a hosted version of it and hosted it on our startup's platform here:
Algorithmia

If you're interested in owning/maintaining it on our platform, I'm happy to help make that happen. Otherwise, I just thought I'd share another way in which your work is being highlighted, and I'll keep an eye out for updates that we can take advantage of. Feel free to close this issue. :-)

Relicense under dual MIT/Apache-2.0

This issue was automatically generated. Feel free to close without ceremony if
you do not agree with re-licensing or if it is not possible for other reasons.
Respond to @cmr with any questions or concerns, or pop over to
#rust-offtopic on IRC to discuss.

You're receiving this because someone (perhaps the project maintainer)
published a crates.io package with the license as "MIT" xor "Apache-2.0" and
the repository field pointing here.

TL;DR the Rust ecosystem is largely Apache-2.0. Being available under that
license is good for interoperation. The MIT license as an add-on can be nice
for GPLv2 projects to use your code.

Why?

The MIT license requires reproducing countless copies of the same copyright
header with different names in the copyright field, for every MIT library in
use. The Apache license does not have this drawback. However, this is not the
primary motivation for me creating these issues. The Apache license also has
protections from patent trolls and an explicit contribution licensing clause.
However, the Apache license is incompatible with GPLv2. This is why Rust is
dual-licensed as MIT/Apache (the "primary" license being Apache, MIT only for
GPLv2 compat), and doing so would be wise for this project. This also makes
this crate suitable for inclusion and unrestricted sharing in the Rust
standard distribution and other projects using dual MIT/Apache, such as my
personal ulterior motive, the Robigalia project.

Some ask, "Does this really apply to binary redistributions? Does MIT really
require reproducing the whole thing?" I'm not a lawyer, and I can't give legal
advice, but some Google Android apps include open source attributions using
this interpretation. Others also agree with
it
.
But, again, the copyright notice redistribution is not the primary motivation
for the dual-licensing. It's stronger protections to licensees and better
interoperation with the wider Rust ecosystem.

How?

To do this, get explicit approval from each contributor of copyrightable work
(as not all contributions qualify for copyright, due to not being a "creative
work", e.g. a typo fix) and then add the following to your README:

## License

Licensed under either of

 * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
 * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)

at your option.

### Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any
additional terms or conditions.

and in your license headers, if you have them, use the following boilerplate
(based on that used in Rust):

// Copyright 2016 barcoders developers
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

It's commonly asked whether license headers are required. I'm not comfortable
making an official recommendation either way, but the Apache license
recommends it in their appendix on how to use the license.

Be sure to add the relevant LICENSE-{MIT,APACHE} files. You can copy these
from the Rust repo for a plain-text
version.

And don't forget to update the license metadata in your Cargo.toml to:

license = "MIT/Apache-2.0"

I'll be going through projects which agree to be relicensed and have approval
by the necessary contributors and doing this changes, so feel free to leave
the heavy lifting to me!

Contributor checkoff

To agree to relicensing, comment with :

I license past and future contributions under the dual MIT/Apache-2.0 license, allowing licensees to chose either at their option.

Or, if you're a contributor, you can check the box in this repo next to your
name. My scripts will pick this exact phrase up and check your checkbox, but
I'll come through and manually review this issue later as well.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.