熟悉png格式的朋友都知道png有3个必须的文件块,其他的可以去除.这个工具的作用就是提取3个文件块.当然,文件头与文件尾还是单加上的.对图片的优化还是有一定作用的:
/*
* Created on 2006-1-24
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author lhz1
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.io.*;
public class ProImage {
static int size;
static byte[] a;
static byte[] b;
static byte[] f;
static int i=0;
static String[] Fname;
static int Fcount;
/**
*
*/
public ProImage() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) throws IOException {
ProImage.find(args[0]);
//ProImage.readImage("1.png");
//ProImage.moImage();
//ProImage.writeImage("2.png");
}
public static void readImage(String url) throws IOException{
try {
DataInputStream dis=new DataInputStream(new FileInputStream(new File(url)));
size=dis.available();
b=new byte[size];
for(i=0;i<size;i++){
b[i]=dis.readByte();
}
//a=b;
dis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void moImage(){//49 48 44 52 IHDR标识 50 4C 54 45 PLTE标识 49 44 41 54 IDAT标识 IEND 00 00 00 00 49 45 4E 44 AE 42 60 82
int k=8;
int j=0;
int c=0;
a=new byte[size];
//加入文件头;
a[0]=(byte)0x89;
a[1]=(byte)0x50;
a[2]=(byte)0x4E;
a[3]=(byte)0x47;
a[4]=(byte)0x0D;
a[5]=(byte)0x0A;
a[6]=(byte)0x1A;
a[7]=(byte)0x0A;
//System.out.println(size);
for(i=0;i<size-4;i++){
if(b[i]==0x49&&b[i+1]==0x48&&b[i+2]==0x44&&b[i+3]==0x52){//IHDR
System.out.println("find");
int len=(int )( ( ( b[i-4] & 0xff ) << 24) | ((b[i-3] & 0xff ) << 16 )| ((b[i-2] & 0xff) << 8 ) | (b[i-1] & 0xff) ) ;
j=i;
for( c=0;c<len+12;c++){
a[k]=b[j-4];
k++;
j++;
}
}
if(b[i]==0x50&&b[i+1]==0x4C&&b[i+2]==0x54&&b[i+3]==0x45){//PLTE
int len=(int )( ( ( b[i-4] & 0xff ) << 24) | ((b[i-3] & 0xff ) << 16 )| ((b[i-2] & 0xff) << 8 ) | (b[i-1] & 0xff) ) ;
j=i;
for(c=0;c<len+12;c++){
a[k]=b[j-4];
k++;
j++;
}
}
if(b[i]==0x49&&b[i+1]==0x44&&b[i+2]==0x41&&b[i+3]==0x54){//IDAT
int len=(int )( ( ( b[i-4] & 0xff ) << 24) | ((b[i-3] & 0xff ) << 16 )| ((b[i-2] & 0xff) << 8 ) | (b[i-1] & 0xff) ) ;
j=i;
System.out.println("len"+len);
for(c=0;c<len+12;c++){
a[k]=b[j-4];
k++;
j++;
}
}
a[k]=(byte)0x00;
a[k+1]=(byte)0x00;
a[k+2]=(byte)0x00;
a[k+3]=(byte)0x00;
a[k+4]=(byte)0x49;
a[k+5]=(byte)0x45;
a[k+6]=(byte)0x4E;
a[k+7]=(byte)0x44;
a[k+8]=(byte)0x4E;
a[k+9]=(byte)0x42;
a[k+10]=(byte)0x60;
a[k+11]=(byte)0x82;
}
f=new byte[k+12];
for(i=0;i<k+12;i++){
f[i]=a[i];
}
}
public static void writeImage(String pngName) throws IOException{
FileOutputStream fos=new FileOutputStream( pngName);
System.out.println(f[0]);
fos.write(f);
fos.close();
}
public static void find(String dir)
{
File parent = new File(dir);
File child[] = parent.listFiles();
Fcount=child.length;
Fname=new String[Fcount];
for(int i = 0;i<child.length;i++)
{
if(child[i].isFile())
{
Fname[i]=child[i].toString();
System.out.println("file"+Fname[i]);
try {
ProImage.readImage(Fname[i]);
ProImage.moImage();
ProImage.writeImage(Fname[i]);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
//find(child[i].toString());
}
}
}
}