"Image → PngFile" Cache

package lab.sodino.PngTest; public class PngTest { public static void main(String[] args) { // 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; // // String head = getStr(a1) + getStr(a2) + getStr(a3) + getStr(a4) + getStr(a5) + getStr(a6) + getStr(a7) // + getStr(a8); // System.out.println(head); // String tail = getStr(e1) + getStr(e2) + getStr(e3) + getStr(e4) + getStr(e5) + getStr(e6) + getStr(e7) // + getStr(e8); // System.out.println(tail); } public static String getStr(byte b) { return (String.valueOf((char) (b))); } } package lab.sodino.PngTest; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; public class PntToStreamFile { public static void main(String[] args) { byte[] bs = getFileToByte(new File("D://01.png")); int start = 8; int end = bs.length - 8; int count = bs.length - 16; for (int i = 0; i < count; i++) { System.out.print(Integer.toHexString(bs[i]) + " "); if (i != 0 && i % 27 == 0) { System.out.println(); } } try { File f = new File("D://01.dat"); if (f.exists() == false) { f.createNewFile(); } FileOutputStream fos = new FileOutputStream(f); fos.write(bs); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException ie) { ie.printStackTrace(); } } public static byte[] getFileToByte(File file) { byte[] by = new byte[(int) file.length()]; try { InputStream is = new FileInputStream(file); ByteArrayOutputStream bytestream = new ByteArrayOutputStream(); byte[] bb = new byte[2048]; int ch; ch = is.read(bb); while (ch != -1) { bytestream.write(bb, 0, ch); ch = is.read(bb); System.out.println("ch : " + ch); } by = bytestream.toByteArray(); } catch (Exception ex) { ex.printStackTrace(); } return by; } } package lab.sodino.PngTest; /* *数据文件转png图片程序 *@author k7sem *05年10月12日修改 */ import java.io.*; class TransferFileToPng { 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; // -------------------------------------- TransferFileToPng() // 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 ch = 0; // 返回具体读到了多少个字节 try { FileInputStream fs = new FileInputStream(new File(s)); // 用文件路径 构造一个文件流 ch = fs.read(b); // 将读到的byte放入b的数组中,返回实际读到数i if (ch != -1) // 判断文件是否为空 { System.out.println("The file size is " + ch + " byte"); } else { System.out.println("The file is empty "); } } catch (Exception e) { e.printStackTrace(); } int pngCount = 0; boolean head = true; boolean f = false; // 保证每次只查找一次文件尾 int y = 0; // 存放文件byte的长度 int headbyte = 0; for (int j = 0; j < ch; 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 < ch; 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(".", pngCount + ".png")); fos.write(b, j, y); pngCount++; } catch (Exception e) { } } } } f = false; } } System.out.println(pngCount + " files has transfer successful!!");// 打印出实际转换了多少个PNG文件 } public static void main(String[] args) { TransferFileToPng ui = new TransferFileToPng();// 产生transfer 对象 // ui.readFile(ui.in()); // 执行读取文件的方法 ui.readFile("D://01.dat"); } }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值