找字函数

制作字库

1.打开vscode的触摸精灵插件

image.png

2.右键点击设备列表,选择触摸抓抓

image.png

3.在打开的抓抓页面中点击字库生成

image.png

4.取字的颜色填写到二值化下面的框中,调整二值化的精度(范围0-100),将图片二值化

image.png

如果字体有多种颜色,颜色之间用 , 分割 例如: 0xFFFFFF,0xAAAAAA,0x333333

5.点击添加,输入字键,点击字值框,然后框选数据,添加完成后点击生成字库
字库制作.gif

函数:fontInit() 初始化字库

函数说明 : 初始化字库/加载字库。
函数方法 : fontInit(数组 table);
返回值 : 无

参数类型说明
table数组字库数组

示例

function main()
    local fontLib = {
            {'触', {0xFFFFFF}, 90, 'iVBORw0KGgoAAAANSUhEUgAAABcAAAAcCAYAAACK7SRjAAAAsUlEQVR4AbXBAYrCMAAAwd2Q/395z4IFEbWtXmbshkUGCw0WGryg8g2VR4OLVM4afEHljMFFFRUqRyZ3Ko9UdhXPKlQq3pncVexUKo5UfDJYaHCBispZgwsqNipnDC6q2KgcGbyhoqLyrGKj8snkicqu4p2KI5MblV2FSsWvJjcVv1LZqGwqJl9QeadiNzlJZVfxisqjyUkVVw0WGiw0WGhyp7Kr+A92wyKDhQYLDRb6A6nTR1D8mGLiAAAAAElFTkSuQmCC'}, 
            {'摸', {0xFFFFFF}, 90, 'iVBORw0KGgoAAAANSUhEUgAAABUAAAAZCAYAAADe1WXtAAAAi0lEQVR4AbXBQWrDMAAAwV2h/3956x4KxogkrqMZO/Blgw0GH1L51GCDyUHlEyrvVEwOFWcqFe+oVFwNFipUXlGpWBlsMNhAIF6ouFL5VbFiB75ssMFgg8mJyn9V/JksVNyhcjbYYLDBZEHliclCxR0qZ4OLiqcGFypPTRZUnpgsVNyhcmYHvmywwQ8PTzQ4ee0K3AAAAABJRU5ErkJggg=='}
            }
    fontInit(fontLib)
end

注意事项:

只有初始化字库后才能找字 识字

函数:fontFindText() 找字

函数说明 : 通过触摸精灵字库返回文字坐标。
函数方法 : fontFindText(文本型 str,整数型 x1,整数型y1, 整数型 x2, 整数型 y2,整数型 degree);
返回值 : 整数型 x, y

参数类型说明
str文本型要查找的字
x1整数型欲寻找的区域左上角的X坐标
y1整数型欲寻找的区域左上角的Y坐标
x2整数型欲寻找的区域右下角的X坐标
y2整数型欲寻找的区域右下角的Y坐标
degree整数型精度,范围:1 ~ 100,数值越大精度越高,100为完全匹配,默认为100
返回值类型说明
x整数型找到的坐标X值,未找到返回 -1
y整数型找到的坐标Y值,未找到返回 -1

示例

