标签:
你是否发现用Texturepacker在打包压缩资源文件之后。把原稿文件弄丢了,但是又要增加新的小png的时候,却无从下手了?或者你在借用其他游戏资源时,不知道哪个图片对应哪个名字。
打包生成的有两个文件
一个是**.txt 一个是**.png(有些不是txt是.pack 只要内容一样就行,你用记事本打开看看)
下面为代码:
package com.ll; import java.awt.image.BufferedImage; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.util.ArrayList; import javax.imageio.ImageIO; public class SpliPng { public static void main(String[] args) { // TODO Auto-generated method stub String parentPath = "/Users/haocao/SplitPng/"; // txt.png文件所在的文件夹 String gameFileName = "slots"; File file = new File(parentPath + gameFileName); String[] nameStrings = file.list(); for (String nameString : nameStrings) { if (nameString.contains(".txt")) { String tmpNameString = nameString.substring(0, nameString.lastIndexOf(".")); System.out.println("分割:" + tmpNameString); String targetFileName = tmpNameString; String pathTxt = parentPath + gameFileName + "/" + targetFileName + ".txt"; String pathPNG = parentPath + gameFileName + "/" + targetFileName + ".png"; String outPath = parentPath + gameFileName + "/" + targetFileName; toPNG(pathTxt, pathPNG, outPath); } } System.out.println("全部结束"); } public static void toPNG(String pathTxt, String pathPNG, String OUT) { ArrayList<String> name = new ArrayList<String>(); ArrayList<String> xy = new ArrayList<String>(); ArrayList<String> size = new ArrayList<String>(); try { String encoding = "GBK"; File file = new File(pathTxt); if (file.isFile() && file.exists()) { // 判断文件是否存在 InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到编码格式 BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; int lineNum = 0, lineNum2 = 0; while ((lineTxt = bufferedReader.readLine()) != null) { if (lineTxt.length() > 0) { lineNum++; if (lineNum2 > 0) lineNum2++; if (lineNum == 5) lineNum2 = 1; if (lineNum % 7 == 5) name.add(lineTxt); if (lineNum2 % 7 == 3) xy.add(lineTxt); if (lineNum2 % 7 == 4) size.add(lineTxt); } } read.close(); } else { System.out.println("找不到指定的文件"); } BufferedImage image = (BufferedImage) ImageIO.read(new File(pathPNG)); for (int i = 0; i < name.size(); i++) { String p1 = name.get(i), p2 = xy.get(i), p3 = size.get(i); int x = 0, y = 0, w = 0, h = 0, flag = 0; for (int j = 0; j < p2.length(); j++) { if (p2.charAt(j) <= ‘9‘ && p2.charAt(j) >= ‘0‘) { if (flag == 0) { x = x * 10 + p2.charAt(j) - ‘0‘; } else { y = y * 10 + p2.charAt(j) - ‘0‘; } } if (p2.charAt(j) == ‘,‘) flag = 1; } flag = 0; for (int j = 0; j < p3.length(); j++) { if (p3.charAt(j) <= ‘9‘ && p3.charAt(j) >= ‘0‘) { if (flag == 0) w = w * 10 + p3.charAt(j) - ‘0‘; else h = h * 10 + p3.charAt(j) - ‘0‘; } if (p3.charAt(j) == ‘,‘) flag = 1; } File f = new File(OUT); if (!f.exists()) f.mkdirs(); ImageIO.write(image.getSubimage(x, y, w, h), "png", new FileOutputStream(OUT + "/" + p1 + ".png")); System.out.println(p1 + ":finished"); } } catch (Exception e) { System.out.println("读取文件内容出错:" + e.getLocalizedMessage()); e.printStackTrace(); } } }
Libgdx:将TexturePacker打包的大图分割成小图片
标签:
原文地址:http://my.oschina.net/oahcfly/blog/424968