コード例 #1
0
ファイル: totp.go プロジェクト: sis-direct/totp
// BarcodeImage creates a QR code for use with Google Authenticator (GA).
// label is the string that GA uses in the UI. secretkey should be this user's
// secret key. opt should be the configured Options for this TOTP. If a nil
// options is passed, then DefaultOptions is used.
func BarcodeImage(label string, secretkey []byte, opt *Options) ([]byte, error) {
	if opt == nil {
		opt = DefaultOptions
	}

	u := &url.URL{
		Scheme: "otpauth",
		Host:   "totp",
		Path:   fmt.Sprintf("/%s", label),
	}

	params := url.Values{
		"secret": {base32.StdEncoding.EncodeToString(secretkey)},
		"digits": {strconv.Itoa(int(opt.Digits))},
		"period": {strconv.Itoa(int(opt.TimeStep / time.Second))},
	}

	u.RawQuery = params.Encode()

	c, err := qr.Encode(u.String(), qr.ECLevelM)

	if err != nil {
		return nil, err
	}

	var buf bytes.Buffer

	err = png.Encode(&buf, c.Image(8))

	if err != nil {
		return nil, err
	}

	return buf.Bytes(), nil
}
コード例 #2
0
ファイル: test.go プロジェクト: redage/apps
func viewHandler(w http.ResponseWriter, r *http.Request) {
	grid, err := qrencode.Encode("Testing one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixtee n seventeen eighteen nineteen twenty.", qrencode.ECLevelQ)
	if err != nil {
		return
	}
	png.Encode(w, grid.Image(8))
}
コード例 #3
0
ファイル: qrcode.go プロジェクト: RivenZoo/goutil
func (en *alternateQREncoder) Encode2(content string, w io.Writer) (err error) {
	grid, err := qrencode.Encode(content, en.ecLevel)
	if err != nil {
		return
	}
	err = png.Encode(w, grid.Image(en.blockSize))
	return
}
コード例 #4
0
ファイル: stickerl.go プロジェクト: pilu/stickerl
func codesHandler(w traffic.ResponseWriter, r *traffic.Request) {
	code := r.Param("code")
	url := fmt.Sprintf("%s%s", baseUrl, code)

	grid, err := qrencode.Encode(url, qrencode.ECLevelQ)
	if err != nil {
		panic(err)
	}

	w.Header().Set("Content-Type", "image/png")
	png.Encode(w, grid.Image(8))
}
コード例 #5
0
ファイル: test.go プロジェクト: redage/apps
func main() {

	grid, err := qrencode.Encode("Testing one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixtee n seventeen eighteen nineteen twenty.", qrencode.ECLevelQ)
	if err != nil {
		return
	}
	f, err := os.Create("/tmp/qr.png")
	if err != nil {
		return
	}
	defer f.Close()
	png.Encode(f, grid.Image(8))
}
コード例 #6
0
ファイル: setup.go プロジェクト: NatTuck/defunct-fogsync
func qrCodePNG(data string) string {
	grid, err := qrencode.Encode(data, qrencode.ECLevelQ)
	if err != nil {
		panic(err)
	}

	temp := new(bytes.Buffer)
	png.Encode(temp, grid.Image(6))

	image := base64.StdEncoding.EncodeToString(temp.Bytes())

	return `<img src="data:image/png;base64,` + image + `">`
}
コード例 #7
0
ファイル: qrcode.go プロジェクト: RivenZoo/goutil
func (en *alternateQREncoder) EncodeToFile2(content, output string) (err error) {
	grid, err := qrencode.Encode(content, en.ecLevel)
	if err != nil {
		return
	}
	f, err := os.Create(output)
	if err != nil {
		return
	}
	defer f.Close()
	err = png.Encode(f, grid.Image(en.blockSize))
	return
}
コード例 #8
0
ファイル: demoserver.go プロジェクト: rm8x/golang-demoserver
func (c *appContext) prepareSubmission(s *Submitter) {
	//todo:
	address := url.URL{}
	address.Host = "localhost:3000"
	address.Path = "/unlock/"
	address.Path += s.Id.Hex()
	fmt.Println("URL build result: ",address.RequestURI())
	fmt.Println("Full path: ", address.String()[2:])

	result, err := qrencode.Encode(address.String()[2:], qrencode.ECLevelL)
	if err != nil {
		//handle error
		fmt.Println(err)
		return
	}
	fmt.Println(result.String()
	//todo:

	return
}
コード例 #9
0
ファイル: qr.go プロジェクト: schmich/ward
func (app *App) runQr(query []string) {
	db := app.openStore()
	defer db.Close()

	credential := findCredential(db, query)
	if credential == nil {
		return
	}

	grid, err := qrencode.Encode(credential.Password, qrencode.ECLevelL)
	if err != nil {
		printError("%s\n", err)
		return
	}

	identifier := formatCredential(credential)
	printSuccess("Password for %s:\n", identifier)

	stdout := colorable.NewColorableStdout()
	qrc.PrintAA(stdout, grid, false)
}
コード例 #10
0
ファイル: gocupi.go プロジェクト: kvanbiesen/gocupi
// main
func main() {
	Settings.Read()

	pauseOnPenUp := flag.Bool("pause", false, "Pause when pen is raised (requires keyboard input)")
	toImageFlag := flag.Bool("toimage", false, "Output result to an image file instead of to the stepper")
	toFileFlag := flag.Bool("tofile", false, "Output steps to a text file")
	toChartFlag := flag.Bool("tochart", false, "Output a chart of the movement and velocity")
	countFlag := flag.Bool("count", false, "Outputs the time it would take to draw")
	speedSlowFactor := flag.Float64("slowfactor", 1.0, "Divide max speed by this number")
	flipXFlag := flag.Bool("flipx", false, "Flip the drawing left to right")
	flipYFlag := flag.Bool("flipy", false, "Flip the drawing top to bottom")
	flag.Parse()

	if *speedSlowFactor < 1.0 {
		panic("slowfactor must be greater than 1")
	}
	// apply slow factor to max speed
	Settings.MaxSpeed_MM_S /= *speedSlowFactor
	Settings.Acceleration_Seconds *= *speedSlowFactor
	Settings.Acceleration_MM_S2 /= *speedSlowFactor

	args := flag.Args()
	if len(args) < 1 {
		PrintGenericHelp()
		return
	}

	plotCoords := make(chan Coordinate, 1024)
	var err error
	var params []float64

	switch args[0] {

	case "help":
		if len(args) != 2 {
			PrintGenericHelp()
		} else {
			PrintCommandHelp(args[1])
		}
		return

	case "test":
		plotCoords <- Coordinate{X: 0, Y: 0}
		plotCoords <- Coordinate{X: 10, Y: 0}
		plotCoords <- Coordinate{X: 10.1, Y: 0}
		plotCoords <- Coordinate{X: 10.1, Y: 10}
		plotCoords <- Coordinate{X: 10.1, Y: 10, PenUp: true}
		plotCoords <- Coordinate{X: 20.1, Y: 10, PenUp: true}
		plotCoords <- Coordinate{X: 20.1, Y: 15}
		plotCoords <- Coordinate{X: 20.1, Y: 15, PenUp: true}
		plotCoords <- Coordinate{X: 0, Y: 0, PenUp: true}
		close(plotCoords)

	case "circle":
		if params, err = GetArgsAsFloats(args[1:], 3, true); err != nil {
			fmt.Println("ERROR: ", err)
			fmt.Println()
			PrintCommandHelp("circle")
			return
		}
		circleSetup := SlidingCircle{
			Radius:             params[0],
			CircleDisplacement: params[1],
			NumbCircles:        int(params[2]),
		}

		fmt.Println("Generating sliding circle")
		go GenerateSlidingCircle(circleSetup, plotCoords)

	case "gcode":
		if len(args) < 3 {
			fmt.Println("ERROR: ", fmt.Sprint("Expected 2 parameters and saw ", len(args)-1))
			fmt.Println()
			PrintCommandHelp("svg")
		}

		scale, _ := strconv.ParseFloat(args[1], 64)
		if scale == 0 {
			scale = 1
		}

		fmt.Println("Generating Gcode path")
		data := ParseGcodeFile(args[2])
		go GenerateGcodePath(data, scale, plotCoords)

	case "grid":
		if params, err = GetArgsAsFloats(args[1:], 2, true); err != nil {
			fmt.Println("ERROR: ", err)
			fmt.Println()
			PrintCommandHelp("grid")
			return
		}
		gridSetup := Grid{
			Width: params[0],
			Cells: params[1],
		}

		fmt.Println("Generating grid")
		go GenerateGrid(gridSetup, plotCoords)

	case "hilbert":
		if params, err = GetArgsAsFloats(args[1:], 2, true); err != nil {
			fmt.Println("ERROR: ", err)
			fmt.Println()
			PrintCommandHelp("hilbert")
			return
		}
		hilbertSetup := HilbertCurve{
			Size:   params[0],
			Degree: int(params[1]),
		}

		fmt.Println("Generating hilbert curve")
		go GenerateHilbertCurve(hilbertSetup, plotCoords)

	case "imagearc":
		if params, err = GetArgsAsFloats(args[1:], 2, true); err != nil {
			fmt.Println("ERROR: ", err)
			fmt.Println()
			PrintCommandHelp("imagearc")
			return
		}
		arcSetup := Arc{
			Size:    params[0],
			ArcDist: params[1],
		}

		fmt.Println("Generating image arc path")
		data := LoadImage(args[3])
		data = GaussianImage(data)
		go GenerateArc(arcSetup, data, plotCoords)

	case "imageraster":
		if params, err = GetArgsAsFloats(args[1:], 2, true); err != nil {
			fmt.Println("ERROR: ", err)
			fmt.Println()
			PrintCommandHelp("imageraster")
			return
		}
		rasterSetup := Raster{
			Size:     params[0],
			PenWidth: params[1],
		}

		fmt.Println("Generating image raster path")
		data := LoadImage(args[3])
		go GenerateRaster(rasterSetup, data, plotCoords)

	case "lissa":
		if params, err = GetArgsAsFloats(args[1:], 3, true); err != nil {
			fmt.Println("ERROR: ", err)
			fmt.Println()
			PrintCommandHelp("lissa")
			return
		}
		posFunc := func(t float64) Coordinate {
			return Coordinate{
				X: params[0] * math.Cos(params[1]*t+math.Pi/2.0),
				Y: params[0] * math.Sin(params[2]*t),
			}
		}

		fmt.Println("Generating Lissajous curve")
		go GenerateParametric(posFunc, plotCoords)

	case "line":
		if params, err = GetArgsAsFloats(args[1:], 2, true); err != nil {
			fmt.Println("ERROR: ", err)
			fmt.Println()
			PrintCommandHelp("line")
			return
		}
		lineSetup := BouncingLine{
			Angle:         params[0],
			TotalDistance: params[1],
		}

		fmt.Println("Generating line")
		go GenerateBouncingLine(lineSetup, plotCoords)

	case "move":
		PerformMouseTracking()
		return

	case "parabolic":
		if params, err = GetArgsAsFloats(args[1:], 3, true); err != nil {
			fmt.Println("ERROR: ", err)
			fmt.Println()
			PrintCommandHelp("parabolic")
			return
		}
		parabolicSetup := Parabolic{
			Radius:           params[0],
			PolygonEdgeCount: params[1],
			Lines:            params[2],
		}

		fmt.Println("Generating parabolic graph")
		go GenerateParabolic(parabolicSetup, plotCoords)

	case "setup":
		if params, err = GetArgsAsFloats(args[1:], 3, false); err != nil {
			fmt.Println("ERROR: ", err)
			fmt.Println()
			PrintCommandHelp("setup")
			return
		}

		if params[0] != 0 {
			Settings.SpoolHorizontalDistance_MM = params[0]
		} else {
			fmt.Println("Using existing SpoolHorizontalDistance_MM of", Settings.SpoolHorizontalDistance_MM)
		}
		if params[1] != 0 {
			Settings.StartingLeftDist_MM = params[1]
		} else {
			fmt.Println("Using existing StartingLeftDist_MM of", Settings.StartingLeftDist_MM)
		}
		if params[2] != 0 {
			Settings.StartingRightDist_MM = params[2]
		} else {
			fmt.Println("Using existing StartingRightDist_MM of", Settings.StartingRightDist_MM)
		}

		if Settings.SpoolHorizontalDistance_MM > (Settings.StartingLeftDist_MM + Settings.StartingRightDist_MM) {
			fmt.Println("ERROR: Attempted to specify a setup where the two string distances are less than the distance between idlers")
			return
		}

		Settings.CalculateDerivedFields()

		polarSystem := PolarSystemFromSettings()
		polarPos := PolarCoordinate{LeftDist: Settings.StartingLeftDist_MM, RightDist: Settings.StartingRightDist_MM}
		pos := polarPos.ToCoord(polarSystem)

		if pos.X < Settings.DrawingSurfaceMinX_MM || pos.X > Settings.DrawingSurfaceMaxX_MM || pos.Y < Settings.DrawingSurfaceMinY_MM || pos.Y > Settings.DrawingSurfaceMaxY_MM {
			fmt.Println("ERROR: The specified settings result in a pen position that exceeds the DrawingSurfaceMin/Max as defined in gocupi_config.xml")
			fmt.Printf("Initial X,Y position of pen would have been %.3f, %.3f", pos.X, pos.Y)
			fmt.Println()
		} else {
			fmt.Printf("Initial X,Y position of pen is %.3f, %.3f", pos.X, pos.Y)
			fmt.Println()
			Settings.Write()
		}

		return

	case "spiral":
		if params, err = GetArgsAsFloats(args[1:], 2, true); err != nil {
			fmt.Println("ERROR: ", err)
			fmt.Println()
			PrintCommandHelp("spiral")
			return
		}
		spiralSetup := Spiral{
			RadiusBegin:       params[0],
			RadiusEnd:         0.01,
			RadiusDeltaPerRev: params[1],
		}

		fmt.Println("Generating spiral")
		go GenerateSpiral(spiralSetup, plotCoords)

	case "spiro":
		if params, err = GetArgsAsFloats(args[1:], 3, true); err != nil {
			fmt.Println("ERROR: ", err)
			fmt.Println()
			PrintCommandHelp("spiro")
			return
		}
		bigR := params[0]
		littleR := params[1]
		pen := params[2]

		posFunc := func(t float64) Coordinate {
			return Coordinate{
				X: (bigR-littleR)*math.Cos(t) + pen*math.Cos(((bigR-littleR)/littleR)*t),
				Y: (bigR-littleR)*math.Sin(t) - pen*math.Sin(((bigR-littleR)/littleR)*t),
			}
		}

		fmt.Println("Generating spiro")
		go GenerateParametric(posFunc, plotCoords)

	case "spool":
		if len(args) == 3 {

			leftSpool := strings.ToLower(args[1]) == "l"
			if params, err = GetArgsAsFloats(args[2:], 1, true); err != nil {
				fmt.Println("ERROR: ", err)
				fmt.Println()
				PrintCommandHelp("spool")
				return
			}

			MoveSpool(leftSpool, params[0])
		} else {
			InteractiveMoveSpool()
		}
		return

	case "svg":
		if len(args) < 3 {
			fmt.Println("ERROR: ", fmt.Sprint("Expected at least 2 parameters and saw ", len(args)-1))
			fmt.Println()
			PrintCommandHelp("svg")
			return
		}

		size, _ := strconv.ParseFloat(args[1], 64)
		if size == 0 {
			size = 1
		}

		svgType := "top"
		if len(args) > 3 {
			svgType = strings.ToLower(args[3])
		}

		fmt.Println("Generating svg path")
		data := ParseSvgFile(args[2])
		switch svgType {
		case "top":
			go GenerateSvgTopPath(data, size, plotCoords)

		case "box":
			go GenerateSvgBoxPath(data, size, plotCoords)

		default:
			fmt.Println("Expected top or box as the svg type, and saw", svgType)
			return
		}

	case "text":
		if len(args) != 3 {
			fmt.Println("ERROR: ", fmt.Sprint("Expected at least 2 parameters and saw ", len(args)-1))
			fmt.Println()
			PrintCommandHelp("text")
			return
		}
		height, _ := strconv.ParseFloat(args[1], 64)
		if height == 0 {
			height = 40
		}

		fmt.Println("Generating text path")
		go GenerateTextPath(args[2], height, plotCoords)

	case "qr":
		if params, err = GetArgsAsFloats(args[1:], 2, true); err != nil {
			fmt.Println("ERROR: ", err)
			fmt.Println()
			PrintCommandHelp("qr")
			return
		}
		rasterSetup := Raster{
			Size:     params[0],
			PenWidth: params[1],
		}

		fmt.Println("Generating qr raster path for ", args[3])
		data, err := qrencode.Encode(args[3], qrencode.ECLevelQ)
		if err != nil {
			panic(err)
		}
		imageData := data.ImageWithMargin(1, 0)
		go GenerateRaster(rasterSetup, imageData, plotCoords)

	default:
		PrintGenericHelp()
		return
	}

	if *flipXFlag || *flipYFlag {
		originalPlotCoords := plotCoords
		plotCoords = make(chan Coordinate, 1024)
		go FlipPlotCoords(*flipXFlag, *flipYFlag, originalPlotCoords, plotCoords)
	}

	if *toImageFlag {
		fmt.Println("Outputting to image")
		DrawToImage("output.png", plotCoords)
		return
	}

	// output the max speed and acceleration
	fmt.Println()
	fmt.Printf("MaxSpeed: %.3f mm/s Accel: %.3f mm/s^2", Settings.MaxSpeed_MM_S, Settings.Acceleration_MM_S2)
	fmt.Println()

	stepData := make(chan int8, 1024)
	go GenerateSteps(plotCoords, stepData)
	switch {
	case *countFlag:
		CountSteps(stepData)
	case *toFileFlag:
		WriteStepsToFile(stepData)
	case *toChartFlag:
		WriteStepsToChart(stepData)
	default:
		WriteStepsToSerial(stepData, *pauseOnPenUp)
	}
}