数据文件转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()); //執行讀取文件的方法
??
?}
}
----------------------------------------------------------------------------------------------------------------------
總結
以上是生活随笔為你收集整理的数据文件转PNG图片程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Winter is here
- 下一篇: 图形面积——竞赛水题