-
Notifications
You must be signed in to change notification settings - Fork 229
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
Comments
Do you mean the length/width of the image or the size in bytes? |
@Plaenkler Thank you for answering that. The overall image size of QR img is fixed, but the actual part of QR img is changing. 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
} |
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 Lines 81 to 94 in 3357de7
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")
} |
It looks like QR image size depends on accountName length.
I was wondering how to fix QR image size.
The text was updated successfully, but these errors were encountered: