需要引入
using LitJson;
using ZXing;
using ZXing.QrCode;
IEnumerator Photo()
{
AudioManager._instance.PlayAudio(1);
yield return new WaitForEndOfFrame();
Texture2D texture2D = new Texture2D(1920, 1080);
texture2D.ReadPixels(new Rect(0, 0, 1920, 1080), 0, 0, false);
texture2D.Apply();
GameState._gameState = gameState.end;
rawImage.GetChild(1).GetComponent<RawImage>().texture = texture2D;
// JPGEncoder encoder = new JPGEncoder(texture2D, 50);
byte[] textureByte = texture2D.EncodeToJPG();
// encoder.doEncoding();
// byte[] textureByte = encoder.GetBytes();
WWWForm wWWForm = new WWWForm();
wWWForm.AddBinaryData("photo", textureByte);
WWW www = new WWW(Url, wWWForm);
// UnityEngine.Networking.UnityWebRequest www = UnityEngine.Networking.UnityWebRequest.Post(Url, wWWForm);
yield return www;
Debug.Log(www.text);
JsonData jsonData = JsonMapper.ToObject(www.text);
string photopath = jsonData["msg"].ToString();//獲取網址信息
Texture2D ImgQR = new Texture2D(256, 256);
ImgQR.SetPixels32(Encode(photopath, 256, 256));//將網址轉成二維碼
ImgQR.Apply();
rawImage.GetChild(2).GetComponent<RawImage>().texture = ImgQR;
Hand.SetActive(true);
//yield return new WaitForSeconds(30);
//if (GameState._gameState ==gameState.end&& isback==false)
//{
// GameState._gameState = gameState.start;
//}
}
public Color32[] Encode(string textForEncoding, int width, int height)
{
BarcodeWriter writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
writer.Options = new QrCodeEncodingOptions();
writer.Options.Height = height;
writer.Options.Width = width;
writer.Options.Margin = 0;
return writer.Write(textForEncoding);
}