Esempio n. 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()))
	}
}
Esempio n. 2
0
func qrHandler(c chan struct{}, in io.Reader) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		defer close(c)
		err := func() error {
			b, err := ioutil.ReadAll(in)
			if err != nil {
				return err
			} else if len(b) == 0 {
				return fmt.Errorf("no text provided")
			}
			code, err := qr.Encode(string(b), qr.H, qr.Auto)
			if err != nil {
				return err
			} else {
				code, err = barcode.Scale(code, 400, 400)
				if err != nil {
					return fmt.Errorf("scaling barcode: %s", err)
				}
				w.Header().Set("Content-Type", "image/png")
				buf := &bytes.Buffer{}
				err = png.Encode(buf, code)
				if err != nil {
					return fmt.Errorf("encoding png image: %s", err)
				}
				io.Copy(w, buf)
			}
			return nil
		}()
		if err != nil {
			http.Error(w, err.Error(), 500)
		}
	}
}
Esempio n. 3
0
// Barcode puts a registered barcode in the current page.
//
// The size should be specified in the units used to create the PDF document.
//
// Positioning with x, y and flow is inherited from Fpdf.Image().
func Barcode(pdf barcodePdf, code string, x, y, w, h float64, flow bool) {
	unscaled, ok := barcodes[code]

	if !ok {
		err := errors.New("Barcode not found")
		pdf.SetError(err)
		return
	}

	bname := uniqueBarcodeName(code, x, y)
	info := pdf.GetImageInfo(bname)

	if info == nil {
		bcode, err := barcode.Scale(
			unscaled,
			int(convertTo96Dpi(pdf, w)),
			int(convertTo96Dpi(pdf, h)),
		)

		if err != nil {
			pdf.SetError(err)
			return
		}

		err = registerScaledBarcode(pdf, bname, bcode)
		if err != nil {
			pdf.SetError(err)
			return
		}
	}

	pdf.Image(bname, x, y, 0, 0, flow, "jpg", 0, "")
}
Esempio n. 4
0
func createQrCode(data string, scale image.Point) image.Image {
	bar, err := qr.Encode(data, qr.M, qr.Auto)
	chk(err)
	imgWidth := bar.Bounds().Dx() * scale.X
	imgHeight := bar.Bounds().Dy() * scale.Y
	barScale, err := barcode.Scale(bar, imgWidth, imgHeight)
	chk(err)
	return barScale
}
Esempio n. 5
0
func (en *QREncoder) Encode(content string, w io.Writer) (err error) {
	code, err := qr.Encode(content, en.ecLevel, en.encodeMode)
	if err != nil {
		return
	}
	code, err = barcode.Scale(code, en.size, en.size)
	if err != nil {
		return
	}
	err = png.Encode(w, code)
	return
}
Esempio n. 6
0
func QrGenerator(w http.ResponseWriter, r *http.Request) {
	data := r.URL.Query().Get("data")
	if data == "" {
		http.Error(w, "", http.StatusBadRequest)
		return
	}

	s, err := url.QueryUnescape(data)
	if err != nil {
		http.Error(w, "", http.StatusBadRequest)
		return
	}

	code, err := qr.Encode(s, qr.L, qr.Auto)
	if err != nil {
		http.Error(w, "", http.StatusInternalServerError)
		return
	}

	size := r.URL.Query().Get("size")
	if size == "" {
		size = "250"
	}
	intsize, err := strconv.Atoi(size)
	if err != nil {
		intsize = 250
	}

	// Scale the barcode to the appropriate size
	code, err = barcode.Scale(code, intsize, intsize)
	if err != nil {
		http.Error(w, "", http.StatusInternalServerError)
		return
	}

	buffer := new(bytes.Buffer)
	if err := png.Encode(buffer, code); err != nil {
		http.Error(w, "", http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "image/png")
	w.Header().Set("Content-Length", strconv.Itoa(len(buffer.Bytes())))

	if _, err := w.Write(buffer.Bytes()); err != nil {
		http.Error(w, "", http.StatusInternalServerError)
		return
	}
}
Esempio n. 7
0
File: otp.go Progetto: glycerine/otp
// Image returns an QR-Code image of the specified width and height,
// suitable for use by many clients like Google-Authenricator
// to enroll a user's TOTP/HOTP key.
func (k *Key) Image(width int, height int) (image.Image, error) {
	b, err := qr.Encode(k.orig, qr.M, qr.Auto)

	if err != nil {
		return nil, err
	}

	b, err = barcode.Scale(b, width, height)

	if err != nil {
		return nil, err
	}

	return b, nil
}
Esempio n. 8
0
func (api *Api) QRCode(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	u4 := p.ByName("challenge")
	url := fmt.Sprintf("http://snapinauth.reidsy.com/authenticate/%s", u4)
	qrcode, err := qr.Encode(url, qr.H, qr.Auto)
	if err != nil {
		fmt.Println(err)
		return
	}
	qrcode, err = barcode.Scale(qrcode, 250, 250)
	if err != nil {
		fmt.Println(err)
		return
	}
	png.Encode(w, qrcode)
}
Esempio n. 9
0
func ExampleEncode() {
	f, _ := os.Create("qrcode.png")
	defer f.Close()

	qrcode, err := Encode("hello world", L, Auto)
	if err != nil {
		fmt.Println(err)
	} else {
		qrcode, err = barcode.Scale(qrcode, 100, 100)
		if err != nil {
			fmt.Println(err)
		} else {
			png.Encode(f, qrcode)
		}
	}
}
Esempio n. 10
0
func (en *QREncoder) EncodeToFile(content, output string) (err error) {
	f, err := os.Create(output)
	if err != nil {
		return
	}
	defer f.Close()

	code, err := qr.Encode(content, en.ecLevel, en.encodeMode)
	if err != nil {
		return
	}
	code, err = barcode.Scale(code, en.size, en.size)
	if err != nil {
		return
	}
	err = png.Encode(f, code)
	return
}
Esempio n. 11
0
func Barcode(data string) (pdf *gofpdf.Fpdf, err error) {
	b, err := code128.Encode(data)
	if err != nil {
		return
	}

	b, err = barcode.Scale(b, b.Bounds().Size().X, 10)
	if err != nil {
		panic(err)
	}

	out, err := ioutil.TempFile("", "lblmgrpnpbarcode")
	if err != nil {
		panic(err)
	}
	var opt jpeg.Options

	opt.Quality = 100
	err = jpeg.Encode(out, b, &opt)
	if err != nil {
		return
	}

	imgHeight := float64(b.Bounds().Size().Y)
	imgWidth := float64(b.Bounds().Size().X / 3)

	pdf = gofpdf.NewCustom(&gofpdf.InitType{
		UnitStr: "mm",
		Size:    gofpdf.SizeType{Wd: imgWidth, Ht: imgHeight},
	})

	pdf.AddPage()

	height, width := pdf.GetPageSize()
	pdf.Image(out.Name(), 0, 0, height, width, false, "jpg", 0, "")

	pdf.SetFillColor(255, 255, 255)
	pdf.Rect(0, 7, imgWidth, 5, "F")
	pdf.SetFont("Arial", "B", 6)
	pdf.Text(0, 9.1, data)
	out.Close()
	os.Remove(out.Name())
	return
}
Esempio n. 12
0
// QR generates a byte slice containing the a QR code encoded as a
// PNG with level Q error correction.
func (otp *HOTP) QR(label string) ([]byte, error) {
	u := otp.URL(label)
	code, err := qr.Encode(u, qr.Q, qr.Auto)
	if err != nil {
		return nil, err
	}

	code, err = barcode.Scale(code, 300, 300)

	if err != nil {
		return nil, err
	}

	var buf bytes.Buffer

	err = png.Encode(&buf, code)

	if err != nil {
		return nil, err
	}

	return buf.Bytes(), nil
}
Esempio n. 13
0
func qrcodeHandler(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id := vars["id"]
	game := vars["game"]

	var proto = "http"
	if strings.Contains(r.Proto, "HTTPS") {
		proto = "httpss"
	}

	qrcode, err := qr.Encode(proto+"://"+r.Host+"/"+game+"/client/"+id, qr.L, qr.Auto)
	if err != nil {
		tools.LOG_ERROR.Println(err)
	} else {
		qrcode, err = barcode.Scale(qrcode, 150, 150)
		if err != nil {
			tools.LOG_ERROR.Println(err)
		} else {
			png.Encode(w, qrcode)
		}
	}

}
Esempio n. 14
0
func (u *User) TotpQrImage() ([]byte, error) {
	otpConfig := u.otpConfig()

	// Image uri
	qrCodeImageUri := otpConfig.ProvisionURI(fmt.Sprintf("indispenso:%s", u.Username))

	// QR code
	baseQrImage, qrErr := qr.Encode(qrCodeImageUri, qr.H, qr.Auto)
	if qrErr != nil {
		return nil, fmt.Errorf("Failed to generate QR code: %s", qrErr)
	}

	qrImage, errScale := barcode.Scale(baseQrImage, 300, 300)
	if errScale != nil {
		return nil, fmt.Errorf("Failed to generate QR code scaling problem: %s", errScale)
	}

	var pngQrBuffer bytes.Buffer
	pngQrWriter := bufio.NewWriter(&pngQrBuffer)
	png.Encode(pngQrWriter, qrImage)
	pngQrWriter.Flush()

	return pngQrBuffer.Bytes(), nil
}
Esempio n. 15
0
func main() {

	var (
		// global
		showHelp    = flag.Bool("help", false, "Show usage help")
		showVersion = flag.Bool("version", false, "Show version")
		inputFile   = flag.String("file", "", "Input file")
		outputFile  = flag.String("out", "", "Output file")
		level       = flag.String("level", "L", "Error correction level (L|M|Q|H)")
		size        = flag.Int("size", 250, "Output image size")

		// wifi
		wifiAuth     = flag.String("wifi-auth", "WPA", "Wifi authentication (WPA|WEP|nopass)")
		wifiSSID     = flag.String("wifi-ssid", "", "Wifi SSID")
		wifiPassword = flag.String("wifi-pw", "", "Wifi password")
		wifiHidden   = flag.Bool("wifi-hidden", false, "Wifi hidden (true|false)")

		// geo
		geoLat  = flag.String("geo-lat", "", "Geo deg N latitude")
		geoLong = flag.String("geo-long", "", "Geo deg W longitude")
		geoElev = flag.String("geo-elev", "", "Geo elevation")

		// Google play
		play = flag.String("googleplay", "", "Google Play uri, e.g. \"org.example.app\"")
	)

	flag.Parse()

	if *showVersion {
		printVersion()
	}

	if (flag.NArg() == 0 && *inputFile == "" && *wifiSSID == "" && *geoLat == "" && *play == "") || *showHelp {
		printUsage()
	}

	var text string
	args := flag.Args()

	switch {
	case *wifiSSID != "":
		text = handleWifi(*wifiAuth, *wifiSSID, *wifiPassword, *wifiHidden)
	case *geoLat != "":
		text = handleGeo(*geoLat, *geoLong, *geoElev)
	case *play != "":
		text = handlePlay(*play)
	case *inputFile != "":
		text = handleInput(*inputFile)
	case args[0] != "":
		text = args[0]
	}

	qrcode, err := qr.Encode(text, handleLevelFlag(*level), qr.Auto)
	if err != nil {
		log.Fatal(err)
	}

	qrcode, err = barcode.Scale(qrcode, *size, *size)
	if err != nil {
		log.Fatal(err)
	}

	var f *os.File
	if *outputFile != "" {
		f, err = os.Create(*outputFile)
		if err != nil {
			log.Fatal(err)
		}
	} else {
		f = os.Stdout
	}
	defer f.Close()

	if err := png.Encode(f, qrcode); err != nil {
		log.Fatal(err)
	}
}
Esempio n. 16
0
func QrGenerator(w http.ResponseWriter, r *http.Request) {
	data := r.URL.Query().Get("data")
	if data == "" {
		http.Error(w, "Param data is required", http.StatusBadRequest)
		return
	}

	s, err := url.QueryUnescape(data)
	if err != nil {
		http.Error(w, "", http.StatusBadRequest)
		return
	}

	log.Println("Begin to generate qr for data:", s)
	code, err := qr.Encode(s, qr.L, qr.Auto)
	if err != nil {
		http.Error(w, "", http.StatusInternalServerError)
		return
	}

	size := r.URL.Query().Get("size")
	if size == "" {
		size = "250"
	}
	intsize, err := strconv.Atoi(size)
	if err != nil {
		intsize = 250
	}

	if intsize < 21 {
		http.Error(w, "Can not generate an qr code image smaller than 21x21", http.StatusInternalServerError)
		return
	}

	if intsize > 500 {
		http.Error(w, "The request size is too big, please set a size smaller than 500.", http.StatusInternalServerError)
		return
	}

	// Scale the barcode to the appropriate size
	code, err = barcode.Scale(code, intsize, intsize)
	if err != nil {
		panic(err)
		http.Error(w, "", http.StatusInternalServerError)
		return
	}
	buffer := new(bytes.Buffer)

	qrImage := markWithLogo(r, code)

	if err := png.Encode(buffer, qrImage); err != nil {
		http.Error(w, "", http.StatusInternalServerError)
		return
	}
	w.Header().Set("Content-Type", "image/png")
	w.Header().Set("Content-Length", strconv.Itoa(len(buffer.Bytes())))

	if _, err := w.Write(buffer.Bytes()); err != nil {
		http.Error(w, "", http.StatusInternalServerError)
		return
	}
}