本次使用的是 JDK1.8,并且使用了Json工具包 Jackson 大致代码如下: /** * @author kxvz * @title EgretAtlasUtil * @descriptio
本次使用的是 JDK1.8,并且使用了Json工具包 Jackson
大致代码如下:
/** * @author kxvz * @title EgretAtlasUtil * @description 对Egret图集进行处理的工具类, 有任何问题可以联系邮箱 * @modify TODO * @date 2020/12/18 15:55 * @mailto [email protected] */public class EgretAtlasUtil { /** * 把需要处理的图集内容(json+png)放到某个文件夹下,这里是设置需要处理的文件夹路径的 */ private static final String DIR_PATH = "/Users/admin/develop/tmp/t1"; /** * 此处是处理好后输出的图片的保存文件夹,为了避免出现文件名冲突,最好新建一个空文件夹进行存放 */ private static final String OUT_DIR_PATH = "/Users/admin/develop/tmp/t2"; /** * 入口 * * @param args * @throws Exception */ public static void main(String[] args) throws Exception { test(); } /** * 开始处理 * * @throws Exception */ public static void test() throws Exception { readAndCut(DIR_PATH, OUT_DIR_PATH); } /** * 读取文件并进行切分 * * @param dirPath * @param outDirPath * @throws Exception */ private static void readAndCut(String dirPath, String outDirPath) throws Exception { /** * 得到文件夹下所有文件 */ Stream<Path> list = Files.list(Paths.get(dirPath)); AtomicInteger count = new AtomicInteger(0); AtomicInteger succ = new AtomicInteger(0); AtomicInteger fail = new AtomicInteger(0); /** * 过滤出Json文件,只对JSON文件进行处理 */ list.filter(tmp -> tmp.getFileName().toString().endsWith(".json")).forEach(tmp -> { count.incrementAndGet(); try { cutting(tmp, outDirPath); System.out.println(tmp.getFileName() + " 已经处理完成!"); succ.incrementAndGet(); } catch (Exception e) { System.err.println(tmp.getFileName() + " => " + e.getMessage()); fail.incrementAndGet(); } }); /** * 输出统计信息 */ System.out.println("一共处理: " + count.get() + " 条数据, 其中成功: " + succ.get() + " 条, 失败: " + fail.get() + " 条!"); } /** * 处理JSON文件 * * @param jsonFile * @param outDir * @throws Exception */ private static void cutting(Path jsonFile, String outDir) throws Exception { if (jsonFile == null) {throw new RuntimeException("Json1文件不存在!");} File json = jsonFile.toFile(); if (!json.exists() || !json.isFile()) {throw new RuntimeException("Json2文件不存在!");} List<String> jsonLines = Files.readAllLines(jsonFile); if (Checker.isEmpty(jsonLines)) {throw new RuntimeException("Json文件内容为空!");} StringBuilder sb = new StringBuilder(); for (String s : jsonLines) { sb.append(s); } Path dir = jsonFile.getParent(); /** * JsonUtil 工具使用的是 jackson 工具包: 大致代码如下 * ObjectMapper mapper = new ObjectMapper(); * JsonNode obj = mapper.readTree(json); * 请自行添加相关依赖包 */ JsonNode obj = JsonUtil.toObj(sb.toString()); JsonNode imgNode = obj.get("file"); /** * 如果是普通的图集信息,这进入此处处理 */ if (Checker.isNotEmpty(imgNode)) { Path imgPath = Paths.get(dir.toString() + File.separator + imgNode.asText()); File imgFile = imgPath.toFile(); if (!imgFile.exists() || !imgFile.isFile()) { throw new RuntimeException("图片文件不存在!");} BufferedImage sourceImage = ImageIO.read(imgFile); String imgFileName = imgPath.getFileName().toString(); String imgName = imgFileName.substring(0, imgFileName.lastIndexOf(".")); File imgDirFile = new File(outDir + File.separator + imgName); if (!imgDirFile.mkdirs()) {throw new RuntimeException("图片文件夹创建失败!");} JsonNode frames = obj.get("frames"); Iterator<Map.Entry<String, JsonNode>> fields = frames.fields(); while (fields.hasNext()) { Map.Entry<String, JsonNode> field = fields.next(); String key = field.getKey(); JsonNode ele = field.getValue(); Integer x = ele.get("x").asInt(); Integer y = ele.get("y").asInt(); Integer w = ele.get("w").asInt(); Integer h = ele.get("h").asInt(); Integer offX = ele.get("offX").asInt(); Integer offY = ele.get("offY").asInt(); Integer sourceW = ele.get("sourceW").asInt(); Integer sourceH = ele.get("sourceH").asInt(); BufferedImage result = new BufferedImage(sourceW, sourceH, BufferedImage.TYPE_INT_ARGB); int[] imageRPG = new int[w * h]; int[] rgb = sourceImage.getRGB(x, y, w, h, imageRPG, 0, w); result.setRGB(offX, offY, w, h, rgb, 0, w); ImageIO.write(result, "png", new File(outDir + File.separator + imgName + File.separator + key + ".png")); } return; } /** * 如果是动画类的图集,则进入此处处理 */ JsonNode mcNode = obj.get("mc"); if (Checker.isNotEmpty(mcNode)) { JsonNode res = obj.get("res"); if (Checker.isNotEmpty(res)) { String img = null; Iterator<Map.Entry<String, JsonNode>> fields = mcNode.fields(); while (fields.hasNext()) { Map.Entry<String, JsonNode> field = fields.next(); String key = field.getKey(); if (!"res".equalsIgnoreCase(key)) { img = key; break; } } if (Checker.isEmpty(img)) { throw new RuntimeException("没找到图片信息"); } Path imgPath = Paths.get(dir.toString() + File.separator + img + ".png"); File imgFile = imgPath.toFile(); if (!imgFile.exists() || !imgFile.isFile()) { throw new RuntimeException("图片文件不存在!");} BufferedImage sourceImage = ImageIO.read(imgFile); String imgFileName = imgPath.getFileName().toString(); String imgName = imgFileName.substring(0, imgFileName.lastIndexOf(".")); File imgDirFile = new File(outDir + File.separator + imgName); if (!imgDirFile.mkdirs()) {throw new RuntimeException("图片文件夹创建失败!");} Iterator<Map.Entry<String, JsonNode>> ress = res.fields(); while (ress.hasNext()) { Map.Entry<String, JsonNode> field = ress.next(); String key = field.getKey(); JsonNode ele = field.getValue(); Integer x = ele.get("x").asInt(); Integer y = ele.get("y").asInt(); Integer w = ele.get("w").asInt(); Integer h = ele.get("h").asInt(); BufferedImage result = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); int[] imageRPG = new int[w * h]; int[] rgb = sourceImage.getRGB(x, y, w, h, imageRPG, 0, w); result.setRGB(0, 0, w, h, rgb, 0, w); ImageIO.write(result, "png", new File(outDir + File.separator + imgName + File.separator + key + ".png")); } } } }}
加了很多注释,应该都能看懂哈..
最近在学习写游戏,有些网上找到的资源不太适合,因此写了一个这种工具...