Exemplo n.º 1
0
Arquivo: pic.go Projeto: SKatiyar/qr
// Encode encodes a string using the given version, level, and mask.
func Encode(w http.ResponseWriter, req *http.Request) {
	val := func(s string) int {
		v, _ := strconv.Atoi(req.FormValue(s))
		return v
	}

	l := coding.Level(val("l"))
	v := coding.Version(val("v"))
	enc := coding.String(req.FormValue("t"))
	m := coding.Mask(val("m"))

	p, err := coding.NewPlan(v, l, m)
	if err != nil {
		panic(err)
	}
	cc, err := p.Encode(enc)
	if err != nil {
		panic(err)
	}

	c := &qr.Code{Bitmap: cc.Bitmap, Size: cc.Size, Stride: cc.Stride, Scale: 8}
	w.Header().Set("Content-Type", "image/png")
	w.Header().Set("Cache-Control", "public, max-age=3600")
	w.Write(c.PNG())
}
Exemplo n.º 2
0
Arquivo: qr.go Projeto: SKatiyar/qr
// Encode returns an encoding of text at the given error correction level.
func Encode(text string, level Level) (*Code, error) {
	// Pick data encoding, smallest first.
	// We could split the string and use different encodings
	// but that seems like overkill for now.
	var enc coding.Encoding
	switch {
	case coding.Num(text).Check() == nil:
		enc = coding.Num(text)
	case coding.Alpha(text).Check() == nil:
		enc = coding.Alpha(text)
	default:
		enc = coding.String(text)
	}

	// Pick size.
	l := coding.Level(level)
	var v coding.Version
	for v = coding.MinVersion; ; v++ {
		if v > coding.MaxVersion {
			return nil, errors.New("text too long to encode as QR")
		}
		if enc.Bits(v) <= v.DataBytes(l)*8 {
			break
		}
	}

	// Build and execute plan.
	p, err := coding.NewPlan(v, l, 0)
	if err != nil {
		return nil, err
	}
	cc, err := p.Encode(enc)
	if err != nil {
		return nil, err
	}

	// TODO: Pick appropriate mask.

	return &Code{cc.Bitmap, cc.Size, cc.Stride, 8}, nil
}