Unity扫描仪画鱼制作—1.获取扫描仪新增的图并识别

1.获取扫描仪的新增图。通过判断扫描仪扫描的文件夹内文件数量来判断。

 void Update()
    {
        if (Time.time - m_CheckNewImageTime > 5f)
        {
            CheckNewImage();
            m_CheckNewImageTime = Time.time;

        }
    }
 
    /// <summary>
    /// 检查新图片
    /// </summary>
    private void CheckNewImage()
    {
        if (!m_ShouldCheckNewImag) return;
        List<string> allImageNamesInFolder = GetAllImageNamesInFolder(srcFolder);
        foreach (var text in allImageNamesInFolder)
        {
            if (!m_ImageDic.ContainsKey(text) && !text.Contains("resize") && !text.Contains("sign")
            && !text.Contains("code") && !text.Contains("seed") && !text.Contains("mark"))
            {
                LoadNewImage(text);
                //  print(text+"    text");
                break;
            }
        }
    }

 

 


2.识别新增的图是哪一种鱼

原理:通过识别图片四个区域的颜色填充来判断是哪种鱼。

 

如图,四个识别点位是 1,1,0,1 可判断为海豚。

 /// <summary>
    /// 识别哪种图片
    /// </summary>
    /// <param name="bitmap"></param>
    /// <returns></returns>
    private uint GetCharactorType(Bitmap bitmap)
    {
        uint num = IsBlack(bitmap, new Vector2(70f, 85f));
        uint num2 = IsBlack(bitmap, new Vector2(965f, 85f));
        uint num3 = IsBlack(bitmap, new Vector2(70f, 925f));
        uint num4 = IsBlack(bitmap, new Vector2(965f, 925f));
        Debug.Log(string.Concat(new object[]{
            "u0:  ",
            num,
            " u1 : ",
            num2,
            " u2 : ",
            num3,
            " u3 : ",
            num4
        }
        ));
        uint result = num | num2 << 1 | num3 << 2 | num4 << 3;
        Debug.Log("-------------------------");
        Debug.Log("GetCharactorType: " + result.ToString("X2"));
        return result;

    }
    /// <summary>
    /// 看这个点的20范围 像素是否是黑色的
    /// </summary>
    /// <param name="bitmap"></param>
    /// <param name="pos"></param>
    /// <returns></returns>
    private uint IsBlack(Bitmap bitmap, Vector2 pos)
    {
        if (pos.x < 0f || pos.x > (float)bitmap.Size.Width || pos.y < 0f || pos.y > (float)bitmap.Size.Height)
        {
            Debug.Log("ImageScan IsBlack out of Size@");
            return 0u;
        }
        int num = 20;
        int num2 = 0;
        int num3 = 1;
        for (int i = -num; i < num; i++)
        {
            for (int j = -num; j < num; j++)
            {
                System.Drawing.Color pixel = bitmap.GetPixel((int)(pos.x + (float)(i * num3)), (int)(pos.y + (float)(j * num3)));
                if ((float)(pixel.R + pixel.G + pixel.B) / 3f < 127.5f)
                {
                    num2++;
                }
                bitmap.SetPixel((int)(pos.x + (float)(i * num3)), (int)(pos.y + (float)(j * num3)), System.Drawing.Color.Red);
            }
        }
        float num4 = (float)num2 / ((float)(num * num) * 4f);
        //   Debug.Log("IsBlack:" + num4);
        return (num4 <= 0.3f) ? 0u : 1u;
    }

识别四个点后 判断

 public static CreaterType GetCreaterType(uint mark)
    {
        switch (mark)
        {
            case 1u:
                return CreaterType.eType_HAIGUI;
            case 2u:
                return CreaterType.eType_HAIMA;
            case 3u:
                return CreaterType.eType_HUDIEYU;
            case 5u:
                return CreaterType.eType_JINGQIANGYU;
            case 6u:
                return CreaterType.eType_CHOUYU;
            case 7u:
                return CreaterType.eType_HETUN;
            case 8u:
                return CreaterType.eType_WUZEI;
            case 9u:
                return CreaterType.eType_JIANYU;
            case 10u:
                return CreaterType.eType_SHAYU;
            case 11u:
                return CreaterType.eType_HAITUN;
        }
        return CreaterType.e_None;
    }

 

 

 

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值