MCPcopy Create free account
hub / github.com/boombuler/barcode / EncodeWithColor

Function EncodeWithColor

pdf417/encoder.go:18–72  ·  view source on GitHub ↗

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.

(data string, securityLevel byte, color barcode.ColorScheme)

Source from the content-addressed store, hash-verified

16// securityLevel should be between 0 and 8. The higher the number, the more
17// additional error-correction codes are added.
18func EncodeWithColor(data string, securityLevel byte, color barcode.ColorScheme) (barcode.Barcode, error) {
19 if securityLevel >= 9 {
20 return nil, fmt.Errorf("Invalid security level %d", securityLevel)
21 }
22
23 sl := securitylevel(securityLevel)
24
25 dataWords, err := highlevelEncode(data)
26 if err != nil {
27 return nil, err
28 }
29
30 columns, rows := calcDimensions(len(dataWords), sl.ErrorCorrectionWordCount())
31 if columns < minCols || columns > maxCols || rows < minRows || rows > maxRows {
32 return nil, fmt.Errorf("Unable to fit data in barcode")
33 }
34
35 barcode := new(pdfBarcode)
36 barcode.data = data
37 barcode.color = color
38
39 codeWords, err := encodeData(dataWords, columns, sl)
40 if err != nil {
41 return nil, err
42 }
43
44 grid := [][]int{}
45 for i := 0; i < len(codeWords); i += columns {
46 grid = append(grid, codeWords[i:min(i+columns, len(codeWords))])
47 }
48
49 codes := [][]int{}
50
51 for rowNum, row := range grid {
52 table := rowNum % 3
53 rowCodes := make([]int, 0, columns+4)
54
55 rowCodes = append(rowCodes, start_word)
56 rowCodes = append(rowCodes, getCodeword(table, getLeftCodeWord(rowNum, rows, columns, securityLevel)))
57
58 for _, word := range row {
59 rowCodes = append(rowCodes, getCodeword(table, word))
60 }
61
62 rowCodes = append(rowCodes, getCodeword(table, getRightCodeWord(rowNum, rows, columns, securityLevel)))
63 rowCodes = append(rowCodes, stop_word)
64
65 codes = append(codes, rowCodes)
66 }
67
68 barcode.code = renderBarcode(codes)
69 barcode.width = (columns+4)*17 + 1
70
71 return barcode, nil
72}
73
74// Encodes the given data as PDF417 barcode.
75// securityLevel should be between 0 and 8. The higher the number, the more

Callers 1

EncodeFunction · 0.70

Calls 10

securitylevelTypeAlias · 0.85
calcDimensionsFunction · 0.85
encodeDataFunction · 0.85
minFunction · 0.85
getCodewordFunction · 0.85
getLeftCodeWordFunction · 0.85
getRightCodeWordFunction · 0.85
renderBarcodeFunction · 0.85
highlevelEncodeFunction · 0.70

Tested by

no test coverage detected