Tanoda
GalleryController.cs
Go to the documentation of this file.
1using System;
2using System.IO;
3using UnityEngine;
4
5public class GalleryController : MonoBehaviour
6{
7#if !UNITY_EDITOR && UNITY_IOS
8 [System.Runtime.InteropServices.DllImport( "__Internal" )]
9 private static extern void SaveImageToAlbum(string path);
10 #endif
11
12 public string GalleryPath { get; private set; } = string.Empty;
13
14 private void Start()
15 {
16 if (Application.platform == RuntimePlatform.Android) SetGalleryPath();
17 }
18
19 private void SetGalleryPath()
20 {
21 if (Application.platform == RuntimePlatform.Android && Application.platform == RuntimePlatform.Android)
22 {
23 var androidJavaClass = new AndroidJavaClass("com.ToolBar.EasyWebCam.EasyWebCam");
24 androidJavaClass.CallStatic("getGalleryPath", gameObject.name);
25 }
26 }
27
32 private void receiveGalleryPath(string androidPath)
33 {
34 GalleryPath = androidPath;
35 if (!Directory.Exists(GalleryPath)) Directory.CreateDirectory(GalleryPath);
36 }
37
38 public void Refresh(string path)
39 {
40 if (Application.platform == RuntimePlatform.Android)
41 {
42 var androidJavaClass = new AndroidJavaClass("com.ToolBar.EasyWebCam.EasyWebCam");
43 androidJavaClass.CallStatic("Refresh", path);
44 }
45 }
46
47 public static void SaveImageToGallery(Texture2D qrCode)
48 {
49#if (!UNITY_WEBPLAYER) && (!UNITY_WEBGL)
50 var bytes = qrCode.EncodeToJPG();
51 if (Application.platform == RuntimePlatform.Android)
52 {
53 var mediactr = FindObjectOfType<GalleryController>();
54 if (mediactr != null)
55 {
56 var aPath = mediactr.GalleryPath;
57 if (!string.IsNullOrEmpty(aPath))
58 {
59 aPath = Path.Combine(aPath, "QRCode_" + DateTime.Now.ToFileTime() + ".jpg");
60 try
61 {
62 File.WriteAllBytes(aPath, bytes);
63 Debug.Log("Saved : " + aPath);
64 }
65 catch
66 {
67 }
68
69 mediactr.Refresh(aPath);
70 }
71 else
72 {
73 Debug.Log("Saved Code image Failed !");
74 }
75 }
76 }
77 else if (Application.platform == RuntimePlatform.IPhonePlayer)
78 {
79 var iPath = Application.persistentDataPath;
80 iPath = Path.Combine(iPath, "QRCode" + DateTime.Now.ToFileTime() + ".jpg");
81 try
82 {
83 File.WriteAllBytes(iPath, bytes);
84 Debug.Log("Saved : " + iPath);
85 }
86 catch
87 {
88 Debug.Log("Saved Code image Failed !");
89 }
90
91#if !UNITY_EDITOR && UNITY_IOS
92 SaveImageToAlbum( iPath );
93 Debug.Log( "Saving to Pictures: " + Path.GetFileName( iPath ) );
94#endif
95 }
96 else if (Application.platform == RuntimePlatform.WindowsPlayer
97 || Application.platform == RuntimePlatform.OSXPlayer
98 || Application.platform == RuntimePlatform.LinuxPlayer
99 || Application.platform == RuntimePlatform.OSXEditor
100 || Application.platform == RuntimePlatform.WindowsEditor)
101 {
102 var text = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
103 text = Path.Combine(text, "QRCode" + DateTime.Now.ToFileTime() + ".jpg");
104 try
105 {
106 File.WriteAllBytes(text, bytes);
107 Debug.Log("Saved : " + text);
108 }
109 catch
110 {
111 }
112 }
113#endif
114 }
115}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19