changhong_newclient/wb_unity_pro/Assets/Scripts/Platform/ShareQRCodePicture.cs

167 lines
6.0 KiB
C#
Raw Normal View History

using LuaInterface;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEngine;
using ZXing;
using ZXing.QrCode;
using ZXing.Common;
static public class QRCodePicture
{
public static void Take(string url, string nick, string uid, LuaInterface.LuaFunction callback, bool noRefresh = false)
{
GameApplication.Instance.StartCoroutine(_QRCodeShare(url, nick, uid, callback, noRefresh));
}
static IEnumerator _QRCodeShare(string url, string nick, string uid, LuaInterface.LuaFunction callback, bool noRefresh)
{
var targeImage = Application.persistentDataPath + "/qrcode" + uid + ".jpg";
var thumbImage = Application.persistentDataPath + "/qrcode" + uid + "_thumb.jpg";
var addr = url + "?nick=" + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(nick)) + "&uid=" + uid;
if (noRefresh && File.Exists(targeImage) && File.Exists(thumbImage))
{
yield return null;
callback.Call(1);
callback.Dispose();
}
else
{
if (File.Exists(targeImage))
File.Delete(targeImage);
else if (File.Exists(thumbImage))
File.Delete(thumbImage);
downloadPicture(targeImage, thumbImage, addr, callback);
yield return null;
}
}
static void downloadPicture(string picture, string thumbImage, string url, LuaInterface.LuaFunction callback)
{
BestHTTP.HTTPManager.SendRequest(url, (request, response) =>
{
if (request.State == BestHTTP.HTTPRequestStates.Finished && request.Response.IsSuccess)
{
var data = request.Response.Data;
var texture = new Texture2D(100, 100);
texture.LoadImage(data);
var longLen = Math.Max(texture.width, texture.height);
var shortLen = Math.Min(texture.width, texture.height);
var width = texture.width == longLen ? 240 : 240f * shortLen / longLen;
var height = texture.height == longLen ? 240 : 240f * shortLen / longLen;
var thumb = ScaleTexture(texture, (int)width, (int)height);
var pictureData = texture.EncodeToJPG(80);
var thumbData = thumb.EncodeToJPG(60);
File.WriteAllBytes(picture, pictureData);
File.WriteAllBytes(thumbImage, thumbData);
}
else
{
callback.Call(0);
callback.Dispose();
return;
}
callback.Call(1);
callback.Dispose();
});
}
static Texture2D ScaleTexture(Texture2D source, int targetWidth, int targetHeight)
{
Texture2D result = new Texture2D(targetWidth, targetHeight, source.format, false);
float incX = (1.0f / (float)targetWidth);
float incY = (1.0f / (float)targetHeight);
for (int i = 0; i < result.height; ++i)
{
for (int j = 0; j < result.width; ++j)
{
Color newColor = source.GetPixelBilinear((float)j / (float)result.width, (float)i / (float)result.height);
result.SetPixel(j, i, newColor);
}
}
result.Apply();
return result;
}
public static Texture2D GenerateQRcode(string str, int width, int height)
{
MultiFormatWriter writer = new MultiFormatWriter();
Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>();
hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.Add(EncodeHintType.MARGIN, 0);
BitMatrix bitMatrix = writer.encode(str, BarcodeFormat.QR_CODE, width, height, hints);
var texture = new Texture2D(width, height, TextureFormat.RGB24, false);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
if (bitMatrix[y, x])
{
texture.SetPixel(x, y, Color.black);
}
else
{
texture.SetPixel(x, y, Color.white);
}
}
}
texture.Apply();
return texture;
}
public static void CombanitePicture(Texture2D tex1, Texture2D tex2, int posx, int posy, string filename)
{
int width = tex2.width;
int height = tex2.height;
var tem = new Texture2D(tex1.width, tex1.height,TextureFormat.RGB24,false);
for (int y = 0; y < tex1.height; y++)
{
for (int x = 0; x < tex1.width; x++)
{
tem.SetPixel(x, y, tex1.GetPixel(x, y));
}
}
for (int y = posy; y < posy + height; y++)
{
for (int x = posx; x < posx + width; x++)
{
tem.SetPixel(x, y, tex2.GetPixel(x - posx, y - posy));
}
}
tem.Apply();
byte[] bytes = tem.EncodeToJPG();
string path = System.IO.Path.Combine(Application.persistentDataPath, filename);
File.WriteAllBytes(path, bytes);
var longLen = Math.Max(tem.width, tem.height);
var shortLen = Math.Min(tem.width, tem.height);
var twidth = tem.width == longLen ? 240 : 240f * shortLen / longLen;
var theight = tem.height == longLen ? 240 : 240f * shortLen / longLen;
var thumb = ScaleTexture(tem, (int)twidth, (int)theight);
var thumbData = thumb.EncodeToJPG(60);
File.WriteAllBytes(Application.persistentDataPath + "/" + Path.GetFileNameWithoutExtension(filename) + "_thumb" + Path.GetExtension(filename), thumbData);
}
private static Color32[] Encode(string textForEncoding, int width, int height)
{
var writer = new BarcodeWriter
{
Format = BarcodeFormat.QR_CODE,
Options = new QrCodeEncodingOptions
{
Height = height,
Width = width
}
};
return writer.Write(textForEncoding);
}
}