using System.Collections.Generic;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using Baidu.Aip.Ocr;
using System.IO;
using UnityEngine.SceneManagement;
//using UnityEditor;
using Newtonsoft.Json.Linq;//接收返回结果 JObject
using LitJson;
using EasyAR;
//百度图片文字识别接口调用——非子萧
public class Colosring_ImageOCR : MonoBehaviour
{
public Text debugInfo; // 显示debug信息
public Text tex_TW; // 显示图片文字识别的结果
public Text tex_wait;
// public Image image;
private string sss="";
private Ocr client; // 用来调用百度AI接口
private JObject result; // 接收返回的结果
//private string ApiKey = "G7h0xBIkf4xfR6NE1mfpZvF3";//此处填写自己申请的key//BaiDuTW
//private string SecretKey = "gKsyBaTKp8GDAeiqG6eNrHkxbbsT4MAN";//此处填写自己申请的key//BaiDuTW
//private string ApiKey = "mRWSUYWXHN5lv35Sq88LCuon";//此处填写自己申请的key//TXYTTL
//private string SecretKey = "vxYRUwDGyY9Sw5o3VUutQr4T2THLOKsM ";//此处填写自己申请的key//TXYTTL
private string ApiKey = "oLESEZHegPqolS46wqQCH3em";//此处填写自己申请的key//Animal
private string SecretKey = "URS2FrHzjsMA6UXLRRt0FznuPrEzcN3m ";//此处填写自己申请的key//Animal
private byte[] bytes;
public GameObject Earth;
public GameObject Sun;
public GameObject Moon;
public GameObject Mercury;//水星
public GameObject Venus;//金星
public GameObject Mars;//火星
public GameObject Jupiter;//木星
public GameObject Saturn;//土星
//public GameObject targetImage;
Camera cam;
RenderTexture renderTexture;
ImageTargetBaseBehaviour targetBehaviour;
private void Awake()
{
//网页端身份安全验证失败,我们需要在程序运行时手动添加安全证书,在Awake方法中加入
System.Net.ServicePointManager.ServerCertificateValidationCallback +=
delegate (object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
System.Security.Cryptography.X509Certificates.X509Chain chain,
System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
};
client = new Ocr(ApiKey, SecretKey);
Earth.SetActive(false);
Sun.SetActive(false);
Moon.SetActive(false);
Mercury.SetActive(false);
Venus.SetActive(false);
Mars.SetActive(false);
Jupiter.SetActive(false);
Saturn.SetActive(false);
Debug.Log("对象!-Awake");
targetBehaviour = GetComponent<ImageTargetBaseBehaviour>();
}
private void Update()
{
tex_TW.text=sss;
if (tex_TW.text=="地球"|| tex_TW.text == "地"||tex_TW.text == "土" || tex_TW.text == "地王求" || tex_TW.text == "土也王求" || tex_TW.text == "土也球")
{
// SceneManager.LoadScene("Earth");
Earth.SetActive(true);
Sun.SetActive(false);
Moon.SetActive(false);
Mercury.SetActive(false);
Venus.SetActive(false);
Mars.SetActive(false);
Jupiter.SetActive(false);
Saturn.SetActive(false);
}
else if (tex_TW.text == "太阳" || tex_TW.text == "大阳" || tex_TW.text == "大日" || tex_TW.text == "太日" )
{
// SceneManager.LoadScene("Earth");
Sun.SetActive(true);
Earth.SetActive(false);
Moon.SetActive(false);
Mercury.SetActive(false);
Venus.SetActive(false);
Mars.SetActive(false);
Jupiter.SetActive(false);
Saturn.SetActive(false);
}
else if (tex_TW.text == "月亮" || tex_TW.text == "月" || tex_TW.text == "亮" || tex_TW.text == "月球")
{
// SceneManager.LoadScene("Earth");
Moon.SetActive(true);
Earth.SetActive(false);
Sun.SetActive(false);
Mercury.SetActive(false);
Venus.SetActive(false);
Mars.SetActive(false);
Jupiter.SetActive(false);
Saturn.SetActive(false);
}
else if (tex_TW.text == "水星" || tex_TW.text == "水" || tex_TW.text == "水日" || tex_TW.text == "水生")
{
// SceneManager.LoadScene("Earth");
Mercury.SetActive(true);
Earth.SetActive(false);
Sun.SetActive(false);
Moon.SetActive(false);
Venus.SetActive(false);
Mars.SetActive(false);
Jupiter.SetActive(false);
Saturn.SetActive(false);
}
else if (tex_TW.text == "金星" || tex_TW.text == "金" || tex_TW.text == "金日" || tex_TW.text == "金生")
{
// SceneManager.LoadScene("Earth");
Venus.SetActive(true);
Earth.SetActive(false);
Sun.SetActive(false);
Moon.SetActive(false);
Mercury.SetActive(false);
Mars.SetActive(false);
Jupiter.SetActive(false);
Saturn.SetActive(false);
}
else if (tex_TW.text == "火星" || tex_TW.text == "火" || tex_TW.text == "火日" || tex_TW.text == "火生")
{
// SceneManager.LoadScene("Earth");
Mars.SetActive(true);
Earth.SetActive(false);
Sun.SetActive(false);
Moon.SetActive(false);
Mercury.SetActive(false);
Venus.SetActive(false);
Jupiter.SetActive(false);
Saturn.SetActive(false);
}
else if(tex_TW.text == "木星" || tex_TW.text == "木" || tex_TW.text == "木日" || tex_TW.text == "木生")
{
// SceneManager.LoadScene("Earth");
Jupiter.SetActive(true);
Earth.SetActive(false);
Sun.SetActive(false);
Moon.SetActive(false);
Mercury.SetActive(false);
Venus.SetActive(false);
Mars.SetActive(false);
Saturn.SetActive(false);
}
else if(tex_TW.text == "土星" || tex_TW.text == "土" || tex_TW.text == "土日" || tex_TW.text == "土生")
{
// SceneManager.LoadScene("Earth");
Saturn.SetActive(true);
Earth.SetActive(false);
Sun.SetActive(false);
Moon.SetActive(false);
Mercury.SetActive(false);
Venus.SetActive(false);
Mars.SetActive(false);
Jupiter.SetActive(false);
}
else if(tex_TW.text =="")
{
// SceneManager.LoadScene("Earth");
Saturn.SetActive(false);
Earth.SetActive(false);
Sun.SetActive(false);
Moon.SetActive(false);
Mercury.SetActive(false);
Venus.SetActive(false);
Mars.SetActive(false);
Jupiter.SetActive(false);
}
}
//拍照
void photo()
{
if (!cam)
{
GameObject go = new GameObject("__cam");
cam = go.AddComponent<Camera>();
go.transform.parent = transform.parent;
cam.hideFlags = HideFlags.HideAndDontSave;
}
cam.CopyFrom(Camera.main);
cam.depth = 0;
cam.cullingMask = 31;
if (!renderTexture)
{
renderTexture = new RenderTexture(Screen.width, Screen.height, -50);
}
cam.targetTexture = renderTexture;
cam.Render();
RenderTexture prev = RenderTexture.active;
RenderTexture.active = renderTexture;
Texture2D png = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.ARGB32, false);
png.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
//cameraImage.Apply();
bytes = png.EncodeToPNG();
StartCoroutine(StartOCR());//解析百度文字识别结果
//result = client.GeneralBasic(bytes);//百度文字识别(普通)
//result = client.Accurate(bytes);//百度文字识别(高精度)
Debug.Log("obj_Accurate_BAIDUAI");
//把图片保存在本地
if (!Directory.Exists(Application.dataPath + "/Resources"))
Directory.CreateDirectory(Application.dataPath + "/Resources");
FileStream file = File.Open(Application.dataPath + "/Resources/"+ "camera_image" + ".png", FileMode.Create);
BinaryWriter writer = new BinaryWriter(file);
writer.Write(bytes);
file.Close();
//——把图片保存在本地
//System.IO.File.WriteAllBytes(Application.dataPath+ "/Resources/camera_image.png", bytes);
UnityEditor.AssetDatabase.Refresh();//刷新,使刚创建的图片立刻导入。接下来才可以被使用
Texture2D.DestroyImmediate(png);
png = null;
RenderTexture.active = prev;
}
// 高精度图片文字识别..//每天限量50次,超过错误码17;
public void Accurate()
{
var path = Application.dataPath + "/Resources/xxocr.jpg";//要识别的文字图片=路径+文件名
// var path = Application.persistentDataPath + "/" + "xxocr" + ".jpg";
var image = File.ReadAllBytes(path);//要识别的文字图片
var result = client.Accurate(image);//百度文字识别
JsonData jd0 = JsonMapper.ToObject(result.ToString());//json解析json数据
JsonData jd1 = JsonMapper.ToObject(result.ToString())["words_result"];
int s2 = (int)jd0["words_result_num"];
for (int i = 0; i < s2; i++)
{
tex_TW.text = "";
sss = "";
sss += jd1[i]["words"].ToString();
}
Debug.Log(result.ToString());
debugInfo.text = result.ToString();
Debug.Log("高精度识别");
}
////拍照,存贮照片在本地
//public void Photograph()
//{
// // StartCoroutine(GetTexture2D());//协程——获取摄像头图像,并把图片保存在本地
// Debug.Log("拍照!");
// // img_Show.enabled = true;//拍照要识别的图片
//}
//获取摄像头图像,并把图片保存在本地
//public void GetTexture2D()
IEnumerator GetTexture2D()
//void GetTexture2D()
{
// yield return new WaitForSeconds(2f);
yield return new WaitForEndOfFrame();
Texture2D t = new Texture2D(1600, 800);//新,实例化一张空白长1600,宽800的图片,//摄像头纹理大小
t.ReadPixels(new Rect(160, 140, 1600, 800), 0, 0, false);//新空白图片的像素为,这个区域中的像素(摄像头在屏幕中的区域的图像)、
//参数1:160为摄像头纹理左下角距离屏幕左边的距离;参数2:140为摄像头纹理左下角距离屏幕底部的距离;参数3:1600f为摄像头纹理宽度,参数4:800f为摄像头纹理高度,
//t.Apply();//把像素应用在图片
//把图片数据转换为byte数组
bytes = t.EncodeToPNG();
// result = client.GeneralBasic(bytes);//百度文字识别(普通)
//StartCoroutine(StartOCR());
//然后保存为图片
// System.IO.File.WriteAllBytes(Application.persistentDataPath+"/"+strTempTime + ".jpg", byt);//把图片保存在本地,文件夹中,名字为当前时间.jpg
//System.IO.File.WriteAllBytes(Application.persistentDataPath + "/" + "xxocr" + ".jpg", byt);//把图片保存在本地,文件夹中,名字为当前时间.jpg
// System.IO.File.WriteAllBytes(Application.dataPath + "/Resources/xxocr.jpg", byt);//把图片保存在本地,Resources文件夹中,名字为当前时间.jpg
// UnityEditor.AssetDatabase.Refresh();//刷新,使刚创建的图片立刻导入。接下来才可以被使用
// img_Show.material.mainTexture = Resources.Load("xxocr", typeof(Texture2D)) as Texture2D;//把拍的照片显示出来;//Resources加载不需要后缀
Debug.Log("照片保存在本地!");
}
//百度文字识别
IEnumerator StartOCR()
{
// Accurate();//百度文字识别//高精度识别,每天限制50次
//var path = Application.dataPath + "/Resources/xxocr.jpg";//要识别的文字图片=路径+文件名
// var path = Application.persistentDataPath + "/" + "xxocr" + ".jpg";
//var image = File.ReadAllBytes(path);//要识别的文字图片
//var result = client.Accurate(image);//百度文字识别(高精度)
result = client.GeneralBasic(bytes);//百度文字识别(普通)
yield return result;
JsonData jd0 = JsonMapper.ToObject(result.ToString());//json解析json数据
JsonData jd1 = JsonMapper.ToObject(result.ToString())["words_result"];
int s2 = (int)jd0["words_result_num"];
for (int i = 0; i < s2; i++)
{
tex_TW.text = "";
sss = "";
sss += jd1[i]["words"].ToString();
}
Debug.Log(result.ToString());
debugInfo.text = result.ToString();
Debug.Log("高精度识别");
//yield return null;
}
IEnumerator Wait()
{
yield return new WaitForSeconds(2f);
Debug.Log("对象-Wait2s");
}
//识别图识别后,拍照,识别文字
private void Start()
{
// debugInfo.text = "";
// tex_TW.text = "";
// GetTexture2D();
// StartCoroutine(GetTexture2D());//拍照,保存在本地
// StartCoroutine(StartOCR());//百度文字识别
Debug.Log("对象-Start");
}
private void OnEnable()
{
//StartCoroutine(photo());//等待2s后拍照
photo();
// StartCoroutine(GetTexture2D());//拍照,保存在本地
// StartCoroutine(StartOCR());//百度文字识别
//StartOCR();
Debug.Log("对象-OnEnable");
}
private void OnDisable()
{
debugInfo.text = "";
tex_TW.text = "";
Earth.SetActive(false);
Sun.SetActive(false);
Moon.SetActive(false);
Mercury.SetActive(false);
Venus.SetActive(false);
Mars.SetActive(false);
Jupiter.SetActive(false);
Saturn.SetActive(false);
Debug.Log("对象-OnDisable");
}
//private void OnDestroy()
//{
// debugInfo.text = "";
// tex_TW.text = "";
// Earth.SetActive(false);
// Sun.SetActive(false);
// Moon.SetActive(false);
// Mercury.SetActive(false);
// Venus.SetActive(false);
// Mars.SetActive(false);
// Jupiter.SetActive(false);
// Saturn.SetActive(false);
// Debug.Log("对象-OnDestroy");
//}
////重新开始识别;拍照,识别
//public void ResPhotoOCR()
//{
// debugInfo.text = "";
// tex_TW.text = "";
// StartCoroutine(Wait());
// StartCoroutine(GetTexture2D());//协程——获取摄像头图像,并把图片保存在本地
// //GetTexture2D();
// //StartCoroutine(StartOCR());//百度识别
// Debug.Log("重新识别");
//}
}
网友评论