JAVA画图方式一

    技术2025-10-29  10

    <!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"><%@ page contentType="text/html; charset=UTF-8" %><%@ page language="java" %><%@ page import= "javax.sql.DataSource "%> <%@ page import="java.sql.*" %><%@ page import="java.util.*" %><%@ page import="java.util.regex.Matcher" %><%@ page import="java.util.regex.Pattern" %><%@ page language="java" %><%@ page import= "java.io.* "%> <%@ page import= "java.net.* "%> <%@ page import= "javax.naming.* "%> <%@ page import= "javax.sql.DataSource "%> <%@ page import="java.sql.*" %><%@ page import="java.util.Date" %><%@ page

    import="java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.*,java.sql.*,com.jspsmart.upload.*,java.util.*,cn.oof.da

    tabase.*,cn.oof.house.*"%><%@ page import="javax.imageio.*" %><%@ page import="java.net.URL" %><%@ page import="java.net.URLConnection.*"%><%@ page import="java.io.IOException.*"%><% int width=0;int height=0;  String path=request.getRealPath("/");  String newdir=path+"/pic/";  String largepic="/pic/11.jpg";  String newurl=newdir+"/22.jpg";  java.io.File dir= new java.io.File(newdir);       java.net.URL url = new URL("http://img.tianyablog.com/photo/2005/4/30/573236_144734.jpg");      Image src = javax.imageio.ImageIO.read(url);    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();   long len = urlConnection.getContentLength();   BufferedImage bi = null;    bi = javax.imageio.ImageIO.read(url);   if(bi!=null){     width = bi.getWidth();      height = bi.getHeight(); //获得 高度    }   long size=0;   if(len>0){   size=len/1024;   }    if(size>500){     largepic="/pic/noupload.jpg";    }    else{       if(size<100 && width<550){        javax.imageio.ImageIO.write(bi, "jpg", new java.io.File(newurl));       }       else{     float tagsize=0;       if(width>550){tagsize=550;}       else{tagsize=width;}     int old_w=width;                                      //得到源图宽     int old_h=height;        int new_width=0;     int new_height=0;                             //得到源图长     int tempsize;     float tempdouble;      if(old_w>old_h){      tempdouble=old_w/tagsize;     }else{      tempdouble=old_h/tagsize;     }     new_width=Math.round(old_w/tempdouble);     new_height=Math.round(old_h/tempdouble);//计算新图长宽       Image largeimage = src.getScaledInstance(new_width, new_height, Image.SCALE_DEFAULT);           /*获得缩略图 返回一个100*100的Image对象*/       /*再创建一个BufferedImage对象 用于创建100*100大小的图像*/       BufferedImage oimage;       oimage = new BufferedImage(new_width, new_height, Image.SCALE_DEFAULT);       //获取图像上下文对象,然后把刚才的Image对象画到BufferedImage中去       oimage.getGraphics().drawImage(largeimage, 0, 0, null);       /*最后把图片以JPED写入*/       java.io.File largefile = new java.io.File(newurl);       try {           ImageIO.write(oimage, "jpeg", largefile);       } catch (IOException e) {           e.printStackTrace();       }}}%>

    最新回复(0)