Encode returns an aztec barcode with the given content and color scheme
(data []byte, minECCPercent int, userSpecifiedLayers int, color barcode.ColorScheme)
| 129 | |
| 130 | // Encode returns an aztec barcode with the given content and color scheme |
| 131 | func EncodeWithColor(data []byte, minECCPercent int, userSpecifiedLayers int, color barcode.ColorScheme) (barcode.Barcode, error) { |
| 132 | bits := highlevelEncode(data) |
| 133 | eccBits := ((bits.Len() * minECCPercent) / 100) + 11 |
| 134 | totalSizeBits := bits.Len() + eccBits |
| 135 | var layers, TotalBitsInLayer, wordSize int |
| 136 | var compact bool |
| 137 | var stuffedBits *utils.BitList |
| 138 | if userSpecifiedLayers != DEFAULT_LAYERS { |
| 139 | compact = userSpecifiedLayers < 0 |
| 140 | if compact { |
| 141 | layers = -userSpecifiedLayers |
| 142 | } else { |
| 143 | layers = userSpecifiedLayers |
| 144 | } |
| 145 | if (compact && layers > max_nb_bits_compact) || (!compact && layers > max_nb_bits) { |
| 146 | return nil, fmt.Errorf("Illegal value %d for layers", userSpecifiedLayers) |
| 147 | } |
| 148 | TotalBitsInLayer = totalBitsInLayer(layers, compact) |
| 149 | wordSize = word_size[layers] |
| 150 | usableBitsInLayers := TotalBitsInLayer - (TotalBitsInLayer % wordSize) |
| 151 | stuffedBits = stuffBits(bits, wordSize) |
| 152 | if stuffedBits.Len()+eccBits > usableBitsInLayers { |
| 153 | return nil, fmt.Errorf("Data to large for user specified layer") |
| 154 | } |
| 155 | if compact && stuffedBits.Len() > wordSize*64 { |
| 156 | return nil, fmt.Errorf("Data to large for user specified layer") |
| 157 | } |
| 158 | } else { |
| 159 | wordSize = 0 |
| 160 | stuffedBits = nil |
| 161 | // We look at the possible table sizes in the order Compact1, Compact2, Compact3, |
| 162 | // Compact4, Normal4,... Normal(i) for i < 4 isn't typically used since Compact(i+1) |
| 163 | // is the same size, but has more data. |
| 164 | for i := 0; ; i++ { |
| 165 | if i > max_nb_bits { |
| 166 | return nil, fmt.Errorf("Data too large for an aztec code") |
| 167 | } |
| 168 | compact = i <= 3 |
| 169 | layers = i |
| 170 | if compact { |
| 171 | layers = i + 1 |
| 172 | } |
| 173 | TotalBitsInLayer = totalBitsInLayer(layers, compact) |
| 174 | if totalSizeBits > TotalBitsInLayer { |
| 175 | continue |
| 176 | } |
| 177 | // [Re]stuff the bits if this is the first opportunity, or if the |
| 178 | // wordSize has changed |
| 179 | if wordSize != word_size[layers] { |
| 180 | wordSize = word_size[layers] |
| 181 | stuffedBits = stuffBits(bits, wordSize) |
| 182 | } |
| 183 | usableBitsInLayers := TotalBitsInLayer - (TotalBitsInLayer % wordSize) |
| 184 | if compact && stuffedBits.Len() > wordSize*64 { |
| 185 | // Compact format only allows 64 data words, though C4 can hold more words than that |
| 186 | continue |
| 187 | } |
| 188 | if stuffedBits.Len()+eccBits <= usableBitsInLayers { |
no test coverage detected