Compare commits
21 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
914f64a08d | ||
|
c575a3c357 | ||
|
ca3e24f327 | ||
|
d79eb8727b | ||
|
d5743d95af | ||
|
49d4ce8a5b | ||
|
a8e67c5d16 | ||
|
b1129f9d8b | ||
|
1c8224977e | ||
|
2c9211e1ea | ||
|
30509e0dd7 | ||
|
e966fa7106 | ||
|
87ab0677e3 | ||
|
f3630e1cd4 | ||
|
e8e52a74a7 | ||
|
83789dfea1 | ||
|
d6f8bd41de | ||
|
65580ac6e3 | ||
|
abf40d274d | ||
|
608a8ad611 | ||
|
196dd6e4c1 |
@ -27,8 +27,8 @@ import (
|
||||
"image/png"
|
||||
"os"
|
||||
|
||||
"github.com/boombuler/barcode"
|
||||
"github.com/boombuler/barcode/qr"
|
||||
"git.bbr-dev.info/brajkovic/barcode"
|
||||
"git.bbr-dev.info/brajkovic/barcode/qr"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@ -48,6 +48,6 @@ func main() {
|
||||
```
|
||||
|
||||
## Documentation ##
|
||||
See [GoDoc](https://godoc.org/github.com/boombuler/barcode)
|
||||
See [GoDoc](https://godoc.org/git.bbr-dev.info/brajkovic/barcode)
|
||||
|
||||
To create a barcode use the Encode function from one of the subpackages.
|
||||
|
@ -5,18 +5,19 @@ import (
|
||||
"image"
|
||||
"image/color"
|
||||
|
||||
"github.com/boombuler/barcode"
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"git.bbr-dev.info/brajkovic/barcode"
|
||||
"git.bbr-dev.info/brajkovic/barcode/utils"
|
||||
)
|
||||
|
||||
type aztecCode struct {
|
||||
*utils.BitList
|
||||
size int
|
||||
content []byte
|
||||
color barcode.ColorScheme
|
||||
}
|
||||
|
||||
func newAztecCode(size int) *aztecCode {
|
||||
return &aztecCode{utils.NewBitList(size * size), size, nil}
|
||||
func newAztecCode(size int, color barcode.ColorScheme) *aztecCode {
|
||||
return &aztecCode{utils.NewBitList(size * size), size, nil, barcode.ColorScheme16}
|
||||
}
|
||||
|
||||
func (c *aztecCode) Content() string {
|
||||
@ -28,7 +29,11 @@ func (c *aztecCode) Metadata() barcode.Metadata {
|
||||
}
|
||||
|
||||
func (c *aztecCode) ColorModel() color.Model {
|
||||
return color.Gray16Model
|
||||
return c.color.Model
|
||||
}
|
||||
|
||||
func (c *aztecCode) ColorScheme() barcode.ColorScheme {
|
||||
return c.color
|
||||
}
|
||||
|
||||
func (c *aztecCode) Bounds() image.Rectangle {
|
||||
@ -37,9 +42,9 @@ func (c *aztecCode) Bounds() image.Rectangle {
|
||||
|
||||
func (c *aztecCode) At(x, y int) color.Color {
|
||||
if c.GetBit(x*c.size + y) {
|
||||
return color.Black
|
||||
return c.color.Foreground
|
||||
}
|
||||
return color.White
|
||||
return c.color.Background
|
||||
}
|
||||
|
||||
func (c *aztecCode) set(x, y int) {
|
||||
|
@ -4,8 +4,8 @@ package aztec
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/boombuler/barcode"
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"git.bbr-dev.info/brajkovic/barcode"
|
||||
"git.bbr-dev.info/brajkovic/barcode/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -124,6 +124,11 @@ func drawBullsEye(matrix *aztecCode, center, size int) {
|
||||
|
||||
// Encode returns an aztec barcode with the given content
|
||||
func Encode(data []byte, minECCPercent int, userSpecifiedLayers int) (barcode.Barcode, error) {
|
||||
return EncodeWithColor(data, minECCPercent, userSpecifiedLayers, barcode.ColorScheme16)
|
||||
}
|
||||
|
||||
// Encode returns an aztec barcode with the given content and color scheme
|
||||
func EncodeWithColor(data []byte, minECCPercent int, userSpecifiedLayers int, color barcode.ColorScheme) (barcode.Barcode, error) {
|
||||
bits := highlevelEncode(data)
|
||||
eccBits := ((bits.Len() * minECCPercent) / 100) + 11
|
||||
totalSizeBits := bits.Len() + eccBits
|
||||
@ -215,7 +220,7 @@ func Encode(data []byte, minECCPercent int, userSpecifiedLayers int) (barcode.Ba
|
||||
alignmentMap[origCenter+i] = center + newOffset + 1
|
||||
}
|
||||
}
|
||||
code := newAztecCode(matrixSize)
|
||||
code := newAztecCode(matrixSize, color)
|
||||
code.content = data
|
||||
|
||||
// draw data bits
|
||||
|
@ -4,7 +4,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"git.bbr-dev.info/brajkovic/barcode/utils"
|
||||
)
|
||||
|
||||
func Test_StuffBits(t *testing.T) {
|
||||
|
@ -1,7 +1,7 @@
|
||||
package aztec
|
||||
|
||||
import (
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"git.bbr-dev.info/brajkovic/barcode/utils"
|
||||
)
|
||||
|
||||
func bitsToWords(stuffedBits *utils.BitList, wordSize int, wordCount int) []int {
|
||||
|
@ -1,7 +1,7 @@
|
||||
package aztec
|
||||
|
||||
import (
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"git.bbr-dev.info/brajkovic/barcode/utils"
|
||||
)
|
||||
|
||||
func highlevelEncode(data []byte) *utils.BitList {
|
||||
|
@ -5,7 +5,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"git.bbr-dev.info/brajkovic/barcode/utils"
|
||||
)
|
||||
|
||||
func bitStr(bl *utils.BitList) string {
|
||||
|
@ -3,7 +3,7 @@ package aztec
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"git.bbr-dev.info/brajkovic/barcode/utils"
|
||||
)
|
||||
|
||||
type encodingMode byte
|
||||
|
@ -3,7 +3,7 @@ package aztec
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"git.bbr-dev.info/brajkovic/barcode/utils"
|
||||
)
|
||||
|
||||
type token interface {
|
||||
|
@ -1,6 +1,8 @@
|
||||
package barcode
|
||||
|
||||
import "image"
|
||||
import (
|
||||
"image"
|
||||
)
|
||||
|
||||
const (
|
||||
TypeAztec = "Aztec"
|
||||
@ -40,3 +42,7 @@ type BarcodeIntCS interface {
|
||||
Barcode
|
||||
CheckSum() int
|
||||
}
|
||||
|
||||
type BarcodeColor interface {
|
||||
ColorScheme() ColorScheme
|
||||
}
|
||||
|
@ -5,8 +5,8 @@ import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
|
||||
"github.com/boombuler/barcode"
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"git.bbr-dev.info/brajkovic/barcode"
|
||||
"git.bbr-dev.info/brajkovic/barcode/utils"
|
||||
)
|
||||
|
||||
var encodingTable = map[rune][]bool{
|
||||
@ -32,8 +32,8 @@ var encodingTable = map[rune][]bool{
|
||||
'D': []bool{true, false, true, false, false, true, true, false, false, true},
|
||||
}
|
||||
|
||||
// Encode creates a codabar barcode for the given content
|
||||
func Encode(content string) (barcode.Barcode, error) {
|
||||
// Encode creates a codabar barcode for the given content and color scheme
|
||||
func EncodeWithColor(content string, color barcode.ColorScheme) (barcode.Barcode, error) {
|
||||
checkValid, _ := regexp.Compile(`[ABCD][0123456789\-\$\:/\.\+]*[ABCD]$`)
|
||||
if content == "!" || checkValid.ReplaceAllString(content, "!") != "!" {
|
||||
return nil, fmt.Errorf("can not encode \"%s\"", content)
|
||||
@ -45,5 +45,10 @@ func Encode(content string) (barcode.Barcode, error) {
|
||||
}
|
||||
resBits.AddBit(encodingTable[r]...)
|
||||
}
|
||||
return utils.New1DCode(barcode.TypeCodabar, content, resBits), nil
|
||||
return utils.New1DCodeWithColor(barcode.TypeCodabar, content, resBits, color), nil
|
||||
}
|
||||
|
||||
// Encode creates a codabar barcode for the given content
|
||||
func Encode(content string) (barcode.Barcode, error) {
|
||||
return EncodeWithColor(content, barcode.ColorScheme16)
|
||||
}
|
||||
|
@ -6,8 +6,8 @@ import (
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/boombuler/barcode"
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"git.bbr-dev.info/brajkovic/barcode"
|
||||
"git.bbr-dev.info/brajkovic/barcode/utils"
|
||||
)
|
||||
|
||||
func strToRunes(str string) []rune {
|
||||
@ -155,8 +155,8 @@ func getCodeIndexList(content []rune) *utils.BitList {
|
||||
return result
|
||||
}
|
||||
|
||||
// Encode creates a Code 128 barcode for the given content
|
||||
func Encode(content string) (barcode.BarcodeIntCS, error) {
|
||||
// Encode creates a Code 128 barcode for the given content and color scheme
|
||||
func EncodeWithColor(content string, color barcode.ColorScheme) (barcode.BarcodeIntCS, error) {
|
||||
contentRunes := strToRunes(content)
|
||||
if len(contentRunes) <= 0 || len(contentRunes) > 80 {
|
||||
return nil, fmt.Errorf("content length should be between 1 and 80 runes but got %d", len(contentRunes))
|
||||
@ -180,10 +180,19 @@ func Encode(content string) (barcode.BarcodeIntCS, error) {
|
||||
sum = sum % 103
|
||||
result.AddBit(encodingTable[sum]...)
|
||||
result.AddBit(encodingTable[stopSymbol]...)
|
||||
return utils.New1DCodeIntCheckSum(barcode.TypeCode128, content, result, sum), nil
|
||||
return utils.New1DCodeIntCheckSumWithColor(barcode.TypeCode128, content, result, sum, color), nil
|
||||
}
|
||||
|
||||
// Encode creates a Code 128 barcode for the given content
|
||||
func Encode(content string) (barcode.BarcodeIntCS, error) {
|
||||
return EncodeWithColor(content, barcode.ColorScheme16)
|
||||
}
|
||||
|
||||
func EncodeWithoutChecksum(content string) (barcode.Barcode, error) {
|
||||
return EncodeWithoutChecksumWithColor(content, barcode.ColorScheme16)
|
||||
}
|
||||
|
||||
func EncodeWithoutChecksumWithColor(content string, color barcode.ColorScheme) (barcode.Barcode, error) {
|
||||
contentRunes := strToRunes(content)
|
||||
if len(contentRunes) <= 0 || len(contentRunes) > 80 {
|
||||
return nil, fmt.Errorf("content length should be between 1 and 80 runes but got %d", len(contentRunes))
|
||||
@ -199,5 +208,5 @@ func EncodeWithoutChecksum(content string) (barcode.Barcode, error) {
|
||||
result.AddBit(encodingTable[idx]...)
|
||||
}
|
||||
result.AddBit(encodingTable[stopSymbol]...)
|
||||
return utils.New1DCode(barcode.TypeCode128, content, result), nil
|
||||
return utils.New1DCodeWithColor(barcode.TypeCode128, content, result, color), nil
|
||||
}
|
||||
|
@ -6,8 +6,8 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/boombuler/barcode"
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"git.bbr-dev.info/brajkovic/barcode"
|
||||
"git.bbr-dev.info/brajkovic/barcode/utils"
|
||||
)
|
||||
|
||||
type encodeInfo struct {
|
||||
@ -111,9 +111,9 @@ func prepare(content string) (string, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// Encode returns a code39 barcode for the given content
|
||||
// Encode returns a code39 barcode for the given content and color scheme
|
||||
// if includeChecksum is set to true, a checksum character is calculated and added to the content
|
||||
func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.BarcodeIntCS, error) {
|
||||
func EncodeWithColor(content string, includeChecksum bool, fullASCIIMode bool, color barcode.ColorScheme) (barcode.BarcodeIntCS, error) {
|
||||
if fullASCIIMode {
|
||||
var err error
|
||||
content, err = prepare(content)
|
||||
@ -148,5 +148,11 @@ func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.B
|
||||
if err != nil {
|
||||
checkSum = 0
|
||||
}
|
||||
return utils.New1DCodeIntCheckSum(barcode.TypeCode39, content, result, int(checkSum)), nil
|
||||
return utils.New1DCodeIntCheckSumWithColor(barcode.TypeCode39, content, result, int(checkSum), color), nil
|
||||
}
|
||||
|
||||
// Encode returns a code39 barcode for the given content
|
||||
// if includeChecksum is set to true, a checksum character is calculated and added to the content
|
||||
func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.BarcodeIntCS, error) {
|
||||
return EncodeWithColor(content, includeChecksum, fullASCIIMode, barcode.ColorScheme16)
|
||||
}
|
||||
|
@ -5,8 +5,8 @@ import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/boombuler/barcode"
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"git.bbr-dev.info/brajkovic/barcode"
|
||||
"git.bbr-dev.info/brajkovic/barcode/utils"
|
||||
)
|
||||
|
||||
type encodeInfo struct {
|
||||
@ -74,9 +74,9 @@ func prepare(content string) (string, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// Encode returns a code93 barcode for the given content
|
||||
// Encode returns a code93 barcode for the given content and color scheme
|
||||
// if includeChecksum is set to true, two checksum characters are calculated and added to the content
|
||||
func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.Barcode, error) {
|
||||
func EncodeWithColor(content string, includeChecksum bool, fullASCIIMode bool, color barcode.ColorScheme) (barcode.Barcode, error) {
|
||||
if fullASCIIMode {
|
||||
var err error
|
||||
content, err = prepare(content)
|
||||
@ -88,7 +88,9 @@ func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.B
|
||||
}
|
||||
|
||||
data := content + string(getChecksum(content, 20))
|
||||
data += string(getChecksum(data, 15))
|
||||
if includeChecksum {
|
||||
data += string(getChecksum(data, 15))
|
||||
}
|
||||
|
||||
data = "*" + data + "*"
|
||||
result := new(utils.BitList)
|
||||
@ -102,7 +104,13 @@ func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.B
|
||||
}
|
||||
result.AddBit(true)
|
||||
|
||||
return utils.New1DCode(barcode.TypeCode93, content, result), nil
|
||||
return utils.New1DCodeWithColor(barcode.TypeCode93, content, result, color), nil
|
||||
}
|
||||
|
||||
// Encode returns a code93 barcode for the given content
|
||||
// if includeChecksum is set to true, two checksum characters are calculated and added to the content
|
||||
func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.Barcode, error) {
|
||||
return EncodeWithColor(content, includeChecksum, fullASCIIMode, barcode.ColorScheme16)
|
||||
}
|
||||
|
||||
func getChecksum(content string, maxWeight int) rune {
|
||||
|
39
color_scheme.go
Normal file
39
color_scheme.go
Normal file
@ -0,0 +1,39 @@
|
||||
package barcode
|
||||
|
||||
import "image/color"
|
||||
|
||||
// ColorScheme defines a structure for color schemes used in barcode rendering.
|
||||
// It includes the color model, background color, and foreground color.
|
||||
type ColorScheme struct {
|
||||
Model color.Model // Color model to be used (e.g., grayscale, RGB, RGBA)
|
||||
Background color.Color // Color of the background
|
||||
Foreground color.Color // Color of the foreground (e.g., bars in a barcode)
|
||||
}
|
||||
|
||||
// ColorScheme8 represents a color scheme with 8-bit grayscale colors.
|
||||
var ColorScheme8 = ColorScheme{
|
||||
Model: color.GrayModel,
|
||||
Background: color.Gray{Y: 255},
|
||||
Foreground: color.Gray{Y: 0},
|
||||
}
|
||||
|
||||
// ColorScheme16 represents a color scheme with 16-bit grayscale colors.
|
||||
var ColorScheme16 = ColorScheme{
|
||||
Model: color.Gray16Model,
|
||||
Background: color.White,
|
||||
Foreground: color.Black,
|
||||
}
|
||||
|
||||
// ColorScheme24 represents a color scheme with 24-bit RGB colors.
|
||||
var ColorScheme24 = ColorScheme{
|
||||
Model: color.RGBAModel,
|
||||
Background: color.RGBA{255, 255, 255, 255},
|
||||
Foreground: color.RGBA{0, 0, 0, 255},
|
||||
}
|
||||
|
||||
// ColorScheme32 represents a color scheme with 32-bit RGBA colors, which is similar to ColorScheme24 but typically includes alpha for transparency.
|
||||
var ColorScheme32 = ColorScheme{
|
||||
Model: color.RGBAModel,
|
||||
Background: color.RGBA{255, 255, 255, 255},
|
||||
Foreground: color.RGBA{0, 0, 0, 255},
|
||||
}
|
@ -1,8 +1,10 @@
|
||||
package datamatrix
|
||||
|
||||
import (
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"strconv"
|
||||
|
||||
"git.bbr-dev.info/brajkovic/barcode"
|
||||
"git.bbr-dev.info/brajkovic/barcode/utils"
|
||||
)
|
||||
|
||||
type setValFunc func(byte)
|
||||
@ -11,13 +13,15 @@ type codeLayout struct {
|
||||
matrix *utils.BitList
|
||||
occupy *utils.BitList
|
||||
size *dmCodeSize
|
||||
color barcode.ColorScheme
|
||||
}
|
||||
|
||||
func newCodeLayout(size *dmCodeSize) *codeLayout {
|
||||
func newCodeLayout(size *dmCodeSize, color barcode.ColorScheme) *codeLayout {
|
||||
result := new(codeLayout)
|
||||
result.matrix = utils.NewBitList(size.MatrixColumns() * size.MatrixRows())
|
||||
result.occupy = utils.NewBitList(size.MatrixColumns() * size.MatrixRows())
|
||||
result.size = size
|
||||
result.color = color
|
||||
return result
|
||||
}
|
||||
|
||||
@ -159,7 +163,7 @@ func (l *codeLayout) SetValues(data []byte) {
|
||||
}
|
||||
|
||||
func (l *codeLayout) Merge() *datamatrixCode {
|
||||
result := newDataMatrixCode(l.size)
|
||||
result := newDataMatrixCodeWithColor(l.size, l.color)
|
||||
|
||||
//dotted horizontal lines
|
||||
for r := 0; r < l.size.Rows; r += (l.size.RegionRows() + 2) {
|
||||
|
@ -10,19 +10,19 @@ type dmCodeSize struct {
|
||||
}
|
||||
|
||||
func (s *dmCodeSize) RegionRows() int {
|
||||
return (s.Rows - (s.RegionCountHorizontal * 2)) / s.RegionCountHorizontal
|
||||
return (s.Rows - (s.RegionCountVertical * 2)) / s.RegionCountVertical
|
||||
}
|
||||
|
||||
func (s *dmCodeSize) RegionColumns() int {
|
||||
return (s.Columns - (s.RegionCountVertical * 2)) / s.RegionCountVertical
|
||||
return (s.Columns - (s.RegionCountHorizontal * 2)) / s.RegionCountHorizontal
|
||||
}
|
||||
|
||||
func (s *dmCodeSize) MatrixRows() int {
|
||||
return s.RegionRows() * s.RegionCountHorizontal
|
||||
return s.RegionRows() * s.RegionCountVertical
|
||||
}
|
||||
|
||||
func (s *dmCodeSize) MatrixColumns() int {
|
||||
return s.RegionColumns() * s.RegionCountVertical
|
||||
return s.RegionColumns() * s.RegionCountHorizontal
|
||||
}
|
||||
|
||||
func (s *dmCodeSize) DataCodewords() int {
|
||||
|
@ -4,18 +4,23 @@ import (
|
||||
"image"
|
||||
"image/color"
|
||||
|
||||
"github.com/boombuler/barcode"
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"git.bbr-dev.info/brajkovic/barcode"
|
||||
"git.bbr-dev.info/brajkovic/barcode/utils"
|
||||
)
|
||||
|
||||
type datamatrixCode struct {
|
||||
*utils.BitList
|
||||
*dmCodeSize
|
||||
content string
|
||||
color barcode.ColorScheme
|
||||
}
|
||||
|
||||
func newDataMatrixCodeWithColor(size *dmCodeSize, color barcode.ColorScheme) *datamatrixCode {
|
||||
return &datamatrixCode{utils.NewBitList(size.Rows * size.Columns), size, "", color}
|
||||
}
|
||||
|
||||
func newDataMatrixCode(size *dmCodeSize) *datamatrixCode {
|
||||
return &datamatrixCode{utils.NewBitList(size.Rows * size.Columns), size, ""}
|
||||
return &datamatrixCode{utils.NewBitList(size.Rows * size.Columns), size, "", barcode.ColorScheme16}
|
||||
}
|
||||
|
||||
func (c *datamatrixCode) Content() string {
|
||||
@ -27,7 +32,11 @@ func (c *datamatrixCode) Metadata() barcode.Metadata {
|
||||
}
|
||||
|
||||
func (c *datamatrixCode) ColorModel() color.Model {
|
||||
return color.Gray16Model
|
||||
return c.color.Model
|
||||
}
|
||||
|
||||
func (c *datamatrixCode) ColorScheme() barcode.ColorScheme {
|
||||
return c.color
|
||||
}
|
||||
|
||||
func (c *datamatrixCode) Bounds() image.Rectangle {
|
||||
@ -36,9 +45,9 @@ func (c *datamatrixCode) Bounds() image.Rectangle {
|
||||
|
||||
func (c *datamatrixCode) At(x, y int) color.Color {
|
||||
if c.get(x, y) {
|
||||
return color.Black
|
||||
return c.color.Foreground
|
||||
}
|
||||
return color.White
|
||||
return c.color.Background
|
||||
}
|
||||
|
||||
func (c *datamatrixCode) get(x, y int) bool {
|
||||
|
@ -4,11 +4,11 @@ package datamatrix
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/boombuler/barcode"
|
||||
"git.bbr-dev.info/brajkovic/barcode"
|
||||
)
|
||||
|
||||
// Encode returns a Datamatrix barcode for the given content
|
||||
func Encode(content string) (barcode.Barcode, error) {
|
||||
// Encode returns a Datamatrix barcode for the given content and color scheme
|
||||
func EncodeWithColor(content string, color barcode.ColorScheme) (barcode.Barcode, error) {
|
||||
data := encodeText(content)
|
||||
|
||||
var size *dmCodeSize
|
||||
@ -23,7 +23,7 @@ func Encode(content string) (barcode.Barcode, error) {
|
||||
}
|
||||
data = addPadding(data, size.DataCodewords())
|
||||
data = ec.calcECC(data, size)
|
||||
code := render(data, size)
|
||||
code := render(data, size, color)
|
||||
if code != nil {
|
||||
code.content = content
|
||||
return code, nil
|
||||
@ -31,8 +31,13 @@ func Encode(content string) (barcode.Barcode, error) {
|
||||
return nil, errors.New("unable to render barcode")
|
||||
}
|
||||
|
||||
func render(data []byte, size *dmCodeSize) *datamatrixCode {
|
||||
cl := newCodeLayout(size)
|
||||
// Encode returns a Datamatrix barcode for the given content
|
||||
func Encode(content string) (barcode.Barcode, error) {
|
||||
return EncodeWithColor(content, barcode.ColorScheme16)
|
||||
}
|
||||
|
||||
func render(data []byte, size *dmCodeSize, color barcode.ColorScheme) *datamatrixCode {
|
||||
cl := newCodeLayout(size, color)
|
||||
|
||||
cl.SetValues(data)
|
||||
|
||||
@ -69,7 +74,12 @@ func addPadding(data []byte, toCount int) []byte {
|
||||
}
|
||||
for len(data) < toCount {
|
||||
R := ((149 * (len(data) + 1)) % 253) + 1
|
||||
data = append(data, byte((129+R)%254))
|
||||
tmp := 129 + R
|
||||
if tmp > 254 {
|
||||
tmp = tmp - 254
|
||||
}
|
||||
|
||||
data = append(data, byte(tmp))
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package datamatrix
|
||||
|
||||
import (
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"git.bbr-dev.info/brajkovic/barcode/utils"
|
||||
)
|
||||
|
||||
type errorCorrection struct {
|
||||
|
@ -4,8 +4,8 @@ package ean
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/boombuler/barcode"
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"git.bbr-dev.info/brajkovic/barcode"
|
||||
"git.bbr-dev.info/brajkovic/barcode/utils"
|
||||
)
|
||||
|
||||
type encodedNumber struct {
|
||||
@ -157,8 +157,8 @@ func encodeEAN13(code string) *utils.BitList {
|
||||
return result
|
||||
}
|
||||
|
||||
// Encode returns a EAN 8 or EAN 13 barcode for the given code
|
||||
func Encode(code string) (barcode.BarcodeIntCS, error) {
|
||||
// Encode returns a EAN 8 or EAN 13 barcode for the given code and color scheme
|
||||
func EncodeWithColor(code string, color barcode.ColorScheme) (barcode.BarcodeIntCS, error) {
|
||||
var checkSum int
|
||||
if len(code) == 7 || len(code) == 12 {
|
||||
code += string(calcCheckNum(code))
|
||||
@ -175,13 +175,18 @@ func Encode(code string) (barcode.BarcodeIntCS, error) {
|
||||
if len(code) == 8 {
|
||||
result := encodeEAN8(code)
|
||||
if result != nil {
|
||||
return utils.New1DCodeIntCheckSum(barcode.TypeEAN8, code, result, checkSum), nil
|
||||
return utils.New1DCodeIntCheckSumWithColor(barcode.TypeEAN8, code, result, checkSum, color), nil
|
||||
}
|
||||
} else if len(code) == 13 {
|
||||
result := encodeEAN13(code)
|
||||
if result != nil {
|
||||
return utils.New1DCodeIntCheckSum(barcode.TypeEAN13, code, result, checkSum), nil
|
||||
return utils.New1DCodeIntCheckSumWithColor(barcode.TypeEAN13, code, result, checkSum, color), nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New("invalid ean code data")
|
||||
}
|
||||
|
||||
// Encode returns a EAN 8 or EAN 13 barcode for the given code
|
||||
func Encode(code string) (barcode.BarcodeIntCS, error) {
|
||||
return EncodeWithColor(code, barcode.ColorScheme16)
|
||||
}
|
||||
|
2
go.mod
2
go.mod
@ -1 +1 @@
|
||||
module github.com/boombuler/barcode
|
||||
module git.bbr-dev.info/brajkovic/barcode
|
||||
|
@ -3,8 +3,8 @@ package pdf417
|
||||
import "math"
|
||||
|
||||
const (
|
||||
minCols = 2
|
||||
maxCols = 30
|
||||
minCols = 9
|
||||
maxCols = 9
|
||||
maxRows = 30
|
||||
minRows = 2
|
||||
moduleHeight = 2
|
||||
|
@ -4,18 +4,18 @@ package pdf417
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/boombuler/barcode"
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"git.bbr-dev.info/brajkovic/barcode"
|
||||
"git.bbr-dev.info/brajkovic/barcode/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
padding_codeword = 900
|
||||
)
|
||||
|
||||
// Encodes the given data as PDF417 barcode.
|
||||
// Encodes the given data and color scheme as PDF417 barcode.
|
||||
// securityLevel should be between 0 and 8. The higher the number, the more
|
||||
// additional error-correction codes are added.
|
||||
func Encode(data string, securityLevel byte) (barcode.Barcode, error) {
|
||||
func EncodeWithColor(data string, securityLevel byte, color barcode.ColorScheme) (barcode.Barcode, error) {
|
||||
if securityLevel >= 9 {
|
||||
return nil, fmt.Errorf("Invalid security level %d", securityLevel)
|
||||
}
|
||||
@ -34,6 +34,7 @@ func Encode(data string, securityLevel byte) (barcode.Barcode, error) {
|
||||
|
||||
barcode := new(pdfBarcode)
|
||||
barcode.data = data
|
||||
barcode.color = color
|
||||
|
||||
codeWords, err := encodeData(dataWords, columns, sl)
|
||||
if err != nil {
|
||||
@ -70,6 +71,13 @@ func Encode(data string, securityLevel byte) (barcode.Barcode, error) {
|
||||
return barcode, nil
|
||||
}
|
||||
|
||||
// Encodes the given data as PDF417 barcode.
|
||||
// securityLevel should be between 0 and 8. The higher the number, the more
|
||||
// additional error-correction codes are added.
|
||||
func Encode(data string, securityLevel byte) (barcode.Barcode, error) {
|
||||
return EncodeWithColor(data, securityLevel, barcode.ColorScheme16)
|
||||
}
|
||||
|
||||
func encodeData(dataWords []int, columns int, sl securitylevel) ([]int, error) {
|
||||
dataCount := len(dataWords)
|
||||
|
||||
|
@ -4,7 +4,7 @@ import (
|
||||
"errors"
|
||||
"math/big"
|
||||
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"git.bbr-dev.info/brajkovic/barcode/utils"
|
||||
)
|
||||
|
||||
type encodingMode byte
|
||||
|
@ -4,14 +4,15 @@ import (
|
||||
"image"
|
||||
"image/color"
|
||||
|
||||
"github.com/boombuler/barcode"
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"git.bbr-dev.info/brajkovic/barcode"
|
||||
"git.bbr-dev.info/brajkovic/barcode/utils"
|
||||
)
|
||||
|
||||
type pdfBarcode struct {
|
||||
data string
|
||||
width int
|
||||
code *utils.BitList
|
||||
color barcode.ColorScheme
|
||||
}
|
||||
|
||||
func (c *pdfBarcode) Metadata() barcode.Metadata {
|
||||
@ -23,7 +24,11 @@ func (c *pdfBarcode) Content() string {
|
||||
}
|
||||
|
||||
func (c *pdfBarcode) ColorModel() color.Model {
|
||||
return color.Gray16Model
|
||||
return c.color.Model
|
||||
}
|
||||
|
||||
func (c *pdfBarcode) ColorScheme() barcode.ColorScheme {
|
||||
return c.color
|
||||
}
|
||||
|
||||
func (c *pdfBarcode) Bounds() image.Rectangle {
|
||||
@ -34,7 +39,7 @@ func (c *pdfBarcode) Bounds() image.Rectangle {
|
||||
|
||||
func (c *pdfBarcode) At(x, y int) color.Color {
|
||||
if c.code.GetBit((y/moduleHeight)*c.width + x) {
|
||||
return color.Black
|
||||
return c.color.Foreground
|
||||
}
|
||||
return color.White
|
||||
return c.color.Background
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"git.bbr-dev.info/brajkovic/barcode/utils"
|
||||
)
|
||||
|
||||
const charSet string = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"
|
||||
|
@ -3,7 +3,7 @@ package qr
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"git.bbr-dev.info/brajkovic/barcode/utils"
|
||||
)
|
||||
|
||||
func encodeAuto(content string, ecl ErrorCorrectionLevel) (*utils.BitList, *versionInfo, error) {
|
||||
|
@ -4,8 +4,8 @@ package qr
|
||||
import (
|
||||
"image"
|
||||
|
||||
"github.com/boombuler/barcode"
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"git.bbr-dev.info/brajkovic/barcode"
|
||||
"git.bbr-dev.info/brajkovic/barcode/utils"
|
||||
)
|
||||
|
||||
type encodeFn func(content string, eccLevel ErrorCorrectionLevel) (*utils.BitList, *versionInfo, error)
|
||||
@ -54,8 +54,8 @@ func (e Encoding) String() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Encode returns a QR barcode with the given content, error correction level and uses the given encoding
|
||||
func Encode(content string, level ErrorCorrectionLevel, mode Encoding) (barcode.Barcode, error) {
|
||||
// Encode returns a QR barcode with the given content and color scheme, error correction level and uses the given encoding
|
||||
func EncodeWithColor(content string, level ErrorCorrectionLevel, mode Encoding, color barcode.ColorScheme) (barcode.Barcode, error) {
|
||||
bits, vi, err := mode.getEncoder()(content, level)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -63,19 +63,23 @@ func Encode(content string, level ErrorCorrectionLevel, mode Encoding) (barcode.
|
||||
|
||||
blocks := splitToBlocks(bits.IterateBytes(), vi)
|
||||
data := blocks.interleave(vi)
|
||||
result := render(data, vi)
|
||||
result := render(data, vi, color)
|
||||
result.content = content
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func render(data []byte, vi *versionInfo) *qrcode {
|
||||
func Encode(content string, level ErrorCorrectionLevel, mode Encoding) (barcode.Barcode, error) {
|
||||
return EncodeWithColor(content, level, mode, barcode.ColorScheme16)
|
||||
}
|
||||
|
||||
func render(data []byte, vi *versionInfo, color barcode.ColorScheme) *qrcode {
|
||||
dim := vi.modulWidth()
|
||||
results := make([]*qrcode, 8)
|
||||
for i := 0; i < 8; i++ {
|
||||
results[i] = newBarcode(dim)
|
||||
results[i] = newBarCodeWithColor(dim, color)
|
||||
}
|
||||
|
||||
occupied := newBarcode(dim)
|
||||
occupied := newBarCodeWithColor(dim, color)
|
||||
|
||||
setAll := func(x int, y int, val bool) {
|
||||
occupied.Set(x, y, true)
|
||||
|
@ -6,7 +6,7 @@ import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/boombuler/barcode"
|
||||
"git.bbr-dev.info/brajkovic/barcode"
|
||||
)
|
||||
|
||||
type test struct {
|
||||
|
@ -1,7 +1,7 @@
|
||||
package qr
|
||||
|
||||
import (
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"git.bbr-dev.info/brajkovic/barcode/utils"
|
||||
)
|
||||
|
||||
type errorCorrection struct {
|
||||
|
@ -5,7 +5,7 @@ import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"git.bbr-dev.info/brajkovic/barcode/utils"
|
||||
)
|
||||
|
||||
func encodeNumeric(content string, ecl ErrorCorrectionLevel) (*utils.BitList, *versionInfo, error) {
|
||||
|
22
qr/qrcode.go
22
qr/qrcode.go
@ -5,14 +5,15 @@ import (
|
||||
"image/color"
|
||||
"math"
|
||||
|
||||
"github.com/boombuler/barcode"
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"git.bbr-dev.info/brajkovic/barcode"
|
||||
"git.bbr-dev.info/brajkovic/barcode/utils"
|
||||
)
|
||||
|
||||
type qrcode struct {
|
||||
dimension int
|
||||
data *utils.BitList
|
||||
content string
|
||||
color barcode.ColorScheme
|
||||
}
|
||||
|
||||
func (qr *qrcode) Content() string {
|
||||
@ -24,7 +25,11 @@ func (qr *qrcode) Metadata() barcode.Metadata {
|
||||
}
|
||||
|
||||
func (qr *qrcode) ColorModel() color.Model {
|
||||
return color.Gray16Model
|
||||
return qr.color.Model
|
||||
}
|
||||
|
||||
func (c *qrcode) ColorScheme() barcode.ColorScheme {
|
||||
return c.color
|
||||
}
|
||||
|
||||
func (qr *qrcode) Bounds() image.Rectangle {
|
||||
@ -33,9 +38,9 @@ func (qr *qrcode) Bounds() image.Rectangle {
|
||||
|
||||
func (qr *qrcode) At(x, y int) color.Color {
|
||||
if qr.Get(x, y) {
|
||||
return color.Black
|
||||
return qr.color.Foreground
|
||||
}
|
||||
return color.White
|
||||
return qr.color.Background
|
||||
}
|
||||
|
||||
func (qr *qrcode) Get(x, y int) bool {
|
||||
@ -158,9 +163,14 @@ func (qr *qrcode) calcPenaltyRule4() uint {
|
||||
return uint(math.Min(floor, ceil) * 10)
|
||||
}
|
||||
|
||||
func newBarcode(dim int) *qrcode {
|
||||
func newBarCodeWithColor(dim int, color barcode.ColorScheme) *qrcode {
|
||||
res := new(qrcode)
|
||||
res.dimension = dim
|
||||
res.data = utils.NewBitList(dim * dim)
|
||||
res.color = color
|
||||
return res
|
||||
}
|
||||
|
||||
func newBarcode(dim int) *qrcode {
|
||||
return newBarCodeWithColor(dim, barcode.ColorScheme16)
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ package qr
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"git.bbr-dev.info/brajkovic/barcode/utils"
|
||||
)
|
||||
|
||||
func encodeUnicode(content string, ecl ErrorCorrectionLevel) (*utils.BitList, *versionInfo, error) {
|
||||
|
@ -49,11 +49,22 @@ func (bc *intCSscaledBC) CheckSum() int {
|
||||
|
||||
// Scale returns a resized barcode with the given width and height.
|
||||
func Scale(bc Barcode, width, height int) (Barcode, error) {
|
||||
var fill color.Color
|
||||
if v, ok := bc.(BarcodeColor); ok {
|
||||
fill = v.ColorScheme().Background
|
||||
} else {
|
||||
fill = color.White
|
||||
}
|
||||
return ScaleWithFill(bc, width, height, fill)
|
||||
}
|
||||
|
||||
// Scale returns a resized barcode with the given width, height and fill color.
|
||||
func ScaleWithFill(bc Barcode, width, height int, fill color.Color) (Barcode, error) {
|
||||
switch bc.Metadata().Dimensions {
|
||||
case 1:
|
||||
return scale1DCode(bc, width, height)
|
||||
return scale1DCode(bc, width, height, fill)
|
||||
case 2:
|
||||
return scale2DCode(bc, width, height)
|
||||
return scale2DCode(bc, width, height, fill)
|
||||
}
|
||||
|
||||
return nil, errors.New("unsupported barcode format")
|
||||
@ -72,7 +83,7 @@ func newScaledBC(wrapped Barcode, wrapperFunc wrapFunc, rect image.Rectangle) Ba
|
||||
return result
|
||||
}
|
||||
|
||||
func scale2DCode(bc Barcode, width, height int) (Barcode, error) {
|
||||
func scale2DCode(bc Barcode, width, height int, fill color.Color) (Barcode, error) {
|
||||
orgBounds := bc.Bounds()
|
||||
orgWidth := orgBounds.Max.X - orgBounds.Min.X
|
||||
orgHeight := orgBounds.Max.Y - orgBounds.Min.Y
|
||||
@ -87,12 +98,12 @@ func scale2DCode(bc Barcode, width, height int) (Barcode, error) {
|
||||
|
||||
wrap := func(x, y int) color.Color {
|
||||
if x < offsetX || y < offsetY {
|
||||
return color.White
|
||||
return fill
|
||||
}
|
||||
x = (x - offsetX) / factor
|
||||
y = (y - offsetY) / factor
|
||||
if x >= orgWidth || y >= orgHeight {
|
||||
return color.White
|
||||
return fill
|
||||
}
|
||||
return bc.At(x, y)
|
||||
}
|
||||
@ -104,7 +115,7 @@ func scale2DCode(bc Barcode, width, height int) (Barcode, error) {
|
||||
), nil
|
||||
}
|
||||
|
||||
func scale1DCode(bc Barcode, width, height int) (Barcode, error) {
|
||||
func scale1DCode(bc Barcode, width, height int, fill color.Color) (Barcode, error) {
|
||||
orgBounds := bc.Bounds()
|
||||
orgWidth := orgBounds.Max.X - orgBounds.Min.X
|
||||
factor := int(float64(width) / float64(orgWidth))
|
||||
@ -116,12 +127,12 @@ func scale1DCode(bc Barcode, width, height int) (Barcode, error) {
|
||||
|
||||
wrap := func(x, y int) color.Color {
|
||||
if x < offsetX {
|
||||
return color.White
|
||||
return fill
|
||||
}
|
||||
x = (x - offsetX) / factor
|
||||
|
||||
if x >= orgWidth {
|
||||
return color.White
|
||||
return fill
|
||||
}
|
||||
return bc.At(x, 0)
|
||||
}
|
||||
|
@ -5,8 +5,8 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/boombuler/barcode"
|
||||
"github.com/boombuler/barcode/utils"
|
||||
"git.bbr-dev.info/brajkovic/barcode"
|
||||
"git.bbr-dev.info/brajkovic/barcode/utils"
|
||||
)
|
||||
|
||||
const patternWidth = 5
|
||||
@ -78,8 +78,8 @@ func AddCheckSum(content string) (string, error) {
|
||||
return content + string(utils.IntToRune(sum%10)), nil
|
||||
}
|
||||
|
||||
// Encode creates a codabar barcode for the given content
|
||||
func Encode(content string, interleaved bool) (barcode.Barcode, error) {
|
||||
// Encode creates a codabar barcode for the given content and color scheme
|
||||
func EncodeWithColor(content string, interleaved bool, color barcode.ColorScheme) (barcode.Barcode, error) {
|
||||
if content == "" {
|
||||
return nil, errors.New("content is empty")
|
||||
}
|
||||
@ -131,8 +131,13 @@ func Encode(content string, interleaved bool) (barcode.Barcode, error) {
|
||||
resBits.AddBit(mode.end...)
|
||||
|
||||
if interleaved {
|
||||
return utils.New1DCode(barcode.Type2of5Interleaved, content, resBits), nil
|
||||
return utils.New1DCodeWithColor(barcode.Type2of5Interleaved, content, resBits, color), nil
|
||||
} else {
|
||||
return utils.New1DCode(barcode.Type2of5, content, resBits), nil
|
||||
return utils.New1DCodeWithColor(barcode.Type2of5, content, resBits, color), nil
|
||||
}
|
||||
}
|
||||
|
||||
// Encode creates a codabar barcode for the given content
|
||||
func Encode(content string, interleaved bool) (barcode.Barcode, error) {
|
||||
return EncodeWithColor(content, interleaved, barcode.ColorScheme16)
|
||||
}
|
||||
|
@ -5,13 +5,14 @@ import (
|
||||
"image"
|
||||
"image/color"
|
||||
|
||||
"github.com/boombuler/barcode"
|
||||
"git.bbr-dev.info/brajkovic/barcode"
|
||||
)
|
||||
|
||||
type base1DCode struct {
|
||||
*BitList
|
||||
kind string
|
||||
content string
|
||||
color barcode.ColorScheme
|
||||
}
|
||||
|
||||
type base1DCodeIntCS struct {
|
||||
@ -28,7 +29,11 @@ func (c *base1DCode) Metadata() barcode.Metadata {
|
||||
}
|
||||
|
||||
func (c *base1DCode) ColorModel() color.Model {
|
||||
return color.Gray16Model
|
||||
return c.color.Model
|
||||
}
|
||||
|
||||
func (c *base1DCode) ColorScheme() barcode.ColorScheme {
|
||||
return c.color
|
||||
}
|
||||
|
||||
func (c *base1DCode) Bounds() image.Rectangle {
|
||||
@ -37,9 +42,9 @@ func (c *base1DCode) Bounds() image.Rectangle {
|
||||
|
||||
func (c *base1DCode) At(x, y int) color.Color {
|
||||
if c.GetBit(x) {
|
||||
return color.Black
|
||||
return c.color.Foreground
|
||||
}
|
||||
return color.White
|
||||
return c.color.Background
|
||||
}
|
||||
|
||||
func (c *base1DCodeIntCS) CheckSum() int {
|
||||
@ -48,10 +53,20 @@ func (c *base1DCodeIntCS) CheckSum() int {
|
||||
|
||||
// New1DCodeIntCheckSum creates a new 1D barcode where the bars are represented by the bits in the bars BitList
|
||||
func New1DCodeIntCheckSum(codeKind, content string, bars *BitList, checksum int) barcode.BarcodeIntCS {
|
||||
return &base1DCodeIntCS{base1DCode{bars, codeKind, content}, checksum}
|
||||
return &base1DCodeIntCS{base1DCode{bars, codeKind, content, barcode.ColorScheme16}, checksum}
|
||||
}
|
||||
|
||||
// New1DCodeIntCheckSum creates a new 1D barcode where the bars are represented by the bits in the bars BitList
|
||||
func New1DCodeIntCheckSumWithColor(codeKind, content string, bars *BitList, checksum int, color barcode.ColorScheme) barcode.BarcodeIntCS {
|
||||
return &base1DCodeIntCS{base1DCode{bars, codeKind, content, color}, checksum}
|
||||
}
|
||||
|
||||
// New1DCode creates a new 1D barcode where the bars are represented by the bits in the bars BitList
|
||||
func New1DCode(codeKind, content string, bars *BitList) barcode.Barcode {
|
||||
return &base1DCode{bars, codeKind, content}
|
||||
return &base1DCode{bars, codeKind, content, barcode.ColorScheme16}
|
||||
}
|
||||
|
||||
// New1DCode creates a new 1D barcode where the bars are represented by the bits in the bars BitList
|
||||
func New1DCodeWithColor(codeKind, content string, bars *BitList, color barcode.ColorScheme) barcode.Barcode {
|
||||
return &base1DCode{bars, codeKind, content, color}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user