Example #1
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)
		}
	}
}
Example #2
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
}
Example #3
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
}
Example #4
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
	}
}
Example #5
0
File: otp.go Project: 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
}
Example #6
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)
}
Example #7
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
}
Example #8
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
}
Example #9
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)
		}
	}

}
Example #10
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
}
Example #11
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)
	}
}
Example #12
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
	}
}
Example #13
0
// RegisterQR registers a barcode of type QR to the PDF, but not to the page.
// Use Barcode() with the return value to put the barcode on the page.
//
// The ErrorCorrectionLevel and Encoding mode are inherited from qr.Encode().
func RegisterQR(pdf barcodePdf, code string, ecl qr.ErrorCorrectionLevel, mode qr.Encoding) string {
	bcode, err := qr.Encode(code, ecl, mode)
	return registerBarcode(pdf, bcode, err)
}