巴拉巴拉
小魔仙

Java atlas图集分割

java版本的图集分割

atlasFilePath : 图集配置文件
imageFilePath :图集文件
outPath :输出文件夹

图片读取
图片裁剪
图片存储

StringUtil.parseInt:

public static int parseInt(String str){
    return parseInt(str, 0);
}
public static int parseInt(String str, int defaultValue){
    try{
        return Integer.parseInt(str);
    }catch (Exception e) {}
    return defaultValue;
}
package com.xxx.xxx.xxx;

import com.xxx.xxx.xxx.StringUtil;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Iterator;

/**
 * atlas 图集文件分割
 */
public class AtlasImageSplite {

    public static void main(String args[]) throws Exception{
        String atlasFilePath = "/Users/xxx/xxx/xxxTexture.atlas";
        String imageFilePath = "/Users/xxx/xxx/xxxTexture.png";

        String outPath = "/Users/xxx/xxx/out/";
        File outDir = new File(outPath);
        if(!outDir.exists()){
            outDir.mkdirs();
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(atlasFilePath)));
        String line = null;
        int count = 0;
        String name = null;
        int x = 0;
        int y = 0;
        int w = 0;
        int h = 0;
        while(null != (line = br.readLine())){
            count++;
            if(1 == count) continue;;
            if(line.startsWith("size:")) continue;
            if(line.startsWith("format:")) continue;
            if(line.startsWith("filter:")) continue;
            if(line.startsWith("repeat:")) continue;

            if(line.startsWith("  index")){
                System.out.println("处理" + name);

                FileInputStream is = null;
                ImageInputStream iis = null;
                BufferedImage bi = ImageIO.read(new File(imageFilePath));//读取原始图片
                System.out.printf("%s:%d,%d,%d,%d\n",name, x, y, w, h);
                BufferedImage subImage = bi.getSubimage(x, y, w, h);
                ImageIO.write(subImage, "png", new File(outDir + File.separator + name + ".png")); //保存新图片
                continue;
            }
            if(line.startsWith("  xy:")){
                line = line.replaceAll("  xy:","");
                String par[] = line.split(",");
                x = StringUtil.parseInt(par[0].trim());
                y = StringUtil.parseInt(par[1].trim());
                continue;
            }
            if(line.startsWith("  size:")){
                line = line.replaceAll("  size:","");
                String par[] = line.split(",");
                w = StringUtil.parseInt(par[0].trim());
                h = StringUtil.parseInt(par[1].trim());
                continue;
            }
            if(line.startsWith("  ")) continue;
            name = line.trim();
        }
    }
}
赞(1) 打赏
如果文章对你有帮助,欢迎你来评价反馈。AgainFly » Java atlas图集分割

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  • Q Q(选填)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