Android intent and activity经典程序(4)
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; }}
相关新闻>>
- 发表评论
-
- 最新评论 更多>>