function main()
    local fontLib = {
            {'触', {0xFFFFFF}, 90, 'iVBORw0KGgoAAAANSUhEUgAAABcAAAAcCAYAAACK7SRjAAAAsUlEQVR4AbXBAYrCMAAAwd2Q/395z4IFEbWtXmbshkUGCw0WGryg8g2VR4OLVM4afEHljMFFFRUqRyZ3Ko9UdhXPKlQq3pncVexUKo5UfDJYaHCBispZgwsqNipnDC6q2KgcGbyhoqLyrGKj8snkicqu4p2KI5MblV2FSsWvJjcVv1LZqGwqJl9QeadiNzlJZVfxisqjyUkVVw0WGiw0WGhyp7Kr+A92wyKDhQYLDRb6A6nTR1D8mGLiAAAAAElFTkSuQmCC'}, 
            {'摸', {0xFFFFFF}, 90, 'iVBORw0KGgoAAAANSUhEUgAAABUAAAAZCAYAAADe1WXtAAAAi0lEQVR4AbXBQWrDMAAAwV2h/3956x4KxogkrqMZO/Blgw0GH1L51GCDyUHlEyrvVEwOFWcqFe+oVFwNFipUXlGpWBlsMNhAIF6ouFL5VbFiB75ssMFgg8mJyn9V/JksVNyhcjbYYLDBZEHliclCxR0qZ4OLiqcGFypPTRZUnpgsVNyhcmYHvmywwQ8PTzQ4ee0K3AAAAABJRU5ErkJggg=='}
            }
    fontInit(fontLib)
    x, y = fontFindText("触", 100, 100, 200, 200)
    logDebug(string.format("找到的坐标:%s,%s",x,y))
end

注意事项:

1.初始化字库运行一次即可,不用每次找字都初始化字库.
2.范围填写 0,0,-1,-1 可全屏查找

函数:fontFindTextEx() 找字返回数组

函数说明 : 通过触摸精灵字库返回屏幕上所有符合的文字坐标。
函数方法 : fontFindTextEx(文本型 str,整数型 x1,整数型y1, 整数型 x2, 整数型 y2,整数型 degree);
返回值 : 数组 table

参数类型说明
str文本型要查找的字
x1整数型欲寻找的区域左上角的X坐标
y1整数型欲寻找的区域左上角的Y坐标
x2整数型欲寻找的区域右下角的X坐标
y2整数型欲寻找的区域右下角的Y坐标
degree整数型精度,范围:1 ~ 100,数值越大精度越高,100为完全匹配,默认为100
返回值类型说明
table数组返回的坐标数组

示例

function main()
    inspect = require("inspect")
    local fontLib = {
            {'触', {0xFFFFFF}, 90, 'iVBORw0KGgoAAAANSUhEUgAAABcAAAAcCAYAAACK7SRjAAAAsUlEQVR4AbXBAYrCMAAAwd2Q/395z4IFEbWtXmbshkUGCw0WGryg8g2VR4OLVM4afEHljMFFFRUqRyZ3Ko9UdhXPKlQq3pncVexUKo5UfDJYaHCBispZgwsqNipnDC6q2KgcGbyhoqLyrGKj8snkicqu4p2KI5MblV2FSsWvJjcVv1LZqGwqJl9QeadiNzlJZVfxisqjyUkVVw0WGiw0WGhyp7Kr+A92wyKDhQYLDRb6A6nTR1D8mGLiAAAAAElFTkSuQmCC'}, 
            {'摸', {0xFFFFFF}, 90, 'iVBORw0KGgoAAAANSUhEUgAAABUAAAAZCAYAAADe1WXtAAAAi0lEQVR4AbXBQWrDMAAAwV2h/3956x4KxogkrqMZO/Blgw0GH1L51GCDyUHlEyrvVEwOFWcqFe+oVFwNFipUXlGpWBlsMNhAIF6ouFL5VbFiB75ssMFgg8mJyn9V/JksVNyhcjbYYLDBZEHliclCxR0qZ4OLiqcGFypPTRZUnpgsVNyhcmYHvmywwQ8PTzQ4ee0K3AAAAABJRU5ErkJggg=='}
            }
    fontInit(fontLib)
    points = fontFindTextEx("触", 100, 100, 200, 200)
    logDebug(inspect(points))
end

返回的table坐标:

{
  {
    x = 10,
    y = 20 
  },
  {
    x = 100,
    y = 200
  }
  ...
}

注意事项:

1.初始化字库运行一次即可,不用每次找字都初始化字库.
2.范围填写 0,0,-1,-1 可全屏查找

函数:fontOcrText() 识字

