| 1 |
package standard |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"image" |
| 6 |
"image/color" |
| 7 |
"image/jpeg" |
| 8 |
"image/png" |
| 9 |
"os" |
| 10 |
|
| 11 |
"github.com/yeqown/go-qrcode/writer/standard/imgkit" |
| 12 |
) |
| 13 |
|
| 14 |
// funcOption wraps a function that modifies outputImageOptions into an |
| 15 |
// implementation of the ImageOption interface. |
| 16 |
type funcOption struct { |
| 17 |
f func(oo *outputImageOptions) |
| 18 |
} |
| 19 |
|
| 20 |
func (fo *funcOption) apply(oo *outputImageOptions) { |
| 21 |
fo.f(oo) |
| 22 |
} |
| 23 |
|
| 24 |
func newFuncOption(f func(oo *outputImageOptions)) *funcOption { |
| 25 |
return &funcOption{ |
| 26 |
f: f, |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
// WithBgTransparent makes the background transparent. |
| 31 |
func WithBgTransparent() ImageOption { |
| 32 |
return newFuncOption(func(oo *outputImageOptions) { |
| 33 |
oo.bgTransparent = true |
| 34 |
}) |
| 35 |
} |
| 36 |
|
| 37 |
// WithBgColor background color |
| 38 |
func WithBgColor(c color.Color) ImageOption { |
| 39 |
return newFuncOption(func(oo *outputImageOptions) { |
| 40 |
if c == nil { |
| 41 |
return |
| 42 |
} |
| 43 |
|
| 44 |
oo.bgColor = parseFromColor(c) |
| 45 |
}) |
| 46 |
} |
| 47 |
|
| 48 |
// WithBgColorRGBHex background color |
| 49 |
func WithBgColorRGBHex(hex string) ImageOption { |
| 50 |
return newFuncOption(func(oo *outputImageOptions) { |
| 51 |
if hex == "" { |
| 52 |
return |
| 53 |
} |
| 54 |
|
| 55 |
oo.bgColor = parseFromHex(hex) |
| 56 |
}) |
| 57 |
} |
| 58 |
|
| 59 |
// WithFgColor QR color |
| 60 |
func WithFgColor(c color.Color) ImageOption { |
| 61 |
return newFuncOption(func(oo *outputImageOptions) { |
| 62 |
if c == nil { |
| 63 |
return |
| 64 |
} |
| 65 |
|
| 66 |
oo.qrColor = parseFromColor(c) |
| 67 |
}) |
| 68 |
} |
| 69 |
|
| 70 |
// WithFgColorRGBHex Hex string to set QR Color |
| 71 |
func WithFgColorRGBHex(hex string) ImageOption { |
| 72 |
return newFuncOption(func(oo *outputImageOptions) { |
| 73 |
oo.qrColor = parseFromHex(hex) |
| 74 |
}) |
| 75 |
} |
| 76 |
|
| 77 |
// WithLogoImage image should only has 1/5 width of QRCode at most |
| 78 |
func WithLogoImage(img image.Image) ImageOption { |
| 79 |
return newFuncOption(func(oo *outputImageOptions) { |
| 80 |
if img == nil { |
| 81 |
return |
| 82 |
} |
| 83 |
|
| 84 |
oo.logo = img |
| 85 |
}) |
| 86 |
} |
| 87 |
|
| 88 |
// WithLogoImageFileJPEG load image from file, jpeg is required. |
| 89 |
// image should only have 1/5 width of QRCode at most |
| 90 |
func WithLogoImageFileJPEG(f string) ImageOption { |
| 91 |
return newFuncOption(func(oo *outputImageOptions) { |
| 92 |
fd, err := os.Open(f) |
| 93 |
if err != nil { |
| 94 |
fmt.Printf("could not open file(%s), error=%v\n", f, err) |
| 95 |
return |
| 96 |
} |
| 97 |
defer fd.Close() |
| 98 |
|
| 99 |
img, err := jpeg.Decode(fd) |
| 100 |
if err != nil { |
| 101 |
fmt.Printf("could not open file(%s), error=%v\n", f, err) |
| 102 |
return |
| 103 |
} |
| 104 |
|
| 105 |
oo.logo = img |
| 106 |
}) |
| 107 |
} |
| 108 |
|
| 109 |
// WithLogoImageFilePNG load image from file, PNG is required. |
| 110 |
// image should only have 1/5 width of QRCode at most |
| 111 |
func WithLogoImageFilePNG(f string) ImageOption { |
| 112 |
return newFuncOption(func(oo *outputImageOptions) { |
| 113 |
fd, err := os.Open(f) |
| 114 |
if err != nil { |
| 115 |
fmt.Printf("Open file(%s) failed: %v\n", f, err) |
| 116 |
return |
| 117 |
} |
| 118 |
defer fd.Close() |
| 119 |
|
| 120 |
img, err := png.Decode(fd) |
| 121 |
if err != nil { |
| 122 |
fmt.Printf("Decode file(%s) as PNG failed: %v\n", f, err) |
| 123 |
return |
| 124 |
} |
| 125 |
|
| 126 |
oo.logo = img |
| 127 |
}) |
| 128 |
} |
| 129 |
|
| 130 |
// WithQRWidth specify width of each qr block |
| 131 |
func WithQRWidth(width uint8) ImageOption { |
| 132 |
return newFuncOption(func(oo *outputImageOptions) { |
| 133 |
oo.qrWidth = int(width) |
| 134 |
}) |
| 135 |
} |
| 136 |
|
| 137 |
// WithCircleShape use circle shape as rectangle(default) |
| 138 |
func WithCircleShape() ImageOption { |
| 139 |
return newFuncOption(func(oo *outputImageOptions) { |
| 140 |
oo.shape = _shapeCircle |
| 141 |
}) |
| 142 |
} |
| 143 |
|
| 144 |
// WithCustomShape use custom shape as rectangle(default) |
| 145 |
func WithCustomShape(shape IShape) ImageOption { |
| 146 |
return newFuncOption(func(oo *outputImageOptions) { |
| 147 |
oo.shape = shape |
| 148 |
}) |
| 149 |
} |
| 150 |
|
| 151 |
// WithBuiltinImageEncoder option includes: JPEG_FORMAT as default, PNG_FORMAT. |
| 152 |
// This works like WithBuiltinImageEncoder, the different between them is |
| 153 |
// formatTyp is enumerated in (JPEG_FORMAT, PNG_FORMAT) |
| 154 |
func WithBuiltinImageEncoder(format formatTyp) ImageOption { |
| 155 |
return newFuncOption(func(oo *outputImageOptions) { |
| 156 |
var encoder ImageEncoder |
| 157 |
switch format { |
| 158 |
case JPEG_FORMAT: |
| 159 |
encoder = jpegEncoder{} |
| 160 |
case PNG_FORMAT: |
| 161 |
encoder = pngEncoder{} |
| 162 |
default: |
| 163 |
panic("Not supported file format") |
| 164 |
} |
| 165 |
|
| 166 |
oo.imageEncoder = encoder |
| 167 |
}) |
| 168 |
} |
| 169 |
|
| 170 |
// WithCustomImageEncoder to use custom image encoder to encode image.Image into |
| 171 |
// io.Writer |
| 172 |
func WithCustomImageEncoder(encoder ImageEncoder) ImageOption { |
| 173 |
return newFuncOption(func(oo *outputImageOptions) { |
| 174 |
if encoder == nil { |
| 175 |
return |
| 176 |
} |
| 177 |
|
| 178 |
oo.imageEncoder = encoder |
| 179 |
}) |
| 180 |
} |
| 181 |
|
| 182 |
// WithBorderWidth specify the both 4 sides' border width. Notice that |
| 183 |
// WithBorderWidth(a) means all border width use this variable `a`, |
| 184 |
// WithBorderWidth(a, b) mean top/bottom equal to `a`, left/right equal to `b`. |
| 185 |
// WithBorderWidth(a, b, c, d) mean top, right, bottom, left. |
| 186 |
func WithBorderWidth(widths ...int) ImageOption { |
| 187 |
apply := func(arr *[4]int, top, right, bottom, left int) { |
| 188 |
arr[0] = top |
| 189 |
arr[1] = right |
| 190 |
arr[2] = bottom |
| 191 |
arr[3] = left |
| 192 |
} |
| 193 |
|
| 194 |
return newFuncOption(func(oo *outputImageOptions) { |
| 195 |
n := len(widths) |
| 196 |
switch n { |
| 197 |
case 0: |
| 198 |
apply(&oo.borderWidths, _defaultPadding, _defaultPadding, _defaultPadding, _defaultPadding) |
| 199 |
case 1: |
| 200 |
apply(&oo.borderWidths, widths[0], widths[0], widths[0], widths[0]) |
| 201 |
case 2, 3: |
| 202 |
apply(&oo.borderWidths, widths[0], widths[1], widths[0], widths[1]) |
| 203 |
default: |
| 204 |
// 4+ |
| 205 |
apply(&oo.borderWidths, widths[0], widths[1], widths[2], widths[3]) |
| 206 |
} |
| 207 |
}) |
| 208 |
} |
| 209 |
|
| 210 |
// WithHalftone ... |
| 211 |
func WithHalftone(path string) ImageOption { |
| 212 |
return newFuncOption(func(oo *outputImageOptions) { |
| 213 |
srcImg, err := imgkit.Read(path) |
| 214 |
if err != nil { |
| 215 |
fmt.Println("Read halftone image failed: ", err) |
| 216 |
return |
| 217 |
} |
| 218 |
|
| 219 |
oo.halftoneImg = srcImg |
| 220 |
}) |
| 221 |
} |
| 222 |
|
| 223 |
// WithLogoSizeMultiplier used in Writer in validLogoImage method to validate logo size |
| 224 |
func WithLogoSizeMultiplier(multiplier int) ImageOption { |
| 225 |
return newFuncOption(func(oo *outputImageOptions) { |
| 226 |
oo.logoSizeMultiplier = multiplier |
| 227 |
}) |
| 228 |
} |