IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> unity图片相似度2 -> 正文阅读

[游戏开发]unity图片相似度2

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

public class ContrastManager : MonoBehaviour
{

public static ContrastManager _instance;
/// <summary>
/// 添加被对比的图片
/// </summary>
public Texture2D LocalizeImage;
/// <summary>
/// 添加服务器比对图片
/// </summary>
public List<Texture2D> ServerImage = new List<Texture2D>();
/// <summary>
/// 存储哈希值与所对应的图片
/// </summary>
public Dictionary<string, Texture2D> NewHash = new Dictionary<string, Texture2D>();
private SimilarPhoto similarPhoto;
private ScrollRect MyScrollView;
public string NewHashValue;
void Start()
{
    _instance = this;
    MyScrollView = GameObject.Find("MyScrollView").GetComponent<ScrollRect>();
    similarPhoto = SimilarPhoto._Instance;
    //GetAllImageName(Application.persistentDataPath);
    AddLocalizeImage(SetReadWriteTrue(LocalizeImage));
    AddServerImag();
    AddHash();
}
/// <summary>
/// 用于手机端查找直接路径,通过图片名字加到链表内
/// </summary>
/// <param name="_MyPath"></param>
public void GetAllImageName(string _MyPath)
{
    Dictionary<string, string> imageFileDic = GetAllImagesFiles(_MyPath);
    foreach (var path in imageFileDic)
    {
        Texture2D tex = NativeGallery.LoadImageAtPath(path.Value, -1);
        tex.name = path.Key;
        ServerImage.Add(tex);
    }
    AddServerImag();
}
/// <summary>
/// 计算本地图片哈希值
/// </summary>
public void AddLocalizeImage(Texture2D texture)
{
    similarPhoto.SimilarPhotoHash.Clear();
    similarPhoto.StartUpLoadImageData(texture, 10);
    NewHashValue = similarPhoto.SimilarPhotoHash[0];
    Debug.Log("添加被对比的图片");
}
/// <summary>
/// 计算服务器图片链表哈希值
/// </summary>
public void AddServerImag()
{
    similarPhoto.SimilarPhotoHash.Clear();
    for (int i = 0; i < ServerImage.Count; i++)
    {
        similarPhoto.StartUpLoadImageData(SetReadWriteTrue(ServerImage[i]), 10);
        if (!NewHash.ContainsKey(similarPhoto.SimilarPhotoHash[i]))
        {
            NewHash.Add(similarPhoto.SimilarPhotoHash[i], ServerImage[i]);
        }
    }
    Debug.Log("添加比对图片");
}
/// <summary>
/// 添加本地与服务器图片对比
/// </summary>
public void AddHash()
{
    Texture2D texture2D = null;
    foreach (var item in NewHash)
    {
        NewHash.TryGetValue(item.Key, out texture2D);
        float f = similarPhoto.computeDistance(NewHashValue, item.Key) * 100;
        InstanceGO(texture2D, "相似度:" + f.ToString());
    }
    Debug.Log("对比相似度");
}
/// <summary>
/// 实例化
/// </summary>
/// <param name="texture2"></param>
/// <param name="value"></param>
public void InstanceGO(Texture2D texture2, string value)
{
    GameObject go = (GameObject)Instantiate(Resources.Load("images"), MyScrollView.content);
    RawImage Mytexture = go.transform.Find("RawImage").GetComponent<RawImage>();
    Text Mytexts = go.transform.Find("Text").GetComponent<Text>();
    Mytexture.texture = texture2;
    Mytexts.text = value;

}
/// <summary>
/// 获取全部的图片
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private Dictionary<string, string> GetAllImagesFiles(string path)
{
    Dictionary<string, string> imagefilesDic = new Dictionary<string, string>();
    foreach (var file in Directory.GetFiles(path))
    {
        if (Path.GetExtension(file) == ".jpg" || Path.GetExtension(file) == ".AES" || Path.GetExtension(file) == ".png")
            imagefilesDic.Add(Path.GetFileNameWithoutExtension(file), file);
    }
    return imagefilesDic;
}
/// <summary>
/// 获取图片文件
/// </summary>
/// <param name="_MyPath"></param>
/// <returns></returns>
public Dictionary<string, string> GetImageFiles(string _MyPath)
{
    if (!Directory.Exists(_MyPath))
        return new Dictionary<string, string>();
    return GetAllImagesFiles(_MyPath);
}

public Texture2D SetReadWriteTrue(Texture2D texture)
{
    // 创建一个与纹理大小相同的临时渲染纹理
    RenderTexture tmp = RenderTexture.GetTemporary(
                 texture.width,
                 texture.height,
                 0,
                 RenderTextureFormat.Default,
                 RenderTextureReadWrite.Linear);
    Graphics.Blit(texture, tmp);
    // 备份当前设置的渲染纹理
    RenderTexture previous = RenderTexture.active;
    // 将当前渲染纹理设置为我们创建的临时纹理
    RenderTexture.active = tmp;
    // 创建一个新的可读的Texture2D来复制像素到它
    Texture2D myTexture2D = new Texture2D(texture.width, texture.height);
    // 将渲染纹理中的像素复制到新纹理中
    myTexture2D.ReadPixels(new Rect(0, 0, tmp.width, tmp.height), 0, 0);
    myTexture2D.Apply();
    //重置活动渲染纹理
    RenderTexture.active = previous;
    // 释放临时渲染纹理
    RenderTexture.ReleaseTemporary(tmp);
    Debug.Log("是否:" + myTexture2D.isReadable);

    return myTexture2D;
    // “myTexture2D”现在拥有与“纹理”相同的像素,而且是可读的。
}

//RenderTexture转Texture2D
//public Texture2D RenderTexture2Texture2D(RenderTexture rt)
//{
//    RenderTexture preRT = RenderTexture.active;
//    RenderTexture.active = rt;
//    Texture2D tex = new Texture2D(rt.width, rt.height, TextureFormat.ARGB32, false);
//    tex.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
//    tex.Apply();
//    RenderTexture.active = preRT;
//    Debug.Log(tex.isReadable);
//    return tex;
//}

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2021-09-08 11:04:24  更:2021-09-08 11:05:06 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/21 20:31:57-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码