把图像加载到base64,然后用Texture2D加载,再添加到图像库。
private Dictionary<string, GameObject> mPrefabs = new Dictionary<string, GameObject>();
void Start()
{
mPrefabs.Add("Dragen", Resources.Load("DragenBox") as GameObject);
mPrefabs.Add("Cube", Resources.Load("CubeBox") as GameObject);
StartCoroutine(initRuntimeImage());
btnCapture = GameObject.Find("btnCapture").GetComponent<Button>();
btnCapture.onClick.AddListener(OnBtnCaptureClick);
}
public void OnBtnCaptureClick()
{
StartCoroutine(OnScreenShot());
}
IEnumerator OnScreenShot()
{
ScreenShotCount++;
yield return new WaitForEndOfFrame();
DebugLog.Log("OnScreenShot");
try {
Texture2D tTexture2D = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
tTexture2D.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
tTexture2D.Apply();
if (ImgTrackedmanager.referenceLibrary is MutableRuntimeReferenceImageLibrary mutableLibrary)
{
try
{
TestState = mutableLibrary.ScheduleAddImageWithValidationJob(tTexture2D, "ScreenShot" + ScreenShotCount, 0.1f);
DebugLog.Log("Adding runtime img " + TestState.status.ToString() + "--" + TestState.jobHandle.IsCompleted.ToString());
isAddingImage = true;
}
catch (InvalidOperationException e)
{
DebugLog.Log($"AddImage Error {e.Message}");
}
}
}
catch (Exception e)
{
DebugLog.Log("error :" + e.Message);
}
}
IEnumerator initRuntimeImage()
{
DebugLog.Log("initRuntimeImage wait Coroutine");
yield return null;
mPrefabs.Add("Sphere", Resources.Load("SphereBox") as GameObject);
string base64 = "";
AddImage(base64, "Sphere");
}
void AddImage(string base64, string name)
{
if (ImgTrackedmanager.referenceLibrary is MutableRuntimeReferenceImageLibrary mutableLibrary)
{
try
{
Texture2D texture2D = Base64StringToImage(base64); ;
TestState = mutableLibrary.ScheduleAddImageWithValidationJob(texture2D, name, 0.1f);
Debug.Log("Adding runtime img " + TestState.status.ToString() + "--" + TestState.jobHandle.IsCompleted.ToString());
}
catch (InvalidOperationException e)
{
Debug.Log($"AddImage Error {e.Message}");
}
}
}
void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
{
foreach (var trackedImage in eventArgs.added)
{
StartCoroutine(OnImagesChanged(trackedImage));
}
}
IEnumerator OnImagesChanged(ARTrackedImage referenceImage)
{
while (referenceImage.trackingState == TrackingState.None)
{
yield return new WaitForSeconds(0.1f);
Debug.Log($"trackingState: " + referenceImage.trackingState.ToString());
}
Debug.Log($"OnImagesChanged: " + referenceImage.referenceImage.name + ", trackingState:" + referenceImage.trackingState);
Debug.Log("location:(" + referenceImage.transform.position.x + "," + referenceImage.transform.position.y + "," + referenceImage.transform.position.z + ")");
Instantiate(mPrefabs[referenceImage.referenceImage.name], referenceImage.transform.position, referenceImage.transform.rotation);
}
public static Texture2D Base64StringToImage(string basestr)
{
Texture2D t2d = new Texture2D(1, 1);
try
{
byte[] arr = Convert.FromBase64String(basestr);
t2d.LoadImage(arr);
Debug.Log("转换成功");
}
catch
{
Debug.Log("转换失败");
}
return t2d;
}
参考: ARFoundation之路-图像跟踪 AR tracked image manager
|