Skip to content

Commit

Permalink
feat(writer): update writer with latest qrcode version
Browse files Browse the repository at this point in the history
  • Loading branch information
yeqown committed Feb 17, 2022
1 parent 59e502c commit b01704b
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 214 deletions.
14 changes: 0 additions & 14 deletions image-toolkit/go.mod

This file was deleted.

39 changes: 0 additions & 39 deletions image-toolkit/process_test.go

This file was deleted.

19 changes: 0 additions & 19 deletions writer/halftone/go.mod

This file was deleted.

117 changes: 0 additions & 117 deletions writer/halftone/halftone.go

This file was deleted.

3 changes: 3 additions & 0 deletions writer/standard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ func WithCustomImageEncoder(encoder ImageEncoder) ImageOption
// WithBorderWidth(a, b) mean top/bottom equal to `a`, left/right equal to `b`.
// WithBorderWidth(a, b, c, d) mean top, right, bottom, left.
func WithBorderWidth(widths ...int) ImageOption

// WithHalftone ...
func WithHalftone(path string) ImageOption
```

### extension
Expand Down
3 changes: 3 additions & 0 deletions writer/standard/image_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ type outputImageOptions struct {
// borderWidths indicates the border width of the output image. the order is
// top, right, bottom, left same as the WithBorder
borderWidths [4]int

// halftoneImg is the halftone image for the output image.
halftoneImg image.Image
}

func (oo *outputImageOptions) backgroundColor() color.RGBA {
Expand Down
15 changes: 15 additions & 0 deletions writer/standard/image_option_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"image/jpeg"
"image/png"
"os"

"github.com/yeqown/go-qrcode/writer/standard/imgkit"
)

// funcOption wraps a function that modifies outputImageOptions into an
Expand Down Expand Up @@ -202,3 +204,16 @@ func WithBorderWidth(widths ...int) ImageOption {
}
})
}

// WithHalftone ...
func WithHalftone(path string) ImageOption {
return newFuncOption(func(oo *outputImageOptions) {
srcImg, err := imgkit.Read(path)
if err != nil {
fmt.Println("Read halftone image failed: ", err)
return
}

oo.halftoneImg = srcImg
})
}
16 changes: 7 additions & 9 deletions writer/standard/image_qr_shape.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package standard

import (
"image"
"image/color"

"github.com/fogleman/gg"
Expand All @@ -24,15 +23,15 @@ type IShape interface {
type DrawContext struct {
*gg.Context

upperLeft image.Point // (x1, y1)
w, h int
x, y float64
w, h int

color color.Color
}

// UpperLeft returns the point which indicates the upper left position.
func (dc *DrawContext) UpperLeft() image.Point {
return dc.upperLeft
func (dc *DrawContext) UpperLeft() (dx, dy float64) {
return dc.x, dc.y
}

// Edge returns width and height of each shape could take at most.
Expand All @@ -52,8 +51,7 @@ type rectangle struct{}

func (r rectangle) Draw(c *DrawContext) {
// FIXED(@yeqown): miss parameter of DrawRectangle
c.DrawRectangle(float64(c.upperLeft.X), float64(c.upperLeft.Y),
float64(c.w), float64(c.h))
c.DrawRectangle(c.x, c.y, float64(c.w), float64(c.h))
c.SetColor(c.color)
c.Fill()
}
Expand All @@ -75,8 +73,8 @@ func (r circle) Draw(c *DrawContext) {
radius = r2
}

cx, cy := c.upperLeft.X+c.w/2, c.upperLeft.Y+c.h/2 // get center point
c.DrawCircle(float64(cx), float64(cy), float64(radius))
cx, cy := c.x+float64(c.w)/2.0, c.y+float64(c.h)/2.0 // get center point
c.DrawCircle(cx, cy, float64(radius))
c.SetColor(c.color)
c.Fill()
}
Expand Down
10 changes: 6 additions & 4 deletions image-toolkit/io.go → writer/standard/imgkit/io.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
package kit
package imgkit

import (
"errors"
"image"
"image/jpeg"
"image/png"
"os"
"path/filepath"

"github.com/pkg/errors"
)

// Read reads an image from a file. only support PNG and JPEG yet.
func Read(path string) (img image.Image, err error) {
fd, err := os.Open(path)
if err != nil {
return
return nil, errors.Wrap(err, "failed to open file")
}
defer fd.Close()

img, _, err = image.Decode(fd)
if err != nil {
return
return nil, errors.Wrap(err, "failed to decode image")
}

return img, nil
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kit
package imgkit

import (
"image"
Expand Down
39 changes: 39 additions & 0 deletions writer/standard/imgkit/process_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package imgkit_test

import (
"image"
"testing"

"github.com/stretchr/testify/assert"

"github.com/yeqown/go-qrcode/writer/standard/imgkit"
)

func Test_Gray(t *testing.T) {
img, err := imgkit.Read("testdata/test.png")
assert.NoError(t, err)

out := imgkit.Gray(img)
assert.Equal(t, out.Bounds(), img.Bounds())
imgkit.Save(out, "testdata/test_gray.png")
}

func TestBinaryzation(t *testing.T) {
img, err := imgkit.Read("testdata/test.png")
assert.NoError(t, err)

out := imgkit.Binaryzation(img, 60)
assert.Equal(t, out.Bounds(), img.Bounds())
err = imgkit.Save(out, "testdata/test_binaryzation.png")
assert.NoError(t, err)
}

func TestScale(t *testing.T) {
img, err := imgkit.Read("testdata/test_binaryzation.png")
assert.NoError(t, err)

out := imgkit.Scale(img, image.Rect(0, 0, 100, 100), nil)
assert.Equal(t, out.Bounds(), image.Rect(0, 0, 100, 100))
err = imgkit.Save(out, "testdata/test_binaryzation_scale.png")
assert.NoError(t, err)
}
Loading

0 comments on commit b01704b

Please sign in to comment.