img.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright 2016 The go-vgo Project Developers. See the COPYRIGHT
  2. // file at the top-level directory of this distribution and at
  3. // https://github.com/go-vgo/robotgo/blob/master/LICENSE
  4. //
  5. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  6. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  7. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  8. // option. This file may not be copied, modified, or distributed
  9. // except according to those terms.
  10. package robotgo
  11. import (
  12. "image"
  13. "os/exec"
  14. "unsafe"
  15. "github.com/vcaesar/imgo"
  16. )
  17. // DecodeImg decode the image to image.Image and return
  18. func DecodeImg(path string) (image.Image, string, error) {
  19. return imgo.DecodeFile(path)
  20. }
  21. // OpenImg open the image return []byte
  22. func OpenImg(path string) ([]byte, error) {
  23. return imgo.ImgToBytes(path)
  24. }
  25. // Read read the file return image.Image
  26. func Read(path string) (image.Image, error) {
  27. return imgo.Read(path)
  28. }
  29. // Save create a image file with the image.Image
  30. func Save(img image.Image, path string, quality ...int) error {
  31. return imgo.Save(path, img, quality...)
  32. }
  33. // SaveImg save the image by []byte
  34. func SaveImg(b []byte, path string) error {
  35. return imgo.SaveByte(path, b)
  36. }
  37. // SavePng save the image by image.Image
  38. func SavePng(img image.Image, path string) error {
  39. return imgo.SaveToPNG(path, img)
  40. }
  41. // SaveJpeg save the image by image.Image
  42. func SaveJpeg(img image.Image, path string, quality ...int) error {
  43. return imgo.SaveToJpeg(path, img, quality...)
  44. }
  45. // ToByteImg convert image.Image to []byte
  46. func ToByteImg(img image.Image, fm ...string) []byte {
  47. return imgo.ToByte(img, fm...)
  48. }
  49. // ToStringImg convert image.Image to string
  50. func ToStringImg(img image.Image, fm ...string) string {
  51. return string(ToByteImg(img, fm...))
  52. }
  53. // StrToImg convert base64 string to image.Image
  54. func StrToImg(data string) (image.Image, error) {
  55. return imgo.StrToImg(data)
  56. }
  57. // ByteToImg convert []byte to image.Image
  58. func ByteToImg(b []byte) (image.Image, error) {
  59. return imgo.ByteToImg(b)
  60. }
  61. // ImgSize get the file image size
  62. func ImgSize(path string) (int, int, error) {
  63. return imgo.GetSize(path)
  64. }
  65. // Width return the image.Image width
  66. func Width(img image.Image) int {
  67. return img.Bounds().Max.X
  68. }
  69. // Height return the image.Image height
  70. func Height(img image.Image) int {
  71. return img.Bounds().Max.Y
  72. }
  73. // RGBAToBitmap convert the standard image.RGBA to Bitmap
  74. func RGBAToBitmap(r1 *image.RGBA) (bit Bitmap) {
  75. bit.Width = r1.Bounds().Size().X
  76. bit.Height = r1.Bounds().Size().Y
  77. bit.Bytewidth = r1.Stride
  78. src := ToUint8p(r1.Pix)
  79. bit.ImgBuf = src
  80. bit.BitsPixel = 32
  81. bit.BytesPerPixel = 32 / 8
  82. return
  83. }
  84. // ImgToBitmap convert the standard image.Image to Bitmap
  85. func ImgToBitmap(m image.Image) (bit Bitmap) {
  86. bit.Width = m.Bounds().Size().X
  87. bit.Height = m.Bounds().Size().Y
  88. pix, stride, _ := imgo.EncodeImg(m)
  89. bit.Bytewidth = stride
  90. src := ToUint8p(pix)
  91. bit.ImgBuf = src
  92. //
  93. bit.BitsPixel = 32
  94. bit.BytesPerPixel = 32 / 8
  95. return
  96. }
  97. // ToUint8p convert the []uint8 to uint8 pointer
  98. func ToUint8p(dst []uint8) *uint8 {
  99. src := make([]uint8, len(dst)+10)
  100. for i := 0; i <= len(dst)-4; i += 4 {
  101. src[i+3] = dst[i+3]
  102. src[i] = dst[i+2]
  103. src[i+1] = dst[i+1]
  104. src[i+2] = dst[i]
  105. }
  106. ptr := unsafe.Pointer(&src[0])
  107. return (*uint8)(ptr)
  108. }
  109. // ToRGBAGo convert Bitmap to standard image.RGBA
  110. func ToRGBAGo(bmp1 Bitmap) *image.RGBA {
  111. img1 := image.NewRGBA(image.Rect(0, 0, bmp1.Width, bmp1.Height))
  112. img1.Pix = make([]uint8, bmp1.Bytewidth*bmp1.Height)
  113. copyToVUint8A(img1.Pix, bmp1.ImgBuf)
  114. img1.Stride = bmp1.Bytewidth
  115. return img1
  116. }
  117. func val(p *uint8, n int) uint8 {
  118. addr := uintptr(unsafe.Pointer(p))
  119. addr += uintptr(n)
  120. p1 := (*uint8)(unsafe.Pointer(addr))
  121. return *p1
  122. }
  123. func copyToVUint8A(dst []uint8, src *uint8) {
  124. for i := 0; i <= len(dst)-4; i += 4 {
  125. dst[i] = val(src, i+2)
  126. dst[i+1] = val(src, i+1)
  127. dst[i+2] = val(src, i)
  128. dst[i+3] = val(src, i+3)
  129. }
  130. }
  131. // GetText get the image text by tesseract ocr
  132. //
  133. // robotgo.GetText(imgPath, lang string)
  134. func GetText(imgPath string, args ...string) (string, error) {
  135. var lang = "eng"
  136. if len(args) > 0 {
  137. lang = args[0]
  138. if lang == "zh" {
  139. lang = "chi_sim"
  140. }
  141. }
  142. body, err := exec.Command("tesseract", imgPath,
  143. "stdout", "-l", lang).Output()
  144. if err != nil {
  145. return "", err
  146. }
  147. return string(body), nil
  148. }