encode completes the steps required to encode the QR Code. These include adding the terminator bits and padding, splitting the data into blocks and applying the error correction, and selecting the best data mask.
()
| 389 | // adding the terminator bits and padding, splitting the data into blocks and |
| 390 | // applying the error correction, and selecting the best data mask. |
| 391 | func (q *QRCode) encode() { |
| 392 | numTerminatorBits := q.version.numTerminatorBitsRequired(q.data.Len()) |
| 393 | |
| 394 | q.addTerminatorBits(numTerminatorBits) |
| 395 | q.addPadding() |
| 396 | |
| 397 | encoded := q.encodeBlocks() |
| 398 | |
| 399 | const numMasks int = 8 |
| 400 | penalty := 0 |
| 401 | |
| 402 | for mask := 0; mask < numMasks; mask++ { |
| 403 | var s *symbol |
| 404 | var err error |
| 405 | |
| 406 | s, err = buildRegularSymbol(q.version, mask, encoded, !q.DisableBorder) |
| 407 | |
| 408 | if err != nil { |
| 409 | log.Panic(err.Error()) |
| 410 | } |
| 411 | |
| 412 | numEmptyModules := s.numEmptyModules() |
| 413 | if numEmptyModules != 0 { |
| 414 | log.Panicf("bug: numEmptyModules is %d (expected 0) (version=%d)", |
| 415 | numEmptyModules, q.VersionNumber) |
| 416 | } |
| 417 | |
| 418 | p := s.penaltyScore() |
| 419 | |
| 420 | //log.Printf("mask=%d p=%3d p1=%3d p2=%3d p3=%3d p4=%d\n", mask, p, s.penalty1(), s.penalty2(), s.penalty3(), s.penalty4()) |
| 421 | |
| 422 | if q.symbol == nil || p < penalty { |
| 423 | q.symbol = s |
| 424 | q.mask = mask |
| 425 | penalty = p |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | // addTerminatorBits adds final terminator bits to the encoded data. |
| 431 | // |