123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- using GFGEditor;
- using GFGGame;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using UnityEngine;
- using Color = System.Drawing.Color;
- using Graphics = System.Drawing.Graphics;
- public class ImagesClip : MonoBehaviour
- {
- public static void CutImageWhitePart(string FilePath, string TargetPath, string fileName, int WhiteBarRate = 2)
- {
- Debug.Log("fileName:" + fileName);
- Bitmap bmp = new Bitmap(FilePath);
- int top = 0, left = 0;
- int right = bmp.Width, bottom = bmp.Height;
- int width = bmp.Width, height = bmp.Height;
- int xLeft = 0, yTop = 0;
- //寻找最上面的标线,从左(0)到右,从上(0)到下
- for (int i = 0; i < bmp.Height; i++)//行
- {
- bool find = false;
- for (int j = 0; j < bmp.Width; j++)//列
- {
- Color c = bmp.GetPixel(j, i);
- if (IsBlank(c))
- {
- top = i;
- find = true;
- yTop = i;
- break;
- }
- }
- if (find) break;
- }
- //寻找最左边的标线,从上(top位)到下,从左到右
- for (int i = 0; i < bmp.Width; i++)//列
- {
- bool find = false;
- for (int j = top; j < bmp.Height; j++)//行
- {
- Color c = bmp.GetPixel(i, j);
- if (IsBlank(c))
- {
- left = i;
- find = true;
- xLeft = i;
- break;
- }
- }
- if (find) break; ;
- }
- //寻找最下边标线,从下到上,从左到右
- for (int i = bmp.Height - 1; i >= 0; i--)//行
- {
- bool find = false;
- for (int j = left; j < bmp.Width; j++)//列
- {
- Color c = bmp.GetPixel(j, i);
- if (IsBlank(c))
- {
- bottom = i;
- find = true;
- break;
- }
- }
- if (find) break;
- }
- //寻找最右边的标线,从上到下,从右往左
- for (int i = bmp.Width - 1; i >= 0; i--)//列
- {
- bool find = false;
- for (int j = 0; j < bottom; j++)//行
- {
- Color c = bmp.GetPixel(i, j);
- if (IsBlank(c))
- {
- right = i;
- find = true;
- break;
- }
- }
- if (find) break;
- }
- bmp = Cut(bmp, left, top, right - left + 1, bottom - top + 1);
- if (bmp != null)
- {
- bmp.Save(TargetPath + fileName);
- WriteImagePos(TargetPath, fileName, xLeft + (bmp.Width / 2) - (width / 2), yTop + (bmp.Height / 2) - (height / 2));
- bmp.Dispose();
- Debug.Log("换装部件裁剪完成");
- }
- }
- public static Bitmap Cut(Bitmap b, int StartX, int StartY, int iWidth, int iHeight)
- {
- if (b == null)
- {
- return null;
- }
- int w = b.Width;
- int h = b.Height;
- if (StartX >= w || StartY >= h)
- {
- return null;
- }
- if (StartX + iWidth > w)
- {
- iWidth = w - StartX;
- }
- if (StartY + iHeight > h)
- {
- iHeight = h - StartY;
- }
- try
- {
- Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format32bppArgb);
- Graphics g = Graphics.FromImage(bmpOut);
- g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
- g.Dispose();
- return bmpOut;
- }
- catch
- {
- return null;
- }
- }
- /// <summary>
- /// 判断空白与否
- /// </summary>
- /// <param name="c"></param>
- /// <returns></returns>
- public static bool IsBlank(Color c)
- {
- // Debug.Log("r:" + c.R + " G:" + c.G + " b:" + c.B + " a:" + c.A);
- if ((c.R <= 255 || c.G <= 255 || c.B <= 255) && c.A > 0)
- return true;
- else return false;
- }
- public static List<string> ReadImagePosition()
- {
- string targetPath = ImportArtResTool.DressUpTargetPath; ;
- string sourcePath = ImportArtResTool.DressUpTargetPath;
- string[] includeExtensionNames = new string[] { ".png", ".jpg" };
- List<string> result = new List<string>();
- if (Directory.Exists(sourcePath))
- {
- var files = Directory.GetFiles(sourcePath);
- foreach (var file in files)
- {
- if (file.Trim() == file)
- {
- string extensionName = Path.GetExtension(file);//获取扩展名
- if (includeExtensionNames == null || Array.IndexOf(includeExtensionNames, extensionName) >= 0)
- {
- string fileName = Path.GetFileName(file);//返回指定路径字符串的文件名和扩展名。
- int xImage = 0;
- int yImage = 0;
- ReadImagePos(targetPath, fileName, out xImage, out yImage);
- }
- }
- else
- {
- Debug.LogError($"请检查文件名 {file}");
- }
- }
- }
- return result;
- }
- private static void WriteImagePos(string targetPath, string imageName, int iamgeX, int imageY)
- {
- string savePath = Path.Combine(targetPath, imageName.Split('.')[0] + ".bytes");
- using (var writer = new BinaryWriter(File.Open(savePath, FileMode.Create)))
- {
- writer.Write(iamgeX);
- writer.Write(imageY);
- writer.Close();
- }
- }
- private static void ReadImagePos(string targetPath, string imageName, out int imageX, out int imageY)
- {
- string savePath = Path.Combine(targetPath, imageName.Split('.')[0] + ".bytes");
- try
- {
- byte[] data = File.ReadAllBytes(savePath);
- var reader = new BinaryReader(new MemoryStream(data));
- imageX = reader.ReadInt32();
- imageY = reader.ReadInt32();
- reader.Close();
- Debug.Log(imageName + " iamgeX:" + imageX + " iamgeY:" + imageY);
- }
- catch
- {
- imageX = 0;
- imageY = 0;
- Debug.Log(imageName + "未写入位置数据");
- }
- }
- public static void WriteSourceImagesMD5(Dictionary<string, string> resMd5, string saveName)
- {
- string savePath = Path.Combine(ImportArtResTool.Md5FilePath, saveName);
- Debug.Log("savePath:" + savePath + " saveName:" + saveName);
- using (var writer = new BinaryWriter(File.Open(savePath, FileMode.Create)))
- {
- ICollection keys = resMd5.Keys;
- foreach (string key in keys)
- {
- writer.Write(key);
- writer.Write(resMd5[key]);
- }
- writer.Close();
- // 稍微延迟关闭文件流
- System.Threading.Thread.Sleep(100); // 可以根据需要调整延迟时间
- }
- }
- public static Dictionary<string, string> ReadSourceImagesMD5(string saveName)
- {
- string savePath = Path.Combine(ImportArtResTool.Md5FilePath, saveName);
- Dictionary<string, string> resMd5 = new Dictionary<string, string>();
- try
- {
- byte[] data = File.ReadAllBytes(savePath);
- var reader = new BinaryReader(new MemoryStream(data));
- bool isReadEnd = false;
- while (!isReadEnd)
- {
- try
- {
- string key = reader.ReadString();
- string value = reader.ReadString();
- resMd5.Add(key, value);
- }
- catch
- {
- isReadEnd = true;
- }
- }
- reader.Close();
- }
- catch
- {
- Debug.Log(savePath + "不存在");
- }
- return resMd5;
- }
- }
|