当前位置:编程学习 > JSP >>

java 生成缩略图(四种生成方法)(1/2)

文章提供了四种利用java 生成缩略图实例代码,方法二和四是将图按比例缩小,其它生成缩略图代码少,但是不按比例生成。
<%@ page contenttype="text/html; charset=gb2312" language="java" import="java.sql.*" errorpage="" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>java 生成缩略图(四种生成方法)</title>
</head>

<body>
<%
//方法一

image src = toolkit.gettoolkit.createimage;
然后先生成一个bufferedimage bi作为画布.
bufferedimage bi = new bufferedimage(目标宽, 目标高,bufferedimage.type_int_rgb);
得到它的graphics对象:
graphics g = bi.getgraphics;
然后往这个画而上画原图就行了:
g.grawimage;
找一个编码类,如jpegencoder,gifencoder
把bi编码输出就行了.


//方法二

将图按比例缩小。
     public static bufferedimage resize(bufferedimage source, int targetw, int targeth) {
         // targetw,targeth分别表示目标长和宽
         int type = source.gettype();
         bufferedimage target = null;
         double sx = (double) targetw / source.getwidth();
         double sy = (double) targeth / source.getheight();
         //这里想实现在targetw,targeth范围内实现等比缩放。如果不需要等比缩放
         //则将下面的if else语句注释即可
         if(sx>sy)
         {
             sx = sy;
             targetw = (int)(sx * source.getwidth());
         }else{
             sy = sx;
             targeth = (int)(sy * source.getheight());
         }
         if (type == bufferedimage.type_custom) { //handmade
             colormodel cm = source.getcolormodel();
             writableraster raster = cm.createcompatiblewritableraster(targetw, targeth);
             boolean alphapremultiplied = cm.isalphapremultiplied();
             target = new bufferedimage(cm, raster, alphapremultiplied, null);
         } else
             target = new bufferedimage(targetw, targeth, type);
             graphics2d g = target.creategraphics();
             //smoother than exlax:
             g.setrenderinghint(renderinghints.key_rendering, renderinghints.value_render_quality );
             g.drawrenderedimage(source, affinetransform.getscaleinstance(sx, sy));
             g.dispose();
             return target;
         }
    
         public static void saveimageasjpg (string fromfilestr,string savetofilestr,int width,int hight)
         throws exception {
         bufferedimage srcimage;
         // string ex = fromfilestr.substring(fromfilestr.indexof("."),fromfilestr.length());
         string imgtype = "jpeg";
         if (fromfilestr.tolowercase().endswith(".png")) {
             imgtype = "png";
         }
         // system.out.println(ex);
         file savefile=new file(savetofilestr);
         file fromfile=new file(fromfilestr);
         srcimage = imageio.read(fromfile);
         if(width > 0 || hight > 0)
         {
             srcimage = resize(srcimage, width, hight);
         }
         imageio.write(srcimage, imgtype, savefile);

     }
    
     public static void main (string argv[]) {
         try{
         //参数1(from),参数2(to),参数3(宽),参数4(高)
             saveimageasjpg("c:\documents and settings\www.zzzyk.com\www.zzzyk.com\tmr-06.jpg",
                     "c:\documents and settings\www.zzzyk.com\www.zzzyk.com\2.jpg",
                     120,120);
         } catch(exception e){
             e.printstacktrace();
         }

     }

1 2
补充:Jsp教程,Java基础 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,