函数说明 : 将指定区域的图像识别成文字。
函数方法 : fontOcrText(整数型 x1,整数型y1, 整数型 x2, 整数型 y2,整数型 degree);
返回值 : 文本型 text 数组 table

参数类型说明
x1整数型欲识别的区域左上角的X坐标
y1整数型欲识别的区域左上角的Y坐标
x2整数型欲识别的区域右下角的X坐标
y2整数型欲识别的区域右下角的Y坐标
degree整数型精度,范围:1 ~ 100,数值越大精度越高,100为完全匹配,默认为100
返回值类型说明
text文本型识别后得到的答案, 失败的话返回空字符串
table数组返回的坐标等信息

返回的table坐标:

{
  {
    char = "触",    -- char 代表识别的内容
    x = 10,         -- x 坐标
    y = 20          -- y 坐标
  },
  {
    char = "摸", 
    x = 100, 
    y = 200
  }
  ...
}

示例:

function main()
    inspect = require("inspect")
    local fontLib = {
            {'触', {0xFFFFFF}, 90, 'iVBORw0KGgoAAAANSUhEUgAAABcAAAAcCAYAAACK7SRjAAAAsUlEQVR4AbXBAYrCMAAAwd2Q/395z4IFEbWtXmbshkUGCw0WGryg8g2VR4OLVM4afEHljMFFFRUqRyZ3Ko9UdhXPKlQq3pncVexUKo5UfDJYaHCBispZgwsqNipnDC6q2KgcGbyhoqLyrGKj8snkicqu4p2KI5MblV2FSsWvJjcVv1LZqGwqJl9QeadiNzlJZVfxisqjyUkVVw0WGiw0WGhyp7Kr+A92wyKDhQYLDRb6A6nTR1D8mGLiAAAAAElFTkSuQmCC'}, 
            {'摸', {0xFFFFFF}, 90, 'iVBORw0KGgoAAAANSUhEUgAAABUAAAAZCAYAAADe1WXtAAAAi0lEQVR4AbXBQWrDMAAAwV2h/3956x4KxogkrqMZO/Blgw0GH1L51GCDyUHlEyrvVEwOFWcqFe+oVFwNFipUXlGpWBlsMNhAIF6ouFL5VbFiB75ssMFgg8mJyn9V/JksVNyhcjbYYLDBZEHliclCxR0qZ4OLiqcGFypPTRZUnpgsVNyhcmYHvmywwQ8PTzQ4ee0K3AAAAABJRU5ErkJggg=='}
            }
    fontInit(fontLib)
    text, info = fontOcrText(0, 0, -1, -1)
    logDebug(text)
    logDebug(inspect(info))
