日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

数据文件转PNG图片程序

發(fā)布時(shí)間:2024/1/18 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据文件转PNG图片程序 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

做了一段手機(jī)游戲移植。有時(shí)候碰到游戲的資源文件被做成一個(gè)數(shù)據(jù)文件。想提出來(lái)很費(fèi)事,所以自己寫(xiě)了一個(gè)程序來(lái)處理。方法就是自定義幾個(gè)字節(jié)。與PNG格式的圖片字節(jié)相匹配。這樣把PNG圖片信息提出來(lái)

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

/*
*數(shù)據(jù)文件轉(zhuǎn)png圖片程序
*@author k7sem
*05年10月12日修改
*/
import java.io.*;

class transfer
{
?byte a1 = (byte)0x89; //定義PNG文件頭部的匹配參數(shù)
?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的構(gòu)造
?{
?}
?//從屏幕中輸入要轉(zhuǎn)換的文件路徑
?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;

?}
?//讀取文件路徑,進(jìn)行算法分析,從新寫(xiě)入新文件
?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]; //定義存放被處理文件的字節(jié)數(shù)組,一定要比被處理的文件實(shí)際大小要大
??int i = 0; //返回具體讀到了多少個(gè)字節(jié)
??
??try
??{
??FileInputStream fs = new FileInputStream(new File(s)); //用文件路徑 構(gòu)造一個(gè)文件流
??i = fs.read(b); //將讀到的byte放入 b的數(shù)組中,返回實(shí)際讀到數(shù)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的長(zhǎng)度
??int headbyte=0;
??for(int j=0;j<i;j++) //對(duì)所有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個(gè)字節(jié)是否為png頭部信息
???{
????if(head) //判斷第一個(gè)PNG文件從什么位置開(kāi)始
????{
?????head = false;
?????headbyte = j;
????}
????
???
????for(int z=j;z<i;z++) //對(duì)所有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個(gè)字節(jié)是否為文件尾
??????{
???????y = z-j+headbyte;//滿足條件后 將文件尾位置減掉文件頭位置再減掉 頭部信息位數(shù) 所得長(zhǎng)度
?????????????
???????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!!");//打印出實(shí)際轉(zhuǎn)換了多少個(gè)PNG文件
??
?}
?public static void main(String[] args)
?{
??transfer ui = new transfer();//產(chǎn)生transfer 對(duì)象
??ui.readFile(ui.in()); //執(zhí)行讀取文件的方法
??
?}
}

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

總結(jié)

以上是生活随笔為你收集整理的数据文件转PNG图片程序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。