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;
//}
|