您的位置:首页 > 其它

场景拼图工具开发

2015-07-10 15:46 309 查看
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ResPublish
{
class Program
{
//迭代计数器
public static int count=0;
/// <summary>
/// 函数入口
/// 程晗0709
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
//获取所有步骤的返回值
Result result = new Result(true,"");
//运行程序时,检测 texture路径,图片路径是否存在
result = checkAll();
if (!result.isSuccess)
{
Console.WriteLine(result.msg);
Console.ReadLine();
}
//程序调用texture生成图片

//递归开始
result = doTexture(Settings.Default.imagePath+@"\temp0");
Console.WriteLine("操作成功");
Console.ReadLine();
}

/// <summary>
/// 运行程序时,检测 texture路径,图片路径是否存在
/// 程晗0709
/// </summary>
/// <returns></returns>
public static Result checkAll()
{
//验证texture是否存在
if (!Directory.Exists(Settings.Default.texturePath)) {
return new Result(false,"texture路径错误");
};
//验证image是否存在
if (!Directory.Exists(Settings.Default.imagePath)) {
return new Result(false, "image路径错误");
}
//检测图片是否超过大小,并存入temp0中
//创建temp0文件夹
if (!Directory.Exists(Settings.Default.imagePath + @"\temp0"))
{
// Create the directory it does not exist.
Directory.CreateDirectory(Settings.Default.imagePath + @"\temp0");
}
else
{
Directory.Delete(Settings.Default.imagePath + @"\temp0", true);
Directory.CreateDirectory(Settings.Default.imagePath + @"\temp0");
}
//将样本图片存入文件夹
//读取文件夹中所有文件 存入数组中
string[] dir = Directory.GetFiles(Settings.Default.imagePath);
//将数组中的文件存入新的文件夹中。
Image img;
//验证是否存在图片
bool isImage = false;
Regex reg = new Regex("jpg|png");
for (int i = 0; i < dir.Length; i++)
{
//判断是否是图片
if (reg.IsMatch(dir[i])) {
isImage = true;
img = System.Drawing.Image.FromFile(dir[i]);
if (img.Width > 2048 || img.Height > 2048) {
return new Result(false,dir[i]+"图片大小超标!");
}
img.Dispose();
//取出文件名
string imageName = dir[i].Substring(Settings.Default.imagePath.Length + 1);
System.IO.File.Copy(dir[i], Settings.Default.imagePath + @"\temp0\" + imageName);
//剪切文件
//File.Move(listStr[i], path + @"\temp" + count);
}
}
//验证是否存在图片
if (!isImage)
{
return new Result(false, "选中文件夹不存在图片,无法拼图");
}
return new Result(true, "");
}

/// <summary>
/// 程序调用texture生成图片
/// 程晗0710
/// </summary>
/// <returns></returns>
public static Result doTexture(string imagePath)
{
Result result= new Result(true,"");
//新建进程并生成拼图
try
{
//进程所在文件夹
ProcessStartInfo startInfo = new ProcessStartInfo(Settings.Default.texturePath + @"TexturePacker.exe");
//重定向输出
startInfo.RedirectStandardOutput = true;
//错误输出
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
//进程不打开窗口
startInfo.CreateNoWindow = true;
//打开进程的时候附带的命令行
startInfo.Arguments = @"--data e:\" + count + @".plist --format cocos2d --sheet e:\" + count + ".png --size-constraints POT --dither-fs-alpha " + " " + Settings.Default.imagePath + @"\temp" + count;
//开启进程,并读取信息
result.msg=Process.Start(startInfo).StandardError.ReadToEnd();
//正则匹配error 并且切片大于1
Regex reg = new Regex("error");
if (reg.IsMatch(result.msg))
{
//溢出一次就会迭代一次
result.isSuccess = false;
//将错误信息拆分为单词
string[] errorWord = result.msg.Split(' ');
//提取多出来的图片
List<string> images = new List<string>();
Regex regImage=new Regex("jpg|png");
for (int i = 0; i < errorWord.Length; i++) {
if (regImage.IsMatch(errorWord[i])) {
images.Add(errorWord[i]);
}
}
//调用移动文件函数
moveImage(images, Settings.Default.imagePath + @"\temp" + count);
}
}
catch (Exception e ) {
result.isSuccess = false;
result.msg += e;
}
//回调本身
if (count > 0)
{
result = doTexture(Settings.Default.imagePath + @"\temp" + --count);
}
return result;
}

/// <summary>
/// 将数组中的文件剪切到指定文件夹中。
/// </summary>
/// <param name="images">多出的文件</param>
/// <param name="path">指定文件路径</param>
/// <returns></returns>
public static Result moveImage(List<string> images, string path)
{
count++;
//读取文件夹中所有文件 存入数组中
string[] dir = Directory.GetFiles(path);
//正则匹配获取图片
Regex reg=new Regex("jpg|png");
//循环正则,如果有后缀则存入list中
List<string> listStr=new List<string>();
for(int i=0;i<dir.Length;i++){
if(reg.IsMatch(dir[i])){
listStr.Add(dir[i]);
}
}
//创建临时文件夹,如果存在就则删除后再创建空文件夹
try
{
if (!Directory.Exists(Settings.Default.imagePath + @"\temp" + count))
{
// Create the directory it does not exist.
Directory.CreateDirectory(Settings.Default.imagePath + @"\temp" + count);
}
else
{
Directory.Delete(Settings.Default.imagePath + @"\temp" + count, true);
Directory.CreateDirectory(Settings.Default.imagePath + @"\temp" + count);
}
//如果list中没有多出图片,则存入temp并调用拼图函数
string regStr = "";
foreach (string str in images) {
regStr = str + "|" + regStr;
}
Regex regOver = new Regex(regStr.Substring(0,regStr.Length-1));
for (int i = 0; i < listStr.Count; i++)
{
//如果当前图片不存在,则复制入新的文件夹
if (regOver.IsMatch(listStr[i]))
{
//取出文件名
string imageName = listStr[i].Substring(path.Length+1);
System.IO.File.Move(listStr[i], Settings.Default.imagePath + @"\temp" + count + @"\" + imageName);
//剪切文件
//File.Move(listStr[i], path + @"\temp" + count);
}
}
}
catch (Exception e) {
return new Result(false,e.ToString());

a99d
}
//调用拼图函数对temp中的图片进行拼图
doTexture(Settings.Default.imagePath + @"\temp" + count);
return new Result(true, "");
}
}

/// <summary>
/// 结果类
/// </summary>
public class Result {
public bool isSuccess { get; set; }
public string msg { get; set; }

public Result(bool isSuccess, string msg) {
this.isSuccess = isSuccess;
this.msg = msg;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: