go-tinyqr

An experimental minimal QR Code generator  http://go-qrcode.appspot.com
git clone https://git.ce9e.org/go-tinyqr.git

commit
695fc75a09738f9be56aaa3aecfd7dac28e66b63
parent
0bb60b5a716144fa55da373bab578cca49a537d7
Author
tfh <tfh@skip.org>
Date
2017-08-12 16:12
Implement variable sized image support.

Diffstat

M qrcode.go 46 +++++++++++++++++++++++++++++++++++++---------
M qrcode/main.go 3 ---

2 files changed, 37 insertions, 12 deletions


diff --git a/qrcode.go b/qrcode.go

@@ -23,8 +23,16 @@ Two functions cover most use cases:
   23    23 
   24    24 	err := qrcode.WriteFile("https://example.org", qrcode.Medium, 256, "qr.png")
   25    25 
   26    -1 Both examples use the qrcode.Medium error Recovery Level and create a 256x256
   27    -1 pixel, black on white QR Code.
   -1    26 Both examples use the qrcode.Medium error Recovery Level and create a fixed
   -1    27 256x256px size, black on white QR Code.
   -1    28 
   -1    29 To generate a variable sized image instead, specify a negative size (in place of
   -1    30 the 256 above), such as -4 or -5. Larger negative numbers create larger images:
   -1    31 A size of -5 sets each module (QR Code "pixel") to be 5px wide/high.
   -1    32 
   -1    33 - Create a PNG image (variable size, with minimum white padding) and write to a file:
   -1    34 
   -1    35 	err := qrcode.WriteFile("https://example.org", qrcode.Medium, -5, "qr.png")
   28    36 
   29    37 The maximum capacity of a QR Code varies according to the content encoded and
   30    38 the error recovery level. The maximum capacity is 2,953 bytes, 4,296
@@ -53,7 +61,8 @@ import (
   53    61 // Encode a QR Code and return a raw PNG image.
   54    62 //
   55    63 // size is both the image width and height in pixels. If size is too small then
   56    -1 // a larger image is silently returned.
   -1    64 // a larger image is silently returned. Negative values for size cause a
   -1    65 // variable sized image to be returned: See the documentation for Image().
   57    66 //
   58    67 // To serve over HTTP, remember to send a Content-Type: image/png header.
   59    68 func Encode(content string, level RecoveryLevel, size int) ([]byte, error) {
@@ -70,8 +79,9 @@ func Encode(content string, level RecoveryLevel, size int) ([]byte, error) {
   70    79 
   71    80 // WriteFile encodes, then writes a QR Code to the given filename in PNG format.
   72    81 //
   73    -1 // size is both the width and height in pixels. If size is too small then a
   74    -1 // larger image is silently written.
   -1    82 // size is both the image width and height in pixels. If size is too small then
   -1    83 // a larger image is silently written. Negative values for size cause a variable
   -1    84 // sized image to be written: See the documentation for Image().
   75    85 func WriteFile(content string, level RecoveryLevel, size int, filename string) error {
   76    86 	var q *QRCode
   77    87 
@@ -218,11 +228,26 @@ func (q *QRCode) Bitmap() [][]bool {
  218   228 
  219   229 // Image returns the QR Code as an image.Image.
  220   230 //
  221    -1 // size is both the width and height in pixels.
   -1   231 // A positive size sets a fixed image width and height (e.g. 256 yields an
   -1   232 // 256x256px image).
   -1   233 //
   -1   234 // Depending on the amount of data encoded, fixed size images can have different
   -1   235 // amounts of padding (white space around the QR Code). As an alternative, a
   -1   236 // variable sized image can be generated instead:
   -1   237 //
   -1   238 // A negative size causes a variable sized image to be returned. The image
   -1   239 // returned is the minimum size required for the QR Code. Choose a larger
   -1   240 // negative number to increase the scale of the image. e.g. a size of -5 causes
   -1   241 // each module (QR Code "pixel") to be 5px in size.
  222   242 func (q *QRCode) Image(size int) image.Image {
  223   243 	// Minimum pixels (both width and height) required.
  224   244 	realSize := q.symbol.size
  225   245 
   -1   246 	// Variable size support.
   -1   247 	if size < 0 {
   -1   248 		size = size * -1 * realSize
   -1   249 	}
   -1   250 
  226   251 	// Actual pixels available to draw the symbol. Automatically increase the
  227   252 	// image size if it's not large enough.
  228   253 	if size < realSize {
@@ -268,7 +293,8 @@ func (q *QRCode) Image(size int) image.Image {
  268   293 // PNG returns the QR Code as a PNG image.
  269   294 //
  270   295 // size is both the image width and height in pixels. If size is too small then
  271    -1 // a larger image is silently returned.
   -1   296 // a larger image is silently returned. Negative values for size cause a
   -1   297 // variable sized image to be returned: See the documentation for Image().
  272   298 func (q *QRCode) PNG(size int) ([]byte, error) {
  273   299 	img := q.Image(size)
  274   300 
@@ -287,7 +313,8 @@ func (q *QRCode) PNG(size int) ([]byte, error) {
  287   313 // Write writes the QR Code as a PNG image to io.Writer.
  288   314 //
  289   315 // size is both the image width and height in pixels. If size is too small then
  290    -1 // a larger image is silently written.
   -1   316 // a larger image is silently written. Negative values for size cause a
   -1   317 // variable sized image to be written: See the documentation for Image().
  291   318 func (q *QRCode) Write(size int, out io.Writer) error {
  292   319 	var png []byte
  293   320 
@@ -303,7 +330,8 @@ func (q *QRCode) Write(size int, out io.Writer) error {
  303   330 // WriteFile writes the QR Code as a PNG image to the specified file.
  304   331 //
  305   332 // size is both the image width and height in pixels. If size is too small then
  306    -1 // a larger image is silently written.
   -1   333 // a larger image is silently written. Negative values for size cause a
   -1   334 // variable sized image to be written: See the documentation for Image().
  307   335 func (q *QRCode) WriteFile(size int, filename string) error {
  308   336 	var png []byte
  309   337 

diff --git a/qrcode/main.go b/qrcode/main.go

@@ -41,9 +41,6 @@ Usage:
   41    41 	}
   42    42 	flag.Parse()
   43    43 
   44    -1 	if *size <= 0 {
   45    -1 		checkError(fmt.Errorf("Error: value of -s should > 0"))
   46    -1 	}
   47    44 	if len(flag.Args()) == 0 {
   48    45 		flag.Usage()
   49    46 		checkError(fmt.Errorf("Error: no content given"))