Пример #1
1
func MakeBarcode(rw http.ResponseWriter, req *http.Request) {
	var id = req.FormValue("id")
	var bigint *big.Int = big.NewInt(0)
	var code barcode.Barcode
	var uuid cassandra.UUID
	var err error

	if id == "" {
		http.NotFound(rw, req)
		return
	}

	uuid, err = cassandra.ParseUUID(id)
	if err != nil {
		log.Print("Error parsing UUID: ", err)
		rw.WriteHeader(http.StatusInternalServerError)
		rw.Write([]byte("Error parsing UUID: " + err.Error()))
		return
	}

	bigint.SetBytes([]byte(uuid))
	id = bigint.String()

	code, err = code128.Encode(id)
	if err != nil {
		log.Print("Error generating barcode: ", err)
		rw.WriteHeader(http.StatusInternalServerError)
		rw.Write([]byte("Error generating barcode: " + err.Error()))
		return
	}

	code, err = barcode.Scale(code, code.Bounds().Max.X, 24*code.Bounds().Max.Y)
	if err != nil {
		log.Print("Error scaling barcode: ", err)
		rw.WriteHeader(http.StatusInternalServerError)
		rw.Write([]byte("Error scaling barcode: " + err.Error()))
		return
	}

	rw.Header().Set("Content-Type", "image/png")
	rw.Header().Set("Content-Disposition", "inline; filename="+uuid.String()+".png")
	err = png.Encode(rw, code)
	if err != nil {
		log.Print("Error writing out image: ", err)
		rw.Header().Set("Content-Type", "text/plain; charset=utf8")
		rw.Header().Set("Content-Disposition", "inline")
		rw.Write([]byte("Error writing out image: " + err.Error()))
	}
}
Пример #2
0
func NewBitmap(code barcode.Barcode) *Bitmap {
	dim := code.Bounds().Max

	b := &Bitmap{data: make([]bool, dim.X*dim.Y), height: dim.Y, width: dim.X}
	for y := 0; y < dim.Y; y++ {
		for x := 0; x < dim.X; x++ {
			if code.At(x, y) == color.Black {
				b.Set(x, y, true)
			}
		}
	}

	return b
}