小工具DeTexturepacker

29 篇文章 1 订阅

原文地址:http://blog.csdn.net/stalendp/article/details/17652681


用texturepacker打包了素材,不小心把原来的素材丢掉了;或则打开了别人的apk,想用里面的素材做一些练习。针对这些场景,我写了个小工具分享给大家。

附录是个打包好的jar文件(需要Java环境才能够运行),解压后目录中有3个文件,jar和两个素材。切换到目录,运行如下命令就可以把素材还原到result目录下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. java -jar DeTexturepacker.jar Common1  

下面是工具的源代码:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.detp;  
  2.   
  3. import java.awt.Graphics;  
  4. import java.awt.geom.AffineTransform;  
  5. import java.awt.image.AffineTransformOp;  
  6. import java.awt.image.BufferedImage;  
  7. import java.io.BufferedReader;  
  8. import java.io.File;  
  9. import java.io.FileInputStream;  
  10. import java.io.IOException;  
  11. import java.io.InputStream;  
  12. import java.io.InputStreamReader;  
  13. import java.util.ArrayList;  
  14. import java.util.regex.Matcher;  
  15. import java.util.regex.Pattern;  
  16.   
  17. import javax.imageio.ImageIO;  
  18.   
  19.   
  20. public class Decoder {  
  21.     public static String pngname = "";  
  22.     public static String plistname = "";  
  23.     public static void main(String[] args) throws Exception {  
  24.           
  25.         if(args.length==1) {  
  26.             pngname = args[0] + ".png";  
  27.             plistname = args[0] + ".plist";  
  28.         } else if(args.length==2) {  
  29.             for(String arg : args) {  
  30.                 if(arg.contains("png")) {  
  31.                     pngname = arg;  
  32.                 } else if(arg.contains("plist")) {  
  33.                     plistname = arg;  
  34.                 }  
  35.             }  
  36.         } else {  
  37.             System.out.println("Usage: 1) DeTexturepacker name  2) DeTexturepacker name.plist name.png\n");  
  38.             return;  
  39.         }  
  40.         delImg();  
  41.     }  
  42.       
  43.     private static void delImg() throws Exception {  
  44.         InputStream is =   new FileInputStream( pngname );  
  45.         BufferedImage image = ImageIO.read(is);  
  46.         File fold = new File("result");  
  47.         if(fold.exists()) {  
  48.             fold.delete();  
  49.         }  
  50.         fold.mkdir();  
  51.         ArrayList<Frame> frames = exactData().frames;  
  52.         for(Frame f : frames) {  
  53.             System.out.print(".");  
  54.             try {  
  55.                 f.split(image, "result");  
  56.             }catch(Exception e) {  
  57.                 System.err.print("===>" +f.toString());  
  58.                 e.printStackTrace();  
  59.             }  
  60.         }  
  61.         System.out.println("Done!\n");  
  62.     }  
  63.       
  64.     private static PlistData exactData() throws Exception {  
  65.         PlistData pd = new PlistData();  
  66.         String con = getContent();  
  67.           
  68.         Pattern p = Pattern.compile("<key>frames</key>[^<]+<[^<]+(.+)");  
  69.         Matcher m = p.matcher(con);  
  70.         if(m.find()) {  
  71.             con = m.group(1);  
  72.             p = Pattern.compile("<key>([^<]+)<[^<]+<dict>(.*?)</dict>");  
  73.             m = p.matcher(con);  
  74.               
  75.             Pattern pc = Pattern.compile("<key>([^<]+)</key>[^<]+<([^<]+)");  
  76.             while(m.find()) {  
  77.                 if(m.group(1).equals("metadata")) {  
  78.                 } else {  
  79.                     Frame f = new Frame();  
  80.                     pd.add(f);  
  81.                     f.filename = m.group(1);  
  82.                     Matcher mc = pc.matcher(m.group(2));  
  83.                     while(mc.find()){  
  84.                         f.set(mc.group(1), mc.group(2));  
  85.                     }  
  86.                 }  
  87.             }  
  88.         }  
  89.         return pd;  
  90.     }  
  91.       
  92.     private static String getContent() throws IOException {  
  93.         InputStream is = new FileInputStream( plistname);  
  94.         BufferedReader reader = new BufferedReader(new InputStreamReader(is));  
  95.         String line;  
  96.         StringBuilder sb = new StringBuilder();  
  97.         while((line=reader.readLine())!=null) {  
  98.             sb.append(line);  
  99. //          System.out.println(line);  
  100.         }  
  101.         return sb.toString();  
  102.     }  
  103. }  
  104.   
  105.   
  106.   
  107. class Frame {  
  108.     public String filename;  
  109.     public Rect frame, sourceColorRect;  
  110.     public Vec2 offset, size;  
  111.     public boolean rotated;  
  112.       
  113.     public void set(String key, String val) {  
  114.         if(key.equals("frame")) {  
  115.             frame = new Rect(val);  
  116.         } else if(key.equals("offset")) {  
  117.             offset = new Vec2(val);  
  118.         } else if(key.equals("rotated")) {  
  119.             rotated = val.contains("true");  
  120.         } else if(key.equals("sourceColorRect")) {  
  121.             sourceColorRect = new Rect(val);  
  122.         } else if(key.equals("sourceSize")) {  
  123.             size = new Vec2(val);  
  124.         }   
  125.     }  
  126.       
  127.     public void split(BufferedImage image, String root) throws IOException {  
  128.         if(filename.contains("/")) {  
  129.             String[] fs = filename.split("/");  
  130.             String ff = root;  
  131.             for(int i=0; i<fs.length-1; i++) {  
  132.                 ff += "/" + fs[i];  
  133.                 File mf = new File(ff);  
  134.                 if(!mf.exists()) {  
  135.                     mf.mkdir();  
  136.                 }  
  137.             }  
  138.         }  
  139.         BufferedImage si = null;  
  140.         if(rotated) {  
  141.             si = rotateImg(image.getSubimage(frame.x1, frame.y1, frame.y2, frame.x2));  
  142.         } else {  
  143.             si = image.getSubimage(frame.x1, frame.y1, frame.x2, frame.y2);  
  144.         }  
  145.         si = changeSize(si);  
  146.         ImageIO.write(si, "png"new File(String.format("%s/%s", root, filename)));  
  147.     }  
  148.       
  149.     private BufferedImage rotateImg(BufferedImage bufferedImage) {  
  150.         int width = bufferedImage.getWidth(), height = bufferedImage.getHeight();  
  151.         int max = width > height ? width : height;  
  152.           
  153.         BufferedImage tmp = new BufferedImage(max, max, BufferedImage.TYPE_INT_ARGB);  
  154.         Graphics g = tmp.getGraphics();  
  155.         g.drawImage(bufferedImage, (max-width)/2, (max-height)/2null);  
  156.           
  157.          AffineTransform transform = new AffineTransform();  
  158.          transform.rotate(-Math.PI/2, max/2, max/2);  
  159.          AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);  
  160.          tmp = op.filter(tmp, null);  
  161.          try {  
  162.              tmp = tmp.getSubimage((max-height)/2, (max-width)/2, height, width);   
  163.          }catch(Exception e) {  
  164.              tmp = tmp.getSubimage((max-height)/2, (max-width)/2, height-1, width-1);   
  165.              System.err.println("Frame#rotateImg fix");  
  166.          }  
  167.            
  168.          return tmp;  
  169.     }  
  170.       
  171.     private BufferedImage changeSize(BufferedImage bufferedImage){  
  172.         BufferedImage tmp = new BufferedImage(size.x, size.y, BufferedImage.TYPE_INT_ARGB);  
  173.         Graphics g = tmp.getGraphics();  
  174.         int x = (size.x - bufferedImage.getWidth())/2 + offset.x;  
  175.         int y = (size.y - bufferedImage.getHeight())/2 - offset.y;  
  176.         g.drawImage(bufferedImage, x, y, null);  
  177.         return tmp;  
  178.     }  
  179.       
  180.     public String toString() {  
  181.         return String.format("%s\tframe:%s, offset:%s, rotated:%s, sourceColorRect:%s, sourceSize:%s\n",   
  182.                 filename, frame, offset, rotated?"true":"false", sourceColorRect, size);  
  183.     }  
  184.       
  185. }  
  186.   
  187. class PlistData {  
  188.     public ArrayList<Frame> frames = new ArrayList<Frame>();  
  189.     public void add(Frame f) {  
  190.         frames.add(f);  
  191.     }  
  192. }  
  193.   
  194. class Vec2 {  
  195.     static Pattern p = Pattern.compile("(-?\\d+),(-?\\d+)");  
  196.     public int x, y;  
  197.     public Vec2(String val) {  
  198.         Matcher m = p.matcher(val);  
  199.         if(m.find()) {  
  200.             x = Integer.parseInt(m.group(1));  
  201.             y = Integer.parseInt(m.group(2));  
  202.         }else {  
  203.             System.err.println(val + " is not a Vec2!!");  
  204.         }  
  205.     }  
  206.     public Vec2(int _x, int _y) {  
  207.         x = _x;  
  208.         y = _y;  
  209.     }  
  210.     public String toString() {  
  211.         return "(" + x + "," + y + ")";  
  212.     }  
  213. }  
  214.   
  215. class Rect {  
  216.     static Pattern p = Pattern.compile("(-?\\d+),(-?\\d+)[^\\d]+(-?\\d+),(-?\\d+)");  
  217.     public int x1,y1, x2, y2;  
  218.     public Rect(String val) {  
  219.         Matcher m = p.matcher(val);  
  220.         if(m.find()) {  
  221.             x1 = Integer.parseInt(m.group(1));  
  222.             y1 = Integer.parseInt(m.group(2));  
  223.             x2 = Integer.parseInt(m.group(3));  
  224.             y2 = Integer.parseInt(m.group(4));  
  225.         }else {  
  226.             System.err.println(val + " is not a Rect!!");  
  227.         }  
  228.     }  
  229.     public Rect(int _x1, int _y1,int _x2, int _y2) {  
  230.         x1 = _x1;  
  231.         y1 = _y1;  
  232.         x2 = _x2;  
  233.         y2 = _y2;  
  234.     }  
  235.     public String toString() {  
  236.         return String.format("((%d,%d),(%d,%d))", x1,y1, x2, y2);  
  237.     }  
  238. }  


工具下载地址:

http://download.csdn.net/detail/stalendp/6783755


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值