Android intent and activity经典程序(4)

来源:未知 责任编辑:责任编辑 发表时间:2014-01-26 21:59 点击:


Java代码 
package action;import java.awt.image;import java.awt.toolkit;import java.awt.image.bufferedimage;import java.awt.image.cropimagefilter;import java.awt.image.filteredimagesource;import java.awt.image.imagefilter;import java.awt.image.imageproducer;import java.io.bufferedinputstream;import java.io.bufferedoutputstream;import java.io.file;import java.io.fileinputstream;import java.io.fileoutputstream;import java.io.ioexception;import java.io.inputstream;import java.io.outputstream;import java.util.arraylist;import java.util.date;import java.util.list;import java.util.map;import java.util.random;import javax.imageio.imageio;import javax.servlet.http.httpservletrequest;import org.apache.struts2.servletactioncontext;import po.blog;import po.login;import po.photo;import po.userinfo;import service.qqrservice;import util.imagehepler;import com.opensymphony.xwork2.actioncontext;import com.sun.image.codec.jpeg.jpegcodec;import com.sun.image.codec.jpeg.jpegimageencoder;public class kindeditoraction extends baseaction {  private static final long serialversionuid = 1l;    private qqrservice qqrservice;  // 封装参数 private file imgfile;   private string imgfilefilename; private string imgfilecontenttype;  private string filedir = "/blogimg";    private string imgtitle = "";   private string align;   private int imgwidth = 0;   private int imgheight = 0;  // 上传kindeditor 图片及压缩   @suppresswarnings("deprecation")    public string uploadimg() throws exception {        // todo auto-generated method stub      // 只能传图片        try {           if (this.imgfile != null) {             file dir = new file(servletactioncontext.getrequest()                       .getrealpath(filedir));             if (!dir.exists())                  dir.mkdir();                integer login_id = (integer) actioncontext.getcontext()                     .getsession().get("login_id");              date dt = new date();               file dst = new file(dir.getabsolutepath() + file.separator                      + login_id + new date().gettime() + ".jpg");                this.resize_image(this.imgfile, dst);               string id = "context";              string url = servletactioncontext.getrequest().getcontextpath()                     + "/" + this.filedir + "/" + dst.getname();             string border = "0";                string result = "<script type='text/javascript'>";                result += "parent.ke.plugin['image'].insert('" + id + "','"                     + url + "','" + imgtitle + "','" + imgheight + "','"                        + imgheight + "','" + border + "','" + align + "');   ";                result += "</script>";                servletactioncontext.getresponse().getwriter().write(result);           } else {                return null;            }       } catch (runtimeexception e) {          // todo auto-generated catch block          e.printstacktrace();        }       return null;    }   // 自己封装的一个把源文件对象复制成目标文件对象   public boolean copy(file src, file dst) {       inputstream in = null;      outputstream out = null;        try {           in = new bufferedinputstream(new fileinputstream(src), 1024);           out = new bufferedoutputstream(new fileoutputstream(dst), 1024);            byte[] buffer = new byte[1024];         int len = 0;            while ((len = in.read(buffer)) > 0) {                out.write(buffer, 0, len);          }           return true;        } catch (exception e) {         e.printstacktrace();            return false;       } finally {         if (null != in) {               try {                   in.close();             } catch (ioexception e) {                   e.printstacktrace();                }           }           if (null != out) {              try {                   out.close();                } catch (ioexception e) {                   e.printstacktrace();                }           }       }   }   /**  * 图像压缩  *   * @param fi大图文件     * @param fo将要转换出的小图文件   */ public boolean resize_image(file fi, file fo) {     try {           // file fi = new file("e:/3.jpg"); // 大图文件          // file fo = new file("e:/333.jpg"); // 将要转换出的小图文件          // affinetransform transform = new affinetransform();           bufferedimage bis = imageio.read(fi);           int w = bis.getwidth();         // system.out.println(w);           int h = bis.getheight();            // system.out.println(h);           // double scale = (double) w / h;           int nw = w;         int nh = h;         bufferedimage tag = new bufferedimage(nw, nh,                   bufferedimage.type_int_rgb);            this.imgwidth = tag.getwidth();         this.imgheight = tag.getheight();           if (this.imgwidth > 250)             this.imgwidth = 250;            if (this.imgheight >= 250)               this.imgheight = 250;           tag.getgraphics().drawimage(bis, 0, 0, nw, nh, null); // 绘制缩小后的图            // 转换为32*32 jpg格式           fileoutputstream newimage = new fileoutputstream(fo); // 输出到文件流         jpegimageencoder encoder = jpegcodec.createjpegencoder(newimage);           encoder.encode(tag);            newimage.close();           return true;        } catch (exception e) {         e.printstacktrace();            return false;       }   }   public void imageshow(int width, int height, int w, int h) {        int i = width;      int j = height;     width = w;      height = h;     if (i > w) {         float width_bili = w / i;           float height_bili = h / j;          float bili = width_bili;            if (width_bili < height_bili) {              bili = width_bili;          } else {                bili = height_bili;         }           i = (int) (i * bili);           j = (int) (j * bili);       } else {            if (j > h) {             float bili = (float) (h / j);               i = (int) (i * bili);               j = (int) (j * bili);           }       }       width = i;      height = j; }   /**  * 图像的缩略     *   *   * width 输出图片大小 height   *   * @param fi大图文件     * @param fo将要转换出的小图文件   */ public boolean resize_image(file fi, file fo, int width, int height) {      try {           bufferedimage bis = imageio.read(fi);           int w = bis.getwidth();         int h = bis.getheight();            int nw = width;         int nh = height;            bufferedimage tag = new bufferedimage(nw, nh,                   bufferedimage.type_int_rgb);            tag.getgraphics().drawimage(bis, 0, 0, nw, nh, null); // 绘制缩小后的图            // 转换为32*32 jpg格式           fileoutputstream newimage = new fileoutputstream(fo); // 输出到文件流         jpegimageencoder encoder = jpegcodec.createjpegencoder(newimage);           encoder.encode(tag);            newimage.close();           return true;        } catch (exception e) {         e.printstacktrace();            return false;       }   }   /**  * 图像的等比缩略   *   *   * width 输出图片大小 height   *   * @param fi大图文件     * @param fo将要转换出的小图文件   */ public boolean geometric_image(file fi, file fo, int width, int height) {       try {           bufferedimage bis = imageio.read(fi);           int w = bis.getwidth();         int h = bis.getheight();            int nw = w;         int nh = h;         if (w > width) {             nw = width;             nh = nw * h / w;                if (nh > height) {                   nh = height;                    nw = nh * w / h;                }           } else if (h > height) {             nh = height;                nw = nh * w / h;                if (nw > width) {                    nw = width;                 nh = nw * h / w;                }           }           bufferedimage tag = new bufferedimage(nw, nh,                   bufferedimage.type_int_rgb);            tag.getgraphics().drawimage(bis, 0, 0, nw, nh, null); // 绘制缩小后的图            // 转换为32*32 jpg格式           fileoutputstream newimage = new fileoutputstream(fo); // 输出到文件流         jpegimageencoder encoder = jpegcodec.createjpegencoder(newimage);           encoder.encode(tag);            newimage.close();           return true;        } catch (exception e) {         e.printstacktrace();            return false;       }   }   public string getimgtitle() {       return imgtitle;    }   public void setimgtitle(string imgtitle) {      this.imgtitle = imgtitle;   }   public string getalign() {      return align;   }   public void setalign(string align) {        this.align = align; }   public qqrservice getqqrservice() {     return qqrservice;  }   public void setqqrservice(qqrservice qqrservice) {      this.qqrservice = qqrservice;   }   public file getimgfile() {      return imgfile; }   public void setimgfile(file imgfile) {      this.imgfile = imgfile; }   public string getimgfilefilename() {        return imgfilefilename; }   public void setimgfilefilename(string imgfilefilename) {        this.imgfilefilename = imgfilefilename; }   public string getimgfilecontenttype() {     return imgfilecontenttype;  }   public void setimgfilecontenttype(string imgfilecontenttype) {      this.imgfilecontenttype = imgfilecontenttype;   }   public string getfiledir() {        return filedir; }   public void setfiledir(string filedir) {        this.filedir = filedir; }} 

发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码:点击我更换图片
最新评论 更多>>

推荐热点

  • Android 完全退出程序
  • 原创:Android应用开发-Andorid歌词秀,含源码
  • android 屏幕保护
  • Android手机软件汉化教程---第四课 dex文件汉化
  • 众多Android 开源项目推荐,给力工作给力学习
  • Android Audio代码分析4
  • Android得到已安装的应用程序信息!
  • Android开发者指南(29) —— USB Host and Accessory
  • Android成长的幕后推手:工程师鲁宾
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
Copyright © 2008-2015 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1