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

📄 createimageservlet.java

📁 用JavaME开发
💻 JAVA
字号:
/*
 * Created on 2005-9-13
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
import java.io.*;
import java.awt.*;
import java.awt.image.*;

import javax.servlet.*;
import javax.servlet.http.*;

import com.sun.image.codec.jpeg.*;

/**
* @author Liao Xue Feng
*/
public class CreateImageServlet extends HttpServlet {

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws ServletException,IOException{
			  
     
  //byte[] b = new byte[in.readInt()];
  //in.readFully(b);
  // System.out.println("@@"+ len);
  //String name=request.getParameter("username");
  // String photo=request.getParameter("photo");
  //long time=System.currentTimeMillis();  
//  File f = new java.io.File("C:/SNS-WEB-3/home/attachment/photo/" + photo + ".png");
//    DataInputStream in = new DataInputStream(request.getInputStream());
/*
  DataInputStream in = new DataInputStream(request.getInputStream());
  File f = new java.io.File("F:\\java\\apache-tomcat-5.5\\apache-tomcat-5.5.25\\webapps\\photoServer\\WEB-INF\\bbc.jpg");  
  DataOutputStream o = new DataOutputStream(new FileOutputStream(f)); 
        
  byte[] b =new byte[100000];
  int len = 0;
  int size =0;
  while( ( len = in.read( b ) ) != -1 )
  {   
  o.write(b,0,len);
  size+=len;
  }
  o.write(b, 0, b.length);
  o.flush();
  o.close();*/
         DataInputStream in = new DataInputStream(request.getInputStream());
	//	InputStream io=request.getInputStream();
       	FileOutputStream fos=new FileOutputStream("F:\\java\\apache-tomcat-5.5\\apache-tomcat-5.5.25\\webapps\\photoServer\\WEB-INF\\aa.jpeg");
  //    	 DataOutputStream os = new DataOutputStream(fos); 
        
       //  	FileOutputStream fos=new FileOutputStream("bbc.jpeg");
          //  createIamge(io,fos);
			createIamge(in,fos);
		}
/*
	private void createIamge(InputStream io,OutputStream out){
		int width = 100;
		int height = 100;
		int[] rgb=new int[100*100];
		try{
			for(int i=0;i<rgb.length;i++){
				rgb[i]=io.read();
			}
		}catch(IOException ioe){ioe.printStackTrace();}
		BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		bi.setRGB(0,0,100,100,rgb,0,100);
		bi.flush();
		// encode:
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
		param.setQuality(1.0f, false);
		encoder.setJPEGEncodeParam(param);
		System.out.println("yeah");
		try {
			encoder.encode(bi);
		}
		catch(IOException ioe) {
 			ioe.printStackTrace();
		}

	}*/
	private void createIamge(DataInputStream io,OutputStream out){
       int width = 100;
       int height = 100;
       int[] rgb=new int[width*height];
       try{
           int[] bytes=new int[rgb.length*4];
           //io.readu(bytes);
            //rgb=byteToInt(bytes);
           for(int i=0;i<bytes.length;i++){
               bytes[i]=io.readUnsignedByte();
             }
           byteToInt(bytes,rgb);

        }catch(IOException ioe){ioe.printStackTrace();}
          BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
           bi.setRGB(0,0,100,100,rgb,0,100);
           bi.flush();
// encode:
          JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
          JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(1.0f, false);
encoder.setJPEGEncodeParam(param);
System.out.println("yeah");
try {
encoder.encode(bi);
}
catch(IOException ioe) {
 ioe.printStackTrace();
}

}
/*
private void byteToInt(int[] bytes,int[] rgb){
       for(int i=0;i<rgb.length;i++){
    rgb[i]=(
((bytes[i*4]&0xff)<<24)|
((bytes[i*4+1]&0xff)<<16)|
((bytes[i*4+2]&0xff)<<8)|
((bytes[i*4+3]&0xff)<<0));
    
      
//System.out.print(ints[i]+"\t"+(ints[i]<<8)+"\t");
if(i<100){

System.out.println(rgb[i]);

}
       }
}*/

private void byteToInt(int[] bytes,int[] rgb){
       for(int i=0;i<rgb.length;i++){
       	
    rgb[i]=(
((bytes[i*4]&0xff)<<24)|
((bytes[i*4+1]&0xff)<<16)|
((bytes[i*4+2]&0xff)<<8)|
((bytes[i*4+3]&0xff)<<0));
    
 //System.out.println(rgb[i]);     
//System.out.print(ints[i]+"\t"+(ints[i]<<8)+"\t");
if(i<100){

System.out.println(rgb[i]);

}
       }
}
/*
  private   int   toInt(byte   b){   
                  if(b   >=   0)   return   (int)b;   
                  else               return   (int)(b   +   256);   
          }   
    
          //   4   byte   Array   to   int   
          private   int   byteArray4ToInt(byte[]   byteValue){   
                  if(byteValue.length   !=   4)   
                          return   0;   
    
                  int   intValue   =   0;   
                  try{   
                          intValue   =   toInt(byteValue[0]);   
                          intValue   =   (intValue   <<   8)   +   toInt(byteValue[1]);   
                          intValue   =   (intValue   <<   8)   +   toInt(byteValue[2]);   
                          intValue   =   (intValue   <<   8)   +   toInt(byteValue[3]);   
                  }   
                  catch(Exception   e){   
                          e.printStackTrace();   
                  }   
                  return   intValue;   
          }   */

}

⌨️ 快捷键说明

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