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

How to fix QR image size #85

Open
ryan-swit opened this issue Sep 15, 2023 · 3 comments
Open

How to fix QR image size #85

ryan-swit opened this issue Sep 15, 2023 · 3 comments

Comments

@ryan-swit
Copy link

It looks like QR image size depends on accountName length.

I was wondering how to fix QR image size.

@Plaenkler
Copy link

It looks like QR image size depends on accountName length.

I was wondering how to fix QR image size.

Do you mean the length/width of the image or the size in bytes?

@ryan-swit
Copy link
Author

ryan-swit commented Sep 20, 2023

@Plaenkler Thank you for answering that.

The overall image size of QR img is fixed, but the actual part of QR img is changing.
Could it be possible to return the same size even if the length of the account name changes?

I want to be able to fix the offset.

func scale2DCode(bc Barcode, width, height int) (Barcode, error) {
	orgBounds := bc.Bounds()
	orgWidth := orgBounds.Max.X - orgBounds.Min.X
	orgHeight := orgBounds.Max.Y - orgBounds.Min.Y

	factor := int(math.Min(float64(width)/float64(orgWidth), float64(height)/float64(orgHeight)))
	if factor <= 0 {
		return nil, fmt.Errorf("can not scale barcode to an image smaller than %dx%d", orgWidth, orgHeight)
	}

	offsetX := (width - (orgWidth * factor)) / 2
	offsetY := (height - (orgHeight * factor)) / 2

	wrap := func(x, y int) color.Color {
		if x < offsetX || y < offsetY {
			return color.White
		}
		x = (x - offsetX) / factor
		y = (y - offsetY) / factor
		if x >= orgWidth || y >= orgHeight {
			return color.White
		}
		return bc.At(x, y)
	}

	return newScaledBC(
		bc,
		wrap,
		image.Rect(0, 0, width, height),
	), nil
}

@KEINOS
Copy link

KEINOS commented Aug 25, 2024

The overall image size of QR img is fixed, but the actual part of QR img is changing.
Could it be possible to return the same size even if the length of the account name changes?

@ryan-swit

I apologize if I did not understand your question correctly.

If you do not want images to be scaled to a fixed size, you need to write a function like Image() that does not scale them automatically.

otp/otp.go

Lines 81 to 94 in 3357de7

func (k *Key) Image(width int, height int) (image.Image, error) {
b, err := qr.Encode(k.orig, qr.M, qr.Auto)
if err != nil {
return nil, err
}
b, err = barcode.Scale(b, width, height)
if err != nil {
return nil, err
}
return b, nil
}

The below example outputs the QR code that does not scale. Each "dot" of the QR code is alway the same size.

package main

import (
	"fmt"
	"image/png"
	"log"
	"os"

	"github.com/boombuler/barcode/qr"
	"github.com/pquerna/otp/totp"
)

func main() {
	key, err := totp.Generate(totp.GenerateOpts{
		Issuer:      "example.com",
		AccountName: "[email protected]",
		SecretSize:  32,
	})
	if err != nil {
		log.Fatalf("Failed to generate OTP: %v", err)
	}

	qrImgFix, err := qr.Encode(key.String(), qr.M, qr.Auto)
	if err != nil {
		log.Fatalf("Failed to generate QR Code img: %v", err)
	}

	file, err := os.Create("otp_qrcode.png")
	if err != nil {
		log.Fatalf("Failed to create file for QR code: %v", err)
	}

	defer file.Close()

	if err := png.Encode(file, qrImgFix); err != nil {
		log.Fatalf("Failed to save QR code image: %v", err)
	}
	fmt.Println("QR code generated and saved as otp_qrcode.png")
}

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

3 participants