TypechoJoeTheme

Dcr163的博客

统计

GO语言实现二维码解码

2022-01-25
/
3 评论
/
46,542 阅读
/
正在检测是否收录...
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(转载时请注明本文出处及文章链接)

评论 (3)
  1. Amber Powlett 作者
    Windows 10 · Google Chrome

    I think everything said made a lot of sense.
    However, what about this? what if you added a little content? I ain't saying your content isn't good, however what if you added something that makes people desire more? I mean Dcr163的博客 is a little plain. You might peek at Yahoo's home page and note how they create article titles to grab viewers to open the links.
    You might add a related video or a related picture or two to grab people interested about everything've written. In my opinion, it might bring your website a little livelier.

    2024-05-09 回复
  2. 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 回复
  3. Kathryn Ellington 作者
    Windows 10 · Google Chrome

    Thanks for finally writing about >Dcr163的博客

    2023-09-29 回复