Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QR decoding of this file does not find a code. Scanners (Android, online) find it. #56

Closed
wunderbarb opened this issue Mar 25, 2023 · 2 comments

Comments

@wunderbarb
Copy link

The attached PNG file cannot be detected by the lib. What am I doing wrong?

func Decode(data []byte) (string, error) {
	img, err := png.Decode(bytes.NewReader(data))
	if err != nil {
		return "", ErrNotPNG
	}
	// prepare BinaryBitmap
	bmp, err := gozxing.NewBinaryBitmapFromImage(img)
	if err != nil {
		return "", ErrNotPNG
	}
	// decode image
	qrReader := qr1.NewQRCodeReader()
	result, err := qrReader.Decode(bmp, nil)
	if err != nil {
		return "", ErrNotQRCode
	}
	return result.GetText(), nil
}```
![qr](https://user-images.githubusercontent.com/39924160/227664064-a9c716f0-1907-49da-8f15-3a60cb2a0150.png)
@makiuchi-d
Copy link
Owner

makiuchi-d commented Mar 25, 2023

I got a result with DecodeHintType_PURE_BARCODE.

	result, err := qrReader.Decode(bmp, map[gozxing.DecodeHintType]any{
		gozxing.DecodeHintType_PURE_BARCODE: true,
	})

The online decoder is trying several DecodeHintType patterns and there is a pattern contains DecodeHintType.PURE_BARCODE.
https://github.com/zxing/zxing/blob/master/zxingorg/src/main/java/com/google/zxing/web/DecodeServlet.java#L113

@wunderbarb
Copy link
Author

Thanks Makiuchi-san. It works perfectly. I understand now the role of the hints. May I suggest that you provide an example using it in the documentation? This may help users. Following is a piece of code that uses your code with the hints.

// Decode attempts to extract the qrcode from `data`.  `data` is expected to be a raw png
// image.  The function tries several strategies to decode in increasing order of processing time.
func Decode(data []byte) (string, error) {
	img, err := png.Decode(bytes.NewReader(data))
	if err != nil {
		return "", ErrNotPNG
	}
	// prepare BinaryBitmap
	bmp, err := gozxing.NewBinaryBitmapFromImage(img)
	if err != nil {
		return "", ErrNotPNG
	}
	// decode image
	qrReader := qr1.NewQRCodeReader()
	result, err := qrReader.Decode(bmp, nil)
	if err == nil {
		return result.GetText(), nil
	}
	m := map[gozxing.DecodeHintType]any{
		gozxing.DecodeHintType_PURE_BARCODE: true,
	}
	result, err = qrReader.Decode(bmp, m)
	if err == nil {
		return result.GetText(), nil
	}
	m1 := map[gozxing.DecodeHintType]any{
		gozxing.DecodeHintType_TRY_HARDER: true,
	}
	result, err = qrReader.Decode(bmp, m1)
	if err != nil {
		return "", ErrNotQRCode
	}
	return result.GetText(), nil
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants