Libgdx TexturePacker大图分割为小图

如何将TexturePacker打包的大图还原为之前的小图呢?下面的算法帮你轻松实现!



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 main {
    public static void toPNG(String pathTxt,String pathPNG,String OUT)
    {
        ArrayList name=new ArrayList();
        ArrayList xy=new ArrayList();
        ArrayList size=new ArrayList();
        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) {
                    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;iget(i),p2=xy.get(i),p3=size.get(i);

                int x=0,y=0,w=0,h=0,flag=0;
                for(int j=0;jif(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;jif(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.printStackTrace();
        }

    }
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        toPNG("g:/help.txt","g:/help.png","g:/ss");//atlas文件,大图文件,目标输出文件
    }

}

Demo下载

你可能感兴趣的:(Libgdx TexturePacker大图分割为小图)