从100 x 100的QR码创建一个2D位数组。

huangapple go评论72阅读模式
英文:

Create 2D bit array from 100 x 100 QR Code

问题

给定一个尺寸为100 x 100的QR码,我需要创建一个二维位数组(array[100][100])来存储1或0。为了更好地理解我要创建的数组,请查看这个Stack Overflow问题中给出的数组

经过几个小时的搜索,我在Google Code上找到了一个看起来可以完成任务的函数。问题是,这段代码是以.go文件的形式给出的,我的电脑无法打开。

理想的解决方案要么提供另一种语言的解决方案,要么建议我如何使用我在Google Code上找到的代码。

非常感谢您的帮助!

英文:

Given a QR Code of dimensions 100 x 100, I need to make a 2D bit array (array[100][100] that will hold 1 or 0). To get a better idea of the array I am trying to make, look at the array given in this Stack Overflow question.

After hours of searching, I found a function on Google Code that would appear to get the job done. The problem is that the code is given in a .go file, which my computer cannot open.

The ideal solution would either offer a solution in another language, or suggest the way I should go about using the code I found on Google Code.

Thank you in advance for your help!

答案1

得分: 4

如果您想将URL(或其他文本)编码为QR码数组,并显示2D数组的文本表示,您可以很容易地使用Go创建一个命令行工具来实现这一点。以下是使用您提到的Go包的完整工作示例。

要编译并运行它,请访问http://golang.org,在那里您可以找到有关如何安装Go、安装外部库和编译工具的说明:

package main

import (
	"flag"
	"fmt"
	"os"

	"code.google.com/p/go-qrcode"
)

var (
	url   = flag.String("url", "", "Url to encode")
	level = flag.Int("level", 0, "Recovery level. 0 is lowest, 3 is highest")
)

func main() {
	flag.Parse()

	if *level < 0 || *level > 3 {
		fmt.Println("Level must be between 0 and 3")
		os.Exit(1)
	}

	qr, err := qrcode.New(*url, qrcode.RecoveryLevel(*level))
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	printArray(qr.Bitmap())
}

func printArray(a [][]bool) {
	fmt.Println("{")
	for _, x := range a {
		fmt.Print("  {")
		out := ""
		for _, y := range x {
			if y {
				out += "1"
			} else {
				out += "0"
			}
			fmt.Print(out)
			out = ","
		}
		fmt.Println("}")
	}
	fmt.Println("}")
}

用法:

Usage of qrarray.exe:
  -level=0: Recovery level. 0 is lowest, 3 is highest
  -url="": Url to encode
英文:

If you are looking for encoding an url (or other text) to a QR code array and show a textual presentation of the 2D array, you can rather easily make a command line tool to do this in Go.
Below is a fully working example of such a tool using the go package you mentioned.

In order to compile it and run it, go to http://golang.org where you can find instructions on how to install Go, install external libraries, and compile the tool:

package main

import (
	&quot;flag&quot;
	&quot;fmt&quot;
	&quot;os&quot;

	&quot;code.google.com/p/go-qrcode&quot;
)

var (
	url   = flag.String(&quot;url&quot;, &quot;&quot;, &quot;Url to encode&quot;)
	level = flag.Int(&quot;level&quot;, 0, &quot;Recovery level. 0 is lowest, 3 is highest&quot;)
)

func main() {
	flag.Parse()

	if *level &lt; 0 || *level &gt; 3 {
		fmt.Println(&quot;Level must be between 0 and 3&quot;)
		os.Exit(1)
	}

	qr, err := qrcode.New(*url, qrcode.RecoveryLevel(*level))
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	printArray(qr.Bitmap())
}

func printArray(a [][]bool) {
	fmt.Println(&quot;{&quot;)
	for _, x := range a {
		fmt.Print(&quot;  {&quot;)
		out := &quot;&quot;
		for _, y := range x {
			if y {
				out += &quot;1&quot;
			} else {
				out += &quot;0&quot;
			}
			fmt.Print(out)
			out = &quot;,&quot;
		}
		fmt.Println(&quot;}&quot;)
	}
	fmt.Println(&quot;}&quot;)
}

Usage:

Usage of qrarray.exe:
  -level=0: Recovery level. 0 is lowest, 3 is highest
  -url=&quot;&quot;: Url to encode

huangapple
  • 本文由 发表于 2014年11月13日 12:03:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/26901131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定