数据文件转PNG图片程序

做了一段手机游戏移植。有时候碰到游戏的资源文件被做成一个数据文件。想提出来很费事,所以自己写了一个程序来处理。方法就是自定义几个字节。与PNG格式的图片字节相匹配。这样把PNG图片信息提出来

-----------------------------------------------------------------------------------------------------------------------------------------

/*
*数据文件转png图片程序
*@author k7sem
*05年10月12日修改
*/
import java.io.*;

class transfer
{
 byte a1 = (byte)0x89; //定义PNG文件头部的匹配参数
 byte a2 = (byte)0x50;
 byte a3 = (byte)0x4e;
 byte a4 = (byte)0x47;
 byte a5 = (byte)0x0d;
 byte a6 = (byte)0x0a;
 byte a7 = (byte)0x1a;
 byte a8 = (byte)0x0a;
 //------------png file end--------------//png文件尾
 byte e1 = (byte)0x49;
 byte e2 = (byte)0x45;
 byte e3 = (byte)0x4e;
 byte e4 = (byte)0x44;
 byte e5 = (byte)0xae;
 byte e6 = (byte)0x42;
 byte e7 = (byte)0x60;
 byte e8 = (byte)0x82;
 //--------------------------------------

 transfer() //transfer的构造
 {
 }
 //从屏幕中输入要转换的文件路径
 public String in()//Type a file path and return the path as a String
 {
  System.out.println("Please type the file path:");
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  String ss="";
  try{
   ss = br.readLine();
  }catch(Exception e){
   e.printStackTrace();
  }
  return ss;

 }
 //读取文件路径,进行算法分析,从新写入新文件
 public void readFile(String s)//use the file path to contruct a method which is solve the byte of this file and return a byte array
 {
  byte[] b= new  byte[650000]; //定义存放被处理文件的字节数组,一定要比被处理的文件实际大小要大
  int i = 0; //返回具体读到了多少个字节
  
  try
  {
  FileInputStream fs = new FileInputStream(new File(s)); //用文件路径 构造一个文件流
  i = fs.read(b); //将读到的byte放入 b的数组中,返回实际读到数i
  if(i != -1)  //判断文件是否为空 
   {
   System.out.println("The file size is "+i+" byte");   
   }else
   {
    System.out.println("The file is empty ");
   }
  
  }catch(Exception e){
   e.printStackTrace();

  }
  
  int h = 0;
  boolean head = true;
  boolean f = false; //保证每次只查找一次文件尾 
  int y = 0;  //存放文件byte的长度
  int headbyte=0;
  for(int j=0;j<i;j++) //对所有byte遍历
  {
   
   
   if((a1 == b[j])&&(a2 == b[j+1])&&(a3 == b[j+2])&&(a4 == b[j+3])&&(a5 == b[j+4])&&(a6 == b[j+5])&&(a7 == b[j+6])&&(a8 == b[j+7])) // 判断前8个字节是否为png头部信息
   {
    if(head) //判断第一个PNG文件从什么位置开始
    {
     head = false;
     headbyte = j;
    }
    
   
    for(int z=j;z<i;z++) //对所有byte遍历
    {
     
     if(!f)
     {
      
      if((e1==b[z])&&(e2==b[z+1])&&(e3==b[z+2])&&(e4==b[z+3])&&(e5==b[z+4])&&(e6==b[z+5])&&(e7==b[z+6])&&(e8==b[z+7]))//判断后8个字节是否为文件尾
      {
       y = z-j+headbyte;//满足条件后 将文件尾位置减掉文件头位置再减掉 头部信息位数 所得长度
             
       f=true;      
              
       try
       {
             
       FileOutputStream fos = new FileOutputStream(new File(".",h+".png"));
       fos.write(b,j,y);
                
       h++;
      
       }catch (Exception e){}
       
       
      }
      
     }
    
    }
   
    f=false;    
     
  }   
   
  }
  System.out.println(h+" files has transfer successful!!");//打印出实际转换了多少个PNG文件
  
 }
 public static void main(String[] args)
 {
  transfer ui = new transfer();//产生transfer 对象
  ui.readFile(ui.in()); //执行读取文件的方法
  
 }
}

----------------------------------------------------------------------------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贝壳鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值