【libgdx】图片整合工具TexturePacker的打包与反打包

打包工具的使用请参考

奋斗小土豆丶的博文 http://blog.sina.com.cn/s/blog_940dd50a0101cvp1.html

反打包工具

1。这个网上已经有了

	public static void main(String[] args) {
		toPNG("D:/libgdx-0.9.8/tools/test-me!/inputpng/canyonbunny-ui.pack",
				"D:/libgdx-0.9.8/tools/test-me!/inputpng/canyonbunny-ui.png",
				"D:/libgdx-0.9.8/tools/test-me!/outputpng");
	}

	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>();
		Boolean isfirst = true ;
		int num = 5;
		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(isfirst){
						if(lineTxt.trim().equals("")){
							num = 6 ;
						}else{
							num = 5;
						}
						isfirst = false ;
					}
					lineNum++;
					if (lineNum2 > 0)
						lineNum2++;
					if (lineNum == num)
						lineNum2 = 1;
					if (lineNum % 7 == num)
						name.add(lineTxt);
					if (lineNum2 % 7 == 3)
						xy.add(lineTxt);
					if (lineNum2 % 7 == 4)
						size.add(lineTxt);
				}
				read.close();
			} else {
				System.out.println("找不到指定的文件");
			}
			System.out.println(xy);
			System.out.println(size);
			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 + ".png生成成功");
			}

		} catch (Exception e) {
			System.out.println("读取文件内容出错");
			e.printStackTrace();
		}

	}

2.用libgdx的包写的

public class Main {
	public static void main(String[] args) {
		LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
		cfg.title = "CanyonBunny";
		cfg.useGL20 = false;
		cfg.width = 1;
		cfg.height = 1;
		
		new LwjglApplication(new RestoreTexturePackerLibGdx(), cfg).exit();
	}
}

public class RestoreTexturePackerLibGdx extends Game {
	TextureAtlas atlas;
	ArrayList<String> name = new ArrayList<String>();
	ArrayList<String> xy = new ArrayList<String>();
	ArrayList<String> size = new ArrayList<String>();

	@Override
	public void create() {
		String out = "D:/libgdx-0.9.8/tools/test-me!/outputpng_test";
		atlas = new TextureAtlas(Gdx.files.internal("data/canyonbunny.pack"));
		Array<AtlasRegion> array = atlas.getRegions();
		for(int i=0;i<array.size;i++){
			AtlasRegion a = atlas.getRegions().get(i);
			name.add(a.name);
			xy.add("xy: "+a.getRegionX()+", "+a.getRegionY());
			size.add("size: "+a.getRegionWidth()+", "+a.getRegionHeight());
		}
		BufferedImage image;
		try {
			image = (BufferedImage) ImageIO
					.read(Gdx.files.internal("data/canyonbunny.png").read());

			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 + ".png生成成功");
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		}

	}
}


附上Learning Libgdx Game Development这本书的所有图片素材






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值