ShanxiKnowledgeBase/SXElectricity Marketing 2.0/Assets/LG-Utility/Texture2PDF/Scripts/Texture2PDF.cs

168 lines
5.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
public class Texture2PDF : MonoBehaviour
{
/// <summary>
/// 插件默认路径
/// </summary>
private static string pluginPath = string.Format("{0}\\LGUtilityCore\\Texture2PDF-Plugins\\Image2PDF.exe", Application.streamingAssetsPath.Replace("/", "\\"));
/// <summary>
/// 测试导出的Texture2D
/// </summary>
public Texture2D[] textureArray;
/// <summary>
/// 测试输出PDF路径
/// </summary>
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);
}
/// <summary>
/// 执行转换PDF
/// </summary>
/// <param name="image_paths">图片路径集合至少2个</param>
/// <param name="pdf_path">输出PDF文件路径</param>
/// /// <param name="callBack">回调函数(bool,string)</param>
public static void Convert2PDF(string[] image_paths, string pdf_path, Action<bool, string> 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);
}
/// <summary>
/// 执行转换PDF
/// </summary>
/// <param name="textures">Texture2D数组</param>
/// <param name="pdf_path">输出PDF文件路径</param>
/// <param name="callBack">回调函数(bool,string)</param>
public static void Convert2PDF(Texture2D[] textures, string pdf_path, Action<bool, string> callBack = null)
{
if (textures.Length < 2)
return;
List<string> image_paths = new List<string>();
// 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);
});
}
/// <summary>
/// 保存Texture2D为jpg文件
/// </summary>
/// <param name="texture">Texture2D</param>
/// <param name="filePath">jpg保存路径</param>
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);
}
/// <summary>
/// 转换避免Read/Write读取不到等异常
/// </summary>
/// <param name="source">原始Texture2D</param>
/// <returns>临时Texture2D</returns>
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;
}
/// <summary>
/// 执行插件程序
/// </summary>
/// <param name="exePath">插件路径</param>
/// <param name="arguments">插件参数图片至少2个最后1个参数必须是pdf文件路径</param>
/// <returns></returns>
private static void RunExternalProcess(string exePath, string arguments, Action<bool, string> 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);
}
}