TypechoJoeTheme

Dcr163的博客

统计

GO语言实现二维码解码

2022-01-25
/
2 评论
/
45,370 阅读
/
正在检测是否收录...
01/25

go语言实现二维码解码

直接上代码

package main

import (
    "fmt"
    "github.com/tuotoo/qrcode"
    "io/ioutil"
    "log"
    "net/http"
    "os"
    "strings"
)

func main() {
    qrDecode()
}
func qrDecode(){
    var fileName string
    fmt.Println("\n========二维码解析【支持网络二维码】========\n")
    fmt.Println("文件为绝对路径【D:\\a.png】")
    fmt.Printf("【按Q建为退出程序】请输入文件地址:")
    fmt.Scanln(&fileName)
    if strings.ToLower(fileName) == "q" {
        os.Exit(200)
    }
    //处理网络二维码图片
    if (strings.Index(fileName, "http://")) == 0 || strings.Index(fileName, "https://") == 0 {
        fmt.Println("===网络二维码===")
        res, err := http.Get(fileName)
        if err != nil {
            log.Println("网络请求失败:", err.Error())
            qrDecode()
        }
        defer res.Body.Close()
        body, err := ioutil.ReadAll(res.Body)
        if err != nil {
            log.Println("读取内容失败:", err.Error())
            qrDecode()
        }
        var tmpQrImg string = "tmp.jpg"
        file, err := os.Create(tmpQrImg)
        if err != nil {
            log.Println("创建文件失败:", err.Error())
            qrDecode()
        }
        var (
            _       int
            errFile error
        )
        _, errFile = file.Write(body)
        if errFile != nil {
            log.Println("创建文件失败:", errFile.Error())
            qrDecode()
        }
        //重新命名
        fileName = tmpQrImg
        //删除文件
        defer func() {
            err := os.Remove(tmpQrImg)
            if err != nil {
                log.Fatalln("删除网络二维码失败:", err.Error())
            }
        }()
    }
    //Stat返回一个描述name指定的文件对象的FileInfo
    _, err := os.Stat(fileName)
    //判断文件是否存在
    if os.IsNotExist(err) {
        log.Println("文件不存在!", err.Error())
        qrDecode()
    }
    file, err := os.Open(fileName)
    if err != nil {
        log.Println("打开文件失败:", err.Error())
        qrDecode()
    }
    defer file.Close()
    qrmatrix, err := qrcode.Decode(file)
    if err != nil {
        log.Println("解码失败:", err.Error())
        qrDecode()
    } else {
        fmt.Println("解码结果为:")
        fmt.Println(qrmatrix.Content)
        qrDecode()
    }
}

识别率不是特别高,不过也能用。

go二维码
朗读
赞(1)
版权属于:

Dcr163的博客

本文链接:

https://www.dcr163.cn/613.html(转载时请注明本文出处及文章链接)

评论 (2)
  1. Kassie Baum 作者
    Linux · Google Chrome

    And for good reason: It was slow, it seemed completely different than advertised, there were no USB ports with no bulky adapter, the microSD reminiscence card slot wasn't spring loaded, so it was almost inconceivable to get the card out.
    Thus, in Slot Filling level, we further label "耐克"(Nike) as Brand Property (B-Brand/I-Brand), and "黑色"(black) as Color Property (B-Color/I-Color). Without an APX radio, some first responders should carry more than one radio, or depend on info from dispatchers earlier than proceeding with vital response activities.
    The Craig Web site doesn't provide any info on retail locations the place they are available for purchase (or beneficial retail prices, as we mentioned beforehand). The Maylong Web site is maddeningly vague at finest, and just plain inaccurate at worst.
    Web site to help you see your train information -- you might have to attach the detachable USB thumb drive to a pc to sync the data it collects. For more info on slicing-edge products, award a while to the hyperlinks on the subsequent page. That implies that when disaster strikes, first responders from a wide variety of businesses can talk and coordinate in real time.

    2024-01-28 回复
  2. Kathryn Ellington 作者
    Windows 10 · Google Chrome

    Thanks for finally writing about >Dcr163的博客

    2023-09-29 回复

人生倒计时

今日已经过去小时
这周已经过去
本月已经过去
今年已经过去个月

最新回复

  1. William Tardent
    2024-02-27
  2. Maryann Hamer
    2024-02-27
  3. Sanora Pantano
    2024-02-24
  4. aa
    2024-02-21
  5. Kassie Baum
    2024-01-28

标签云