⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 maximagezip.java

📁 j2se用在j2me中的打包代码,使用性很强,代码可以参考
💻 JAVA
字号:
package Jixianyasuo;
import java.io.*;
import java.awt.*;
import java.awt.event.*;

public class MaxImageZip extends WindowAdapter implements ActionListener{
	Frame fram;
	Panel pane1;
	Panel pane2;
	TextField txf1;
	TextField txf2;
	TextField txf3;
	TextField txf4;
	TextField txf5;
	TextField txf6;
	TextField txf7;
	Button but;
	Choice chooser;
	
	String name;
	int start;
	int over;
	String type;
	String Doname;
	boolean isPng = false;
	int len[];
	byte tempData[];
	
	public MaxImageZip(){
		fram= new Frame("Packet of Map");
		pane1 = new Panel();
		pane2 = new Panel();
		fram.setBackground(new Color(0x999999));
		pane1.setBackground(new Color(0x999999));
		pane2.setBackground(new Color(0x999999));
		fram.setLayout(new FlowLayout());
		fram.add(pane1);
		fram.add(pane2);
		pane1.setLayout(new GridLayout(5,2));
		
		txf1 = new TextField(10);
		txf2 = new TextField(5);
		txf3 = new TextField(5);
		txf4 = new TextField(10);
		txf5 = new TextField(10);
		txf6 = new TextField();
		txf7 = new TextField();
		but = new Button("packet");
		pane1.add(new Label("文件名: "));
		pane1.add(txf1);
		pane1.add(new Label("起始: "));
		pane1.add(txf2);
		pane1.add(new Label("结束: "));
		pane1.add(txf3);
		pane1.add(new Label("后缀名: "));
		pane1.add(txf4);
		pane1.add(new Label("打包后文件名: "));
		pane1.add(txf5);
		//pane.add(txf6);
		//pane.add(txf7);
		//chooser = new Choice();
	  //chooser.add("map");
	  //chooser.add("image");
	  //pane2.add(chooser);
		pane2.add(but);
		but.addActionListener(this);
		fram.setSize(230,190);
		fram.setVisible(true);
		fram.addWindowListener(this);
		
		//getLength();
		//getFile();
	}
	
	public void getLength(){
		len = new int[over-start+1];
		for(int a=0;a<over-start+1;a++){
			File file = new File(".\\"+name + (start+a) +"."+type);
      len[a] = (int)file.length();
      System.out.print(len[a]+" ");
    }
    System.out.println("");
		
	}
	
		int PLTElen=0;
	int tRNSlen=0;
	int IDATlen=0;
	short imageWidth=0;
	short imageHeight=0;
	byte bitDepth=0;
	int red = 0;
	int green = 0;
	int blue = 0;

	
	public byte[] getFile(String name){
		try{
			System.out.println(name);
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			DataOutputStream dataout = new DataOutputStream(baos);
      DataInputStream datain;
	    datain = new DataInputStream(new FileInputStream(name));
	    
	    System.out.println("Read png information:   "+name);
	    datain.skipBytes(8);//File Signature
	    datain.readInt();//Data IHDR (IHDR lenght)
	    datain.skipBytes(4);//Data IHDR (Chunk Type)
	    imageWidth = (short)datain.readInt();//Data IHDR (width)
	    imageHeight = (short)datain.readInt();//Data IHDR (height)
	    bitDepth = (byte)datain.readByte();//Data IHDR (Bit depth)
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++    
	    dataout.writeShort(imageWidth);
	    dataout.writeShort(imageHeight);
	    dataout.writeByte(bitDepth);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++	    
	    datain.readByte();//Data IHDR (ColorType)
	    datain.readByte();//Data IHDR (Compression method)
	    datain.readByte();//Data IHDR (Filter method)
	    datain.readByte();//Data IHDR (Interlace method)
	    datain.skipBytes(4);//Data IHDR (CRC)

	    
	    String tempChunkType = " ";
	    int tempLength = 0;
	    while(true){
	    	tempLength=datain.readInt();//Data (lenght)
	    	tempChunkType = ""+(char)datain.readByte()+(char)datain.readByte()+(char)datain.readByte()+(char)datain.readByte();
	    	//Data (Chunk Type)
	    	if(tempChunkType.equals("PLTE")){
    			PLTElen=tempLength;//Data PLTE (PLTE lenght)
			    dataout.writeShort((short)(PLTElen/3));
			    for(int a=0;a<PLTElen/3;a++){
			    	red = datain.readUnsignedByte();
			    	green = datain.readUnsignedByte();
			    	blue = datain.readUnsignedByte();
			    	short temp = (short)((red*31/255)<<11 | (green*63/255)<<5 | (blue*31/255));
			    	dataout.writeShort(temp);
			    }
			    datain.skipBytes(4);//Data PLTE (CRC)
	    	}else if(tempChunkType.equals("IEND")){
	    		dataout.write(tempChunkType.getBytes(),0,4);
	    		datain.skipBytes(4);//Data IEND (CRC)
	    		break;
	    	}else if(tempChunkType.equals("tRNS")){
	    		dataout.write(tempChunkType.getBytes(),0,4);
	    		for(int a=0;a<tempLength;a++){
	    			int in = datain.readUnsignedByte();
	    			if(in == 0){
	    				dataout.writeByte((byte)a);
	    			}
	    		}
	    		datain.skipBytes(4);//Data tRNS (CRC)
	    	}else if(tempChunkType.equals("IDAT")){
	    		dataout.write(tempChunkType.getBytes(),0,4);
	    		dataout.writeShort((short)tempLength);
	    		byte tempb[] = new byte[tempLength+4];
	    		datain.readFully(tempb);
	    		dataout.write(tempb,0,tempLength+4); 
	    	}else {
	    		datain.skipBytes(tempLength+4);
	    	}
	    }
	    byte[] b =  baos.toByteArray();
    	datain.close();
	    datain = null;
	    dataout.close();
	    dataout = null;
	    return b;
	    //dataout = new DataOutputStream(new FileOutputStream(".\\"+Doname));
	    //dataout.write(b,0,b.length); 
			//dataout.flush();
      //dataout.close();
      //dataout = null;
    }catch(Exception e){}
    	return null;
	}
	
	public void getFile(){
		  try
      {
        DataOutputStream dataout = new DataOutputStream(new FileOutputStream(".\\"+Doname));
        boolean flag = false;
        dataout.writeByte(over-start+1);
      	for(int k = 0; k < over-start+1; k++)
        {
            byte tempByte[] = getFile(".\\"+name + (start+k)+"."+type);
						dataout.writeShort(tempByte.length);
        	  dataout.write(tempByte, 0, tempByte.length);
        }
				System.out.println("");
				dataout.flush();
        dataout.close();
        dataout = null;
      }
      catch(Exception exception1)
      {
        System.out.println(exception1);
      }
	}
	public void actionPerformed(ActionEvent e){
		name = txf1.getText();
		start = Integer.parseInt(txf2.getText());
		over = Integer.parseInt(txf3.getText());;
		type = txf4.getText();
		Doname = txf5.getText();
		//int a = chooser.getItemCount();
		//if(a == 1) isPng = false;
		//else if(a == 2) {
		//	System.out.println("Image");
		//	isPng = true;
		//}
		//getLength();
		getFile();
	} 
	public void windowClosing(WindowEvent e){
		fram.dispose();
	}
	public static void main(String arg[]){
		MaxImageZip mapdo = new MaxImageZip();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -