Encode encodes data from reader and splits it into chunks to be futher converted to QR code frames.
(str string)
| 24 | // Encode encodes data from reader and splits it into chunks to be |
| 25 | // futher converted to QR code frames. |
| 26 | func (e *Encoder) Encode(str string) ([]string, error) { |
| 27 | if len(str) < e.chunkLen { |
| 28 | return []string{e.frame(0, len(str), []byte(str))}, nil |
| 29 | } |
| 30 | |
| 31 | numChunks := numberOfChunks(len(str), e.chunkLen) |
| 32 | codec := fountain.NewLubyCodec(numChunks, rand.New(fountain.NewMersenneTwister(200)), solitonDistribution(numChunks)) |
| 33 | |
| 34 | var msg = []byte(str) // copy of str, as EncodeLTBlock is destructive to msg |
| 35 | idsToEncode := ids(int(float64(numChunks) * e.redundancyFactor)) |
| 36 | lubyBlocks := fountain.EncodeLTBlocks(msg, idsToEncode, codec) |
| 37 | |
| 38 | // TODO(divan): use sync.Pool as this probably will be used many times |
| 39 | ret := make([]string, len(lubyBlocks)) |
| 40 | for i, block := range lubyBlocks { |
| 41 | ret[i] = e.frame(block.BlockCode, len(str), block.Data) |
| 42 | } |
| 43 | return ret, nil |
| 44 | } |
| 45 | |
| 46 | // SetRedundancyFactor changes the value of redundancy factor. |
| 47 | func (e *Encoder) SetRedundancyFactor(rf float64) { |