数据文件转PNG图片程序
2005-07-05 09:35
204 查看
做了一段手机游戏移植。有时候碰到游戏的资源文件被做成一个数据文件。想提出来很费事,所以自己写了一个程序来处理。方法就是自定义几个字节。与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图片程序
*@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()); //执行读取文件的方法
}
}
----------------------------------------------------------------------------------------------------------------------
相关文章推荐
- php程序读取firework生成的png图片数据
- Pencil程序无法导出png图片文件的解决方法
- 批量删除指定文件夹下某一格式的文件,如.png图片
- C++文本查询程序 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- C++文本查询程序 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- 保存在数据中image字段的图片文件显示出来
- 数据持久化的本质 - 数据保存成文件,存储到程序的沙盒中 -在应用程序结束时,将内存中的数据以文件的形式搬到(保存到)硬盘中
- 关于Android7.0版本及其以上系统获取本地数据(图片和文件)报系统出现异常,正在退出问题
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- 【Android进阶】如何使用文件来保存程序中的数据
- 数组作数据成员(程序的多文件组织)
- 得到ImageView的图片,压缩成PNG,并得到二进制流数据
- 用Java读取图片文件属性中的详细信息数据
- 根据pix飞控log文件和photo文件夹建立pos文件,提取pos数据小程序
- 【转】C# winform程序发布后找不到数据文件(xml,ini,etc...)的解决
- Java 小程序 实现网络下载图片到制定文件夹,压缩和删除文件
- php保存cavas导出base64数据到图片文件
- 通过文件二进制信息判断图片类型(png,jpg,gif)