using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
public class Texture2PDF : MonoBehaviour
{
///
/// 插件默认路径
///
private static string pluginPath = string.Format("{0}\\LGUtilityCore\\Texture2PDF-Plugins\\Image2PDF.exe", Application.streamingAssetsPath.Replace("/", "\\"));
///
/// 测试导出的Texture2D
///
public Texture2D[] textureArray;
///
/// 测试输出PDF路径
///
public string outPutPdfPath;
// Start is called before the first frame update
void Start()
{
//图片至少2个,最后1个参数必须是pdf文件路径
//Texture2PDF.Convert2PDF(new string[] { "D:\\temp\\pdf-test\\1.jpg", "D:\\temp\\pdf-test\\2.jpg", "D:\\temp\\pdf-test\\3.jpg", "D:\\temp\\pdf-test\\4.jpg" }, "d:\\test_output.pdf");
//Convert2PDF(textureArray, outPutPdfPath);
}
///
/// 执行转换PDF
///
/// 图片路径集合,至少2个
/// 输出PDF文件路径
/// /// 回调函数(bool,string)
public static void Convert2PDF(string[] image_paths, string pdf_path, Action callBack = null)
{
if (image_paths.Length < 2)
return;
StringBuilder args = new StringBuilder();
foreach (var path in image_paths)
args.AppendFormat("{0} ", path);
args.Append(pdf_path);
RunExternalProcess(pluginPath, args.ToString(), callBack);
}
///
/// 执行转换PDF
///
/// Texture2D数组
/// 输出PDF文件路径
/// 回调函数(bool,string)
public static void Convert2PDF(Texture2D[] textures, string pdf_path, Action callBack = null)
{
if (textures.Length < 2)
return;
List image_paths = new List();
// Get the path to the temporary directory
string tempPath = Path.GetTempPath();
foreach (var tex in textures)
{
string filePath = Path.Combine(tempPath, string.Format("{0}.jpg", Guid.NewGuid().ToString()));
SaveTextureAsJPG(tex, filePath);
image_paths.Add(filePath);
}
Convert2PDF(image_paths.ToArray(), pdf_path, (bool status,string result) =>
{
//删除临时图片文件
image_paths.ForEach(x =>
{
try
{
System.IO.File.Delete(x);
}
catch (Exception e)
{
Debug.LogError(e.Message);
}
});
if(callBack!=null)
callBack(status, result);
});
}
///
/// 保存Texture2D为jpg文件
///
/// Texture2D
/// jpg保存路径
private static void SaveTextureAsJPG(Texture2D texture, string filePath)
{
// Encode texture to JPG format
byte[] bytes = DeCompress(texture).EncodeToJPG();
// Write the bytes to a file
File.WriteAllBytes(filePath, bytes);
}
///
/// 转换,避免Read/Write读取不到等异常
///
/// 原始Texture2D
/// 临时Texture2D
public static Texture2D DeCompress(Texture2D source)
{
RenderTexture renderTex = RenderTexture.GetTemporary(
source.width,
source.height,
0,
RenderTextureFormat.Default,
RenderTextureReadWrite.Linear);
Graphics.Blit(source, renderTex);
RenderTexture previous = RenderTexture.active;
RenderTexture.active = renderTex;
Texture2D readableText = new Texture2D(source.width, source.height);
readableText.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0);
readableText.Apply();
RenderTexture.active = previous;
RenderTexture.ReleaseTemporary(renderTex);
return readableText;
}
///
/// 执行插件程序
///
/// 插件路径
/// 插件参数,图片至少2个,最后1个参数必须是pdf文件路径
///
private static void RunExternalProcess(string exePath, string arguments, Action callBack = null)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = exePath;
process.StartInfo.Arguments = arguments;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
bool callBackStatus = true;
string output = "", error = "";
while (!process.HasExited)
{
output = process.StandardOutput.ReadToEnd();
error = process.StandardError.ReadToEnd();
if (!string.IsNullOrEmpty(error))
{
callBackStatus = false;
}
}
process.WaitForExit();
if (null != callBack)
callBack(callBackStatus, callBackStatus ? output : error);
}
}