makiuchi-d / gozxing

Port of ZXing (https://github.com/zxing/zxing) core to pure Go.
Other
564 stars 65 forks source link

error: NotFoundException: dimension = 63 #57

Closed Kanna-Neko closed 1 year ago

Kanna-Neko commented 1 year ago

this is my code, the error occur in the last row:

func helloworld(c *gin.Context) {
        file, _ := c.FormFile("file")
        log.Println(file.Filename)
        fileDetail, err := file.Open()
        if err != nil {
            c.JSON(http.StatusBadRequest, PostScanSettlementReply{
                Status: http.StatusBadRequest,
                Msg:    "error: i" + err.Error(),
            })
            return
        }
        img, _, err := image.Decode(fileDetail)
        if err != nil {
            logcat.Log().Sugar().Error("image decode error, the error is ", err.Error())
            c.JSON(http.StatusBadRequest, PostScanSettlementReply{
                Status: http.StatusBadRequest,
                Msg:    "error: " + err.Error(),
            })
            return
        }

        // prepare BinaryBitmap
        bmp, _ := gozxing.NewBinaryBitmapFromImage(img)

        // decode image
        qrReader := qrcode.NewQRCodeReader()
        result, err := qrReader.Decode(bmp, nil)
                //...... more code
}

The qrcode is abcc

makiuchi-d commented 1 year ago

Your image is slightly distorted, so it's not being read properly.

I heavily distorted the image to make it readable. It would be better to prepare a more properly aligned image.

import (
    "golang.org/x/image/draw"
    "golang.org/x/image/math/f64"
)

...

        w, h := img.Bounds().Max.X, img.Bounds().Max.Y
        aff := f64.Aff3{0.8*0.5, -0.866, float64(w), 0.866, 1.5*0.5, 0}
        img2 := image.NewRGBA(image.Rect(0, 0, w*2, h*2))
        draw.NearestNeighbor.Transform(img2, aff, img, img.Bounds(), draw.Over, nil)

        // prepare BinaryBitmap
        bmp, _ := gozxing.NewBinaryBitmapFromImage(img2)

The result:

{"data":{"goods":[{"number":1,"barcode":"9787113266622"},{"number":2,"barcode":"6939261901617"}],"openid":"oPOop5BprabtLarGEuyefvGrIcnI"}}
Kanna-Neko commented 1 year ago

Thank you very much for solving my problem.