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)
| 16 | // securityLevel should be between 0 and 8. The higher the number, the more |
| 17 | // additional error-correction codes are added. |
| 18 | func 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 |
no test coverage detected