V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
xrlin
V2EX  ›  分享创造

写了一个二维码生成工具

  •  
  •   xrlin · 2018-09-01 16:38:01 +08:00 · 3340 次点击
    这是一个创建于 2057 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在 GitHub 看到用 python 写的二维码与图片结合的工具,我用 Go 也写了一个玩玩。

    安装

    go get -u github.com/xrlin/qart/...
    

    通过命令行使用

    # create code with png
    qart -m test.png -o out.png http://example.com
    
    # 获取帮助信息
    qart -h
    
    Qart -- Generate charming QR Code encoder in Go
    https://github.com/xrlin/qart
    
    Flags:
      -embed
            when set to true, over the code in the source image.
      -m string
            mask image path
      -o string
            out PNG/GIF file prefix
      -pw int
            image point width (module) (default 3)
      -startX int
            mask image start point
      -startY int
            mask image start point
      -t    print as pure text-art on stdout
      -width int
            sub image width
    
    Usage:
      1. Generate normal qr code to stdout
       qart http://excample.com
      2. Generate code with image to file.
       qart -m test.png http://example.com
    
    Tips:
      1. Arguments except for flags are joined by " " and used to generate QR code.
         Default output is STDOUT. You can set the option to save to file.
      2. To generate QR code with mask image(jpg/png/gif) must specify the output file.
    
    
    
    

    在 go 代码中调用

    import "github.com/xrlin/qart"
    
    q, err := qrcode.NewHalftoneCode(content, qrcode.Highest)
    q.AddOption(qart.Option{Embed: false, MaskImagePath: "test.png"})
    pointWidth := 3
    // Get the image.Image represents the qr code
    ret := q.CodeImage(pointWidth)
    
    // Get the bytes of image
    imgBytes, err := q.ImageData(pointWidth)
    

    生成图片样例

    example example example

    example

    项目地址

    https://github.com/xrlin/qart

    为了方便,我写了一个对应的网站 demo 可以在线生成二维码

    网站地址: https://xrlin.github.io/qart-web

    为了图片展示方便,所有的图片文件会在服务器上临时保留一份,所以敏感信息就不要提交上来了,自己也可以克隆一份编译在本地运行, 我的 vps 配置低,在线图片文件大小限制最大为 1M。

    对于图片处理还有很多可以优化的地方,特别是对于存在压缩优化的 gif 图片。如果大家有什么好的建议,欢迎提交 issue 和 pr。

    7 条回复    2018-09-06 10:05:27 +08:00
    MonoLogueChi
        1
    MonoLogueChi  
       2018-09-01 23:28:34 +08:00 via Android
    我以前也写过,用 C#写的,直接用了几个开源库,还在线上测试了一段时间,后来懒得维护就下线了。
    xrlin
        2
    xrlin  
    OP
       2018-09-01 23:54:29 +08:00
    @MonoLogueChi #1 实现起来不难,以后如果我的 vps 懒得续费了也就下线了。
    wangfei324017
        3
    wangfei324017  
       2018-09-02 11:36:19 +08:00
    想问下楼主参考的 Python 写的项目是哪个= =
    xrlin
        4
    xrlin  
    OP
       2018-09-02 13:48:49 +08:00
    @wangfei324017 #3 https://github.com/7sDream/pyqart 只看了相关的一部分,这个项目的代码写得挺好的。
    Thinkerous
        5
    Thinkerous  
       2018-09-02 20:18:45 +08:00
    一直在用 EFQRCODE,还有 mac 工具。
    支持一下!
    7sDream
        6
    7sDream  
       2018-09-05 20:14:37 +08:00
    看了一下 Readme,二维码编码部分是基于 go-qrcode 这个库的。

    从我之前的开发过程经验看,基于其它二维码编码库的项目是很难做 QArt 效果的;不过做 Halftone Qr Code 还是非常简单的,毕竟从原理来看只是个 Mask 的过程。不过 Halftone 最初的论文里对图片二值化那步做了很多优化,很多项目其实都没有实现,也包括我的。

    另外楼主给的例子好像也没有 QArt 方式的,加上 Readme 里贴的论文也是 Halftone,所以我大胆推测其实是和许多项目一样实现了简化版 Halftone。

    So,其实我建议项目名可以稍微换一换。毕竟真正的 QArt 这个名字是 Ross Cox 大神给他自己的这个想法起的,博文见: https://research.swtch.com/qr/draw

    而且他也有一个自己的 QArt 在线工具: https://research.swtch.com/qr/draw

    ---

    当然名字这个事也不强制拉,继续用下去也没关系。只是不想让大家把 QArt 和 Halftone 搞混了,因为 QArt 的难度比 Halftone 高了不少,需要对二维码的生成过程有详细的了解才行,之前在知乎介绍的时候也经常遇到过这种搞混的事情,所以在这里简单提一下。
    xrlin
        7
    xrlin  
    OP
       2018-09-06 10:05:27 +08:00
    @7sDream 是的要实现 Ross Cox 的算法需要额实现自己的编码,这部分打算以后再实现,现在只实现了 Halftone。当时起名字是想要表达艺术二维码的意思,然后将 qrcode 和 art 两个词组合起来了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   999 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 22:45 · PVG 06:45 · LAX 15:45 · JFK 18:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.