| 1 |
package standard |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"image" |
| 6 |
"image/color" |
| 7 |
"io" |
| 8 |
"log" |
| 9 |
"os" |
| 10 |
|
| 11 |
"github.com/yeqown/go-qrcode/v2" |
| 12 |
"github.com/yeqown/go-qrcode/writer/standard/imgkit" |
| 13 |
|
| 14 |
"github.com/fogleman/gg" |
| 15 |
"github.com/pkg/errors" |
| 16 |
) |
| 17 |
|
| 18 |
var _ qrcode.Writer = (*Writer)(nil) |
| 19 |
|
| 20 |
var ( |
| 21 |
ErrNilWriter = errors.New("nil writer") |
| 22 |
) |
| 23 |
|
| 24 |
// Writer is a writer that writes QR Code to io.Writer. |
| 25 |
type Writer struct { |
| 26 |
option *outputImageOptions |
| 27 |
|
| 28 |
closer io.WriteCloser |
| 29 |
} |
| 30 |
|
| 31 |
// New creates a standard writer. |
| 32 |
func New(filename string, opts ...ImageOption) (*Writer, error) { |
| 33 |
if _, err := os.Stat(filename); err != nil && os.IsExist(err) { |
| 34 |
// custom path got: "file exists" |
| 35 |
log.Printf("could not find path: %s, then save to %s", filename, _defaultFilename) |
| 36 |
filename = _defaultFilename |
| 37 |
} |
| 38 |
|
| 39 |
fd, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) |
| 40 |
if err != nil { |
| 41 |
return nil, errors.Wrap(err, "create file failed") |
| 42 |
} |
| 43 |
|
| 44 |
return NewWithWriter(fd, opts...), nil |
| 45 |
} |
| 46 |
|
| 47 |
func NewWithWriter(writeCloser io.WriteCloser, opts ...ImageOption) *Writer { |
| 48 |
dst := defaultOutputImageOption() |
| 49 |
for _, opt := range opts { |
| 50 |
opt.apply(dst) |
| 51 |
} |
| 52 |
|
| 53 |
if writeCloser == nil { |
| 54 |
panic("writeCloser could not be nil") |
| 55 |
} |
| 56 |
|
| 57 |
return &Writer{ |
| 58 |
option: dst, |
| 59 |
closer: writeCloser, |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
const ( |
| 64 |
_defaultFilename = "default.jpeg" |
| 65 |
_defaultPadding = 40 |
| 66 |
) |
| 67 |
|
| 68 |
func (w Writer) Write(mat qrcode.Matrix) error { |
| 69 |
return drawTo(w.closer, mat, w.option) |
| 70 |
} |
| 71 |
|
| 72 |
func (w Writer) Close() error { |
| 73 |
if w.closer == nil { |
| 74 |
return nil |
| 75 |
} |
| 76 |
|
| 77 |
if err := w.closer.Close(); !errors.Is(err, os.ErrClosed) { |
| 78 |
return err |
| 79 |
} |
| 80 |
|
| 81 |
return nil |
| 82 |
} |
| 83 |
|
| 84 |
func (w Writer) Attribute(dimension int) *Attribute { |
| 85 |
return w.option.preCalculateAttribute(dimension) |
| 86 |
} |
| 87 |
|
| 88 |
func drawTo(w io.Writer, mat qrcode.Matrix, option *outputImageOptions) (err error) { |
| 89 |
if option == nil { |
| 90 |
option = defaultOutputImageOption() |
| 91 |
} |
| 92 |
|
| 93 |
if w == nil { |
| 94 |
return ErrNilWriter |
| 95 |
} |
| 96 |
|
| 97 |
img := draw(mat, option) |
| 98 |
|
| 99 |
// DONE(@yeqown): support file format specified config option |
| 100 |
if err = option.imageEncoder.Encode(w, img); err != nil { |
| 101 |
err = fmt.Errorf("imageEncoder.Encode failed: %v", err) |
| 102 |
} |
| 103 |
|
| 104 |
return |
| 105 |
} |
| 106 |
|
| 107 |
// draw deal QRCode's matrix to be an image.Image. Notice that if anyone changed this function, |
| 108 |
// please also check the function outputImageOptions.preCalculateAttribute(). |
| 109 |
func draw(mat qrcode.Matrix, opt *outputImageOptions) image.Image { |
| 110 |
top, right, bottom, left := opt.borderWidths[0], opt.borderWidths[1], opt.borderWidths[2], opt.borderWidths[3] |
| 111 |
// closer as image width, h as image height |
| 112 |
w := mat.Width()*opt.qrBlockWidth() + left + right |
| 113 |
h := mat.Height()*opt.qrBlockWidth() + top + bottom |
| 114 |
dc := gg.NewContext(w, h) |
| 115 |
|
| 116 |
// draw background |
| 117 |
dc.SetColor(opt.backgroundColor()) |
| 118 |
dc.DrawRectangle(0, 0, float64(w), float64(h)) |
| 119 |
dc.Fill() |
| 120 |
|
| 121 |
// qrcode block draw context |
| 122 |
ctx := &DrawContext{ |
| 123 |
Context: dc, |
| 124 |
x: 0.0, |
| 125 |
y: 0.0, |
| 126 |
w: opt.qrBlockWidth(), |
| 127 |
h: opt.qrBlockWidth(), |
| 128 |
color: color.Black, |
| 129 |
} |
| 130 |
shape := opt.getShape() |
| 131 |
|
| 132 |
var ( |
| 133 |
halftoneImg image.Image |
| 134 |
halftoneW = float64(opt.qrBlockWidth()) / 3.0 |
| 135 |
) |
| 136 |
if opt.halftoneImg != nil { |
| 137 |
halftoneImg = imgkit.Binaryzation( |
| 138 |
imgkit.Scale(opt.halftoneImg, image.Rect(0, 0, mat.Width()*3, mat.Width()*3), nil), |
| 139 |
60, |
| 140 |
) |
| 141 |
|
| 142 |
// _ = imgkit.Save(halftoneImg, "mask.jpeg") |
| 143 |
} |
| 144 |
|
| 145 |
// iterate the matrix to Draw each pixel |
| 146 |
mat.Iterate(qrcode.IterDirection_ROW, func(x int, y int, v qrcode.QRValue) { |
| 147 |
// Draw the block |
| 148 |
ctx.x, ctx.y = float64(x*opt.qrBlockWidth()+left), float64(y*opt.qrBlockWidth()+top) |
| 149 |
ctx.w, ctx.h = opt.qrBlockWidth(), opt.qrBlockWidth() |
| 150 |
ctx.color = opt.translateToRGBA(v) |
| 151 |
|
| 152 |
// DONE(@yeqown): make this abstract to Shapes |
| 153 |
switch typ := v.Type(); typ { |
| 154 |
case qrcode.QRType_FINDER: |
| 155 |
shape.DrawFinder(ctx) |
| 156 |
case qrcode.QRType_DATA: |
| 157 |
if halftoneImg == nil { |
| 158 |
shape.Draw(ctx) |
| 159 |
return |
| 160 |
} |
| 161 |
|
| 162 |
ctx2 := &DrawContext{ |
| 163 |
Context: ctx.Context, |
| 164 |
w: int(halftoneW), |
| 165 |
h: int(halftoneW), |
| 166 |
} |
| 167 |
// only halftone image enabled and current block is Data. |
| 168 |
for i := 0; i < 3; i++ { |
| 169 |
for j := 0; j < 3; j++ { |
| 170 |
ctx2.x, ctx2.y = ctx.x+float64(i)*halftoneW, ctx.y+float64(j)*halftoneW |
| 171 |
if i == 1 && j == 1 { |
| 172 |
ctx2.color = ctx.color |
| 173 |
} else { |
| 174 |
ctx2.color = halftoneColor(halftoneImg, opt.bgTransparent, x*3+i, y*3+j) |
| 175 |
} |
| 176 |
shape.Draw(ctx2) |
| 177 |
} |
| 178 |
} |
| 179 |
default: |
| 180 |
shape.Draw(ctx) |
| 181 |
} |
| 182 |
|
| 183 |
// EOFn |
| 184 |
}) |
| 185 |
|
| 186 |
// DONE(@yeqown): add logo image |
| 187 |
if opt.logoImage() != nil { |
| 188 |
// Draw logo image into rgba |
| 189 |
bound := opt.logo.Bounds() |
| 190 |
upperLeft, lowerRight := bound.Min, bound.Max |
| 191 |
logoWidth, logoHeight := lowerRight.X-upperLeft.X, lowerRight.Y-upperLeft.Y |
| 192 |
|
| 193 |
if !validLogoImage(w, h, logoWidth, logoHeight, opt.logoSizeMultiplier) { |
| 194 |
log.Printf("w=%d, h=%d, logoW=%d, logoH=%d, logo is over than 1/%d of QRCode \n", |
| 195 |
w, h, logoWidth, logoHeight, opt.logoSizeMultiplier) |
| 196 |
goto done |
| 197 |
} |
| 198 |
|
| 199 |
// DONE(@yeqown): calculate the xOffset and yOffset which point(xOffset, yOffset) |
| 200 |
// should icon upper-left to start |
| 201 |
dc.DrawImage(opt.logoImage(), (w-logoWidth)/2, (h-logoHeight)/2) |
| 202 |
} |
| 203 |
done: |
| 204 |
return dc.Image() |
| 205 |
} |
| 206 |
|
| 207 |
// halftoneImage is an image.Gray type image, which At(x, y) return color.Gray. |
| 208 |
// black equals to color.Gray{0}, white equals to color.Gray{255}. |
| 209 |
func halftoneColor(halftoneImage image.Image, transparent bool, x, y int) color.Color { |
| 210 |
|
| 211 |
c0 := halftoneImage.At(x, y) |
| 212 |
c1, ok := halftoneImage.At(x, y).(color.Gray) |
| 213 |
if !ok { |
| 214 |
log.Printf("halftoneColor: not a gray image, got: %T\n", c0) |
| 215 |
return c0 |
| 216 |
} |
| 217 |
|
| 218 |
if c1.Y == 255 { |
| 219 |
if transparent { |
| 220 |
return color.RGBA{} |
| 221 |
} |
| 222 |
return color.RGBA{R: 255, G: 255, B: 255, A: 255} |
| 223 |
} |
| 224 |
|
| 225 |
return color.RGBA{A: 255} |
| 226 |
} |
| 227 |
|
| 228 |
func validLogoImage(qrWidth, qrHeight, logoWidth, logoHeight, logoSizeMultiplier int) bool { |
| 229 |
return qrWidth >= logoSizeMultiplier*logoWidth && qrHeight >= logoSizeMultiplier*logoHeight |
| 230 |
} |
| 231 |
|
| 232 |
// Attribute contains basic information of generated image. |
| 233 |
type Attribute struct { |
| 234 |
// width and height of image |
| 235 |
W, H int |
| 236 |
// in the order of "top, right, bottom, left" |
| 237 |
Borders [4]int |
| 238 |
// the length of block edges |
| 239 |
BlockWidth int |
| 240 |
} |