为了正常的体验网站,请在浏览器设置里面开启Javascript功能!
首页 > java图片压缩上传

java图片压缩上传

2018-03-10 4页 doc 18KB 5阅读

用户头像

is_219945

暂无简介

举报
java图片压缩上传java图片压缩上传 java /**Java 2009-10-27 15:54:14 阅读159 评论0 字号:大中小 package com.xkshow.file; import java.awt.*; import java.io.*; import java.awt.image.*; import com.sun.image.codec.jpeg.*; import org.apache.commons.logging.LogFactory; import org.apache.commons.lo...
java图片压缩上传
java图片压缩上传 java /**Java 2009-10-27 15:54:14 阅读159 评论0 字号:大中小 package com.xkshow.file; import java.awt.*; import java.io.*; import java.awt.image.*; import com.sun.image.codec.jpeg.*; import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.Log; import org.apache.commons.io.*; public class ImgPress { private static final Log logger = LogFactory.getLog(ImgPress.class); public ImgPress() { } public static void reduceImg(String imgsrc, String imgdist, int widthdist, int heightdist, int benchmark) { //imgsrc:本地图片路径;imgdist:上传服务器路径;widthdist:压缩宽度;heightdist:压缩高度; // int benchmark说明:0,长宽哪个长,以哪个为标准;1,以宽为基准;2,以高为基准 try { //System.out.println("*******widthdist********:"+widthdist); //System.out.println("*******heightdist********:"+heightdist); //System.out.println("*******benchmark********:"+benchmark); File srcfile = new File(imgsrc); if (!srcfile.exists()) { return; } Image src = javax.imageio.ImageIO.read(srcfile); int width = src.getWidth(null); int height = src.getHeight(null); if (width <= widthdist && height <= heightdist) { // SysUtil.cpoyFile(imgsrc, imgdist); FileUtils.copyFile(new File(imgsrc), new File(imgdist)); return; } // 宽度除以高度的比例 float wh = (float) width / (float) height; if (benchmark == 0) { if (wh > 1) { float tmp_heigth = (float) widthdist / wh; BufferedImage tag = new BufferedImage(widthdist, (int) tmp_heigth, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(src, 0, 0, widthdist, (int) tmp_heigth, null); FileOutputStream out = new FileOutputStream(imgdist); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); out.close(); } else { float tmp_width = (float) heightdist * wh; BufferedImage tag = new BufferedImage((int) tmp_width, heightdist, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(src, 0, 0, (int) tmp_width, heightdist, null); FileOutputStream out = new FileOutputStream(imgdist); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); out.close(); } } if (benchmark == 1) { float tmp_heigth = (float) widthdist / wh; BufferedImage tag = new BufferedImage(widthdist, (int) tmp_heigth, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(src, 0, 0, widthdist, (int) tmp_heigth, null); FileOutputStream out = new FileOutputStream(imgdist); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); out.close(); } if (benchmark == 2) { float tmp_width = (float) heightdist * wh; BufferedImage tag = new BufferedImage((int) tmp_width, heightdist, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(src, 0, 0, (int) tmp_width, heightdist, null); FileOutputStream out = new FileOutputStream(imgdist); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); out.close(); } } catch (IOException ex) { logger.error(ex); } } } ImgPress.reduceImg(this.getUpload().getPath(), dstPath, 200, 180, 0); 本地文件的地址:this.getUpload().getPath() 上传文件的地址:dstPath
/
本文档为【java图片压缩上传】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索