end
注意:识字的时候返回是按照y从小到大,y相同的时候按照x从小到大的顺序来返回的.
如果同一行字体大小不一样的话返回的顺序可能会跟实际不符,可使用下面的代码重新排序
EAF565A4B38AA34B0BEB677BEFE54AA5.png
返回:i查ph找one
function main()
  fontLib = {
    {
      "查",
      {0xFFFFFF},
      90,
      "iVBORw0KGgoAAAANSUhEUgAAABEAAAASCAYAAAC9+TVUAAAAe0lEQVR4Aa3BAYrCMAAAwd2Q/395rwELIuHU2hk78IbKqeKVQPxoVuyo/KfiNNhQWSoqThUnldPghcpSsVNRsagsk4PKs4p3KlRUBOKh4orJoWJRuWJU/GqyUfEJlWVwg8mGyjcGN5hsVHxCZRncYLKh8o3Jk4orBjf4A9t+OSNeKN/qAAAAAElFTkSuQmCC"
    },
    {
      "找",
      {0xFFFFFF},
      90,
      "iVBORw0KGgoAAAANSUhEUgAAABEAAAAPCAYAAAACsSQRAAAAgUlEQVR4AZXBwQ2DMADAQDvK/iu7zQMJRZTCnX3xpbJUXFFZKnZT5UxlqXhqVqgcKu6o7CZfFSo7lX8qJi9V7CY3Kg4qvwwuqOwqrqgMNipPqSxDReWs4heV3WBT8YTKUjEqKt5QORu8UHFWsUweUjmrOEz+UPlncEPlrKJiUTl8AMfNQC63TMJ6AAAAAElFTkSuQmCC"
    },
    {
      "i",
      {0xFFFFFF},
      90,
      "iVBORw0KGgoAAAANSUhEUgAAAAcAAAAUCAYAAABBECfmAAAASklEQVR4AX3BwQnAMBADwZVI/y0rMfjgPtGM8uGHWSSxmUsShySGuZJwJGGYJQmbKUxhFklIYpjCFKYwhSlMYQpTmMIUpnhYkrC95TsQKek67QkAAAAASUVORK5CYII="
    },
    {
      "p",
      {0xFFFFFF},
      90,
      "iVBORw0KGgoAAAANSUhEUgAAAAoAAAARCAYAAADkIz3lAAAASElEQVR4AY3BQQ6AMBDDQG/U/3/ZqBLcgGZmAPmg8hhACoubypuZYQsHKlsohVIohVI4mBm2AaSwOFDZFjeVP6EUSqEUSqF0AZ1REhppas3gAAAAAElFTkSuQmCC"
    },
    {
      "h",
      {0xFFFFFF},
      90,
      "iVBORw0KGgoAAAANSUhEUgAAAAgAAAAQCAYAAAArij59AAAASElEQVR4AYXBQQrAIAADwU3o/7+cKih4qM2MgAzcmMIUpjAfJLE9HCSxSWISEJYkbJKYzJKEUxImMyThxvxIgilMYQpTmMIULywAFBksRhEnAAAAAElFTkSuQmCC"
    },
    {
      "o",
      {0xFFFFFF},
      90,
      "iVBORw0KGgoAAAANSUhEUgAAAAoAAAAPCAYAAADd/14OAAAAVUlEQVR4AY3BAQqAMBAEsZmj///y2kKFIiqXmImGoqloKj6onAYHlZPKbbCpLEk4qSzFpLIk4SkJS9GQhKKp2JLwp9hU/hRNRYNKMSVhUfkyeFB5cwGBFxYsDaQ/uQAAAABJRU5ErkJggg=="
    },
    {
      "n",
      {0xFFFFFF},
      90,
      "iVBORw0KGgoAAAANSUhEUgAAAAsAAAAMCAYAAAC0qUeeAAAASUlEQVR4AZXByQ0DQQwEseqG80+5PPMQsB8fIuPBB0lQGS+OJFwqVxJGEi6VeCRhqDwlYZQfVEZ5UPmmLJQ/qFxloSyUhbJQFt6DoB0Oyd3cJAAAAABJRU5ErkJggg=="
    },
    {
      "e",
      {0xFFFFFF},
      90,
      "iVBORw0KGgoAAAANSUhEUgAAAA4AAAAMCAYAAABSgIzaAAAAXklEQVR4AZ3BAQqDMADAwKT0/1/OVlCQ4sB5Z1/8oFJxZ7JRuVI5VZwmFypLxU5FpWIZvDQ4qCwVdyoWlWXw0mSj8sRkU/HE4KXBoeIfg43KHRWV0+CiYlFRWVRUdh9RZx8xNL+2lgAAAABJRU5ErkJggg=="
    }
  }

  fontInit(fontLib)
  text, info = fontOcrText(390, 360, 550, 400)
  logDebug(text)
  logDebug(sortByLine(info))
end

function sortByLine(info)
  table.sort(
    info,
    function(a, b)
      return a.x < b.x
    end
  )

  local text = ""
  for k, v in pairs(info) do
    text = text .. v.char
  end
  return text
end

image.png
重新排序后顺序和实际相符.

注意事项:

1.初始化字库运行一次即可,不用每次找字都初始化字库.
2.范围填写 0,0,-1,-1 可全屏查找.
3.只能识别初始化字库中的字.