生成缩略图


<%@ page import="java.awt.Image" %>
<%@ page import="java.awt.image.BufferedImage" %>
<%@ page import="java.io.File" %>
<%@ page import="java.io.FileOutputStream" %>
<%@ page import="javax.imageio.ImageIO" %>
<%@ page import="com.sun.image.codec.jpeg.JPEGCodec" %>
<%@ page import="com.sun.image.codec.jpeg.JPEGImageEncoder" %>

 try {
     java.io.File file1 = new 
     java.io.File("x://"+test.jpg);
     Image img = ImageIO.read(file1);
     int imgWidth = img.getWidth(null);
     int imgHeight = img.getHeight(null);
     int zip_width = 80;
     int zip_height = 60;
     if(Float.parseFloat(imgWidth+"")/imgHeight>(4.0/3))
     {
      zip_width = zip_height*imgWidth/imgHeight;
     }
     else
     {
      zip_height = zip_width*imgHeight/imgWidth;
     }

     BufferedImage tag = new BufferedImage(zip_width,zip_height,BufferedImage.TYPE_INT_RGB);
     tag.getGraphics().drawImage(img,0,0,zip_width,zip_height,null);
     FileOutputStream fos = new FileOutputStream("x://"+ "zip_test.jpg");
     JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
     encoder.encode(tag);
     fos.close();
    } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }//添加缩略图

你可能感兴趣的:(生成缩略图)