first commit

This commit is contained in:
techinc 2023-10-15 00:28:30 +02:00
commit b75e892d2a
3 changed files with 277 additions and 0 deletions

96
dim.go Normal file
View file

@ -0,0 +1,96 @@
package main
import (
_ "image/jpeg"
_ "image/png" //I'm needed
"math"
"os"
"github.com/kenshaw/escpos"
)
const ESC = byte(27)
func pageWidth(widht int) []byte {
return []byte{escpos.GS, 87, byte(widht), 0}
}
func pageMargin(margin int) []byte {
return []byte{escpos.GS, 76, byte(margin), 0}
}
func pageMode() []byte {
return []byte{ESC, 'L'}
}
func pageDirection(direction uint8) []byte {
// https://www.epson-biz.com/modules/ref_escpos/index.php?content_id=55
return []byte{ESC, 'T', direction}
}
func pagePrintArea(x, y, width, height int) []byte {
return []byte{ESC, 'W',
byte(x & 255), byte(x >> 8 & 255),
byte(y & 255), byte(y >> 8 & 255),
byte(width & 255), byte(width >> 8 & 255),
byte(height & 255), byte(height >> 8 & 255)}
}
func offsetY(offset int) []byte {
// A positive number specifies movement downward, and a negative number specifies movement upward.
// The horizontal or vertical motion unit is used for the print direction set by ESC T.
// When the starting position is set to the upper left or lower right of the print area using ESC T, the vertical motion unit is used.
// When the starting position is set to the upper right or lower left of the print area using ESC T, the horizontal motion unit is used.
return []byte{escpos.GS, '\\', byte(offset & 255), byte(offset >> 8 & 255)}
}
func offsetX(offset int) []byte {
// A positive number specifies movement to the right, and a negative number specifies movement to the left.
// When Standard mode is selected, the horizontal motion unit is used.
// when Page mode is selected, the horizontal or vertical motion unit is used for the print direction set by ESC T.
// When the starting position is set to the upper left or lower right of the print area using ESC T, the horizontal motion unit is used.
// When the starting position is set to the upper right or lower left of the print area using ESC T, the vertical motion unit is used.
return []byte{ESC, '\\', byte(offset & 255), byte(offset >> 8 & 255)}
}
func invert(n int) []byte {
//When the LSB of n is 0, white/black reverse print mode is turned off.
//n = 0 255
return []byte{escpos.GS, byte('B'), byte(n)}
}
func disableSensors() []byte {
return []byte{ESC, 'c', '3', 0}
}
func printSpeed(m int) []byte {
// m = 0 9, 48 57 : slowest - fastest
// [Default] 0 = Setting value of GS ( E <Function 5> customize value (a = 6)
// m = 0
//Slower print speed seems to retain a bit more quality in dithered images. pure B/W images do not have a noticable difference
return []byte{escpos.GS, '(', 'K', 2, 0, 50, byte(m & 255), byte(m >> 8 & 255)}
}
func main() {
f, err := os.OpenFile("/dev/usb/lp0", os.O_WRONLY|os.O_CREATE, 0)
if err != nil {
panic(err)
}
defer f.Close()
ep := escpos.New(f)
ep.Init()
ep.WriteRaw(pageMode())
ep.WriteRaw(pageDirection(1))
ep.WriteRaw(pagePrintArea(0, 0, int(math.Round((29.0/80.0)*512)), 850))
ep.WriteRaw(offsetY(30))
ep.SetFontSize(1,1)
ep.Write("Technologia Incognita\n")
ep.WriteRaw(offsetY(35))
ep.SetFontSize(3,2)
ep.SetEmphasize(1)
ep.Write("Do Not Hack\n")
ep.SetEmphasize(0)
ep.WriteRaw(offsetY(15))
ep.SetFontSize(1,1)
ep.Write("Mattronix - 2023-10-14 \n")
ep.WriteRaw([]byte{12}) // FF: in Page mode, prints all the data in the print buffer collectively and switches from Page mode to Standard mode.
//ep.Formfeed()
ep.Cut()
ep.End()
}

BIN
kia.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

181
stickerimg.go Normal file
View file

@ -0,0 +1,181 @@
package main
import (
"image"
_ "image/jpeg"
_ "image/png" //I'm needed
"math"
"os"
"github.com/kenshaw/escpos"
"github.com/kenshaw/escpos/raster"
)
// var ditherers []dither.Dither = []dither.Dither{
// dither.Dither{
// "FloydSteinberg",
// dither.Settings{
// [][]float32{
// []float32{0.0, 0.0, 0.0, 7.0 / 48.0, 5.0 / 48.0},
// []float32{3.0 / 48.0, 5.0 / 48.0, 7.0 / 48.0, 5.0 / 48.0, 3.0 / 48.0},
// []float32{1.0 / 48.0, 3.0 / 48.0, 5.0 / 48.0, 3.0 / 48.0, 1.0 / 48.0},
// },
// },
// },
// dither.Dither{
// "Stucki",
// dither.Settings{
// [][]float32{
// []float32{0.0, 0.0, 0.0, 8.0 / 42.0, 4.0 / 42.0},
// []float32{2.0 / 42.0, 4.0 / 42.0, 8.0 / 42.0, 4.0 / 42.0, 2.0 / 42.0},
// []float32{1.0 / 42.0, 2.0 / 42.0, 4.0 / 42.0, 2.0 / 42.0, 1.0 / 42.0},
// },
// },
// },
// dither.Dither{
// "Atkinson",
// dither.Settings{
// [][]float32{
// []float32{0.0, 0.0, 1.0 / 8.0, 1.0 / 8.0},
// []float32{1.0 / 8.0, 1.0 / 8.0, 1.0 / 8.0, 0.0},
// []float32{0.0, 1.0 / 8.0, 0.0, 0.0},
// },
// },
// },
// dither.Dither{
// "Burkes",
// dither.Settings{
// [][]float32{
// []float32{0.0, 0.0, 0.0, 8.0 / 32.0, 4.0 / 32.0},
// []float32{2.0 / 32.0, 4.0 / 32.0, 8.0 / 32.0, 4.0 / 32.0, 2.0 / 32.0},
// []float32{0.0, 0.0, 0.0, 0.0, 0.0},
// },
// },
// },
// dither.Dither{
// "Sierra-3",
// dither.Settings{
// [][]float32{
// []float32{0.0, 0.0, 0.0, 5.0 / 32.0, 3.0 / 32.0},
// []float32{2.0 / 32.0, 4.0 / 32.0, 5.0 / 32.0, 4.0 / 32.0, 2.0 / 32.0},
// []float32{0.0, 2.0 / 32.0, 3.0 / 32.0, 2.0 / 32.0, 0.0},
// },
// },
// },
// dither.Dither{
// "Sierra-2",
// dither.Settings{
// [][]float32{
// []float32{0.0, 0.0, 0.0, 4.0 / 16.0, 3.0 / 16.0},
// []float32{1.0 / 16.0, 2.0 / 16.0, 3.0 / 16.0, 2.0 / 16.0, 1.0 / 16.0},
// []float32{0.0, 0.0, 0.0, 0.0, 0.0},
// },
// },
// },
// dither.Dither{
// "Sierra-Lite",
// dither.Settings{
// [][]float32{
// []float32{0.0, 0.0, 2.0 / 4.0},
// []float32{1.0 / 4.0, 1.0 / 4.0, 0.0},
// []float32{0.0, 0.0, 0.0},
// },
// },
// },
// }
const ESC = byte(27)
func pageWidth(widht int) []byte {
return []byte{escpos.GS, 87, byte(widht), 0}
}
func pageMargin(margin int) []byte {
return []byte{escpos.GS, 76, byte(margin), 0}
}
func pageMode() []byte {
return []byte{ESC, 'L'}
}
func pageDirection(direction uint8) []byte {
// https://www.epson-biz.com/modules/ref_escpos/index.php?content_id=55
return []byte{ESC, 'T', direction}
}
func pagePrintArea(x, y, width, height int) []byte {
return []byte{ESC, 'W',
byte(x & 255), byte(x >> 8 & 255),
byte(y & 255), byte(y >> 8 & 255),
byte(width & 255), byte(width >> 8 & 255),
byte(height & 255), byte(height >> 8 & 255)}
}
func offsetY(offset int) []byte {
// A positive number specifies movement downward, and a negative number specifies movement upward.
// The horizontal or vertical motion unit is used for the print direction set by ESC T.
// When the starting position is set to the upper left or lower right of the print area using ESC T, the vertical motion unit is used.
// When the starting position is set to the upper right or lower left of the print area using ESC T, the horizontal motion unit is used.
return []byte{escpos.GS, '\\', byte(offset & 255), byte(offset >> 8 & 255)}
}
func offsetX(offset int) []byte {
// A positive number specifies movement to the right, and a negative number specifies movement to the left.
// When Standard mode is selected, the horizontal motion unit is used.
// when Page mode is selected, the horizontal or vertical motion unit is used for the print direction set by ESC T.
// When the starting position is set to the upper left or lower right of the print area using ESC T, the horizontal motion unit is used.
// When the starting position is set to the upper right or lower left of the print area using ESC T, the vertical motion unit is used.
return []byte{ESC, '\\', byte(offset & 255), byte(offset >> 8 & 255)}
}
func invert(n int) []byte {
//When the LSB of n is 0, white/black reverse print mode is turned off.
//n = 0 255
return []byte{escpos.GS, byte('B'), byte(n)}
}
func disableSensors() []byte {
return []byte{ESC, 'c', '3', 0}
}
func printSpeed(m int) []byte {
// m = 0 9, 48 57 : slowest - fastest
// m = 0 - 13 : slowest - fastest version V
// [Default] 0 = Setting value of GS ( E <Function 5> customize value (a = 6)
// m = 0
//Slower print speed seems to retain a bit more quality in dithered images. pure B/W images do not have a noticable difference
return []byte{escpos.GS, '(', 'K', 2, 0, 50, byte(m & 255), byte(m >> 8 & 255)}
}
func main() {
f, err := os.OpenFile("/dev/usb/lp0", os.O_WRONLY|os.O_CREATE, 0)
if err != nil {
panic(err)
}
defer f.Close()
//load image
imagef, err := os.Open("kia.png")
if err != nil {
panic(err)
}
defer imagef.Close()
img, _, err := image.Decode(imagef)
ep := escpos.New(f)
ep.Init()
ep.WriteRaw(pageWidth(int(math.Round((29.0 / 80.0) * 512))))
ep.WriteRaw(pageMargin(1))
ep.WriteRaw(printSpeed(13))
// ditherer := ditherers[1]
// log.Println("Using ditherer type: " + ditherer.Type)
// dg := ditherer.Monochrome(img, 1.18)
rasterConv := &raster.Converter{
MaxWidth: 186, // == math.Round((29.0 / 80.0) * 512)) == 29mm sticker roll
Threshold: 0.1, //pre-dithered images are black or white anyway, so this can be extremely low as there won't be any in-between colors (default: 0.5)
}
for i := 0; i < 2; i++ {
rasterConv.Print(img, ep)
ep.Cut()
}
//ep.WriteRaw([]byte{12}) // FF: in Page mode, prints all the data in the print buffer collectively and switches from Page mode to Standard mode.
//ep.Formfeed()
ep.End()
}