ImageZipUtil
2015-11-26 11:48
267 查看
import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import javax.imageio.ImageIO; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGEncodeParam; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class ImageZipUtil { /** * 等比例压缩图片文件<br> * 先保存原文件,再压缩、上传 * * @param oldFile * 要进行压缩的文件 * @param newFile * 新文件 * @param width * 宽度 //设置宽度时(高度传入0,等比例缩放) * @param height * 高度 //设置高度时(宽度传入0,等比例缩放) * @param quality * 质量 <A * href='\"http://www.eoeandroid.com/home.php?mod=space&uid=7300\ * " ' target='\"_blank\"'>@return</A> 返回压缩后的文件的全路径 */ public static String zipImageFile(File oldFile, File newFile, int width, int height, float quality) { if (oldFile == null) { return null; } try { /** 对服务器上的临时文件进行处理 */ Image srcFile = ImageIO.read(oldFile); int w = srcFile.getWidth(null); // System.out.println(w); int h = srcFile.getHeight(null); // System.out.println(h); double bili; if (width > 0) { bili = width / (double) w; height = (int) (h * bili); } else { if (height > 0) { bili = height / (double) h; width = (int) (w * bili); } } /** 宽,高设定 */ BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null); // String filePrex = oldFile.getName().substring(0, // oldFile.getName().indexOf('.')); /** 压缩后的文件名 */ // newImage = filePrex + smallIcon+ // oldFile.getName().substring(filePrex.length()); /** 压缩之后临时存放位置 */ FileOutputStream out = new FileOutputStream(newFile); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag); /** 压缩质量 */ jep.setQuality(quality, true); encoder.encode(tag, jep); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return newFile.getAbsolutePath(); } /** * 按宽度高度压缩图片文件<br> * 先保存原文件,再压缩、上传 * * @param oldFile * 要进行压缩的文件全路径 * @param newFile * 新文件 * @param width * 宽度 * @param height * 高度 * @param quality * 质量 * @return 返回压缩后的文件的全路径 */ public static String zipWidthHeightImageFile(File oldFile, File newFile, int width, int height, float quality) { if (oldFile == null) { return null; } String newImage = null; try { /** 对服务器上的临时文件进行处理 */ Image srcFile = ImageIO.read(oldFile); int w = srcFile.getWidth(null); // System.out.println(w); int h = srcFile.getHeight(null); // System.out.println(h); width = (width == 0) ? w : width; height = (height == 0) ? h : height; /** 宽,高设定 */ BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null); // String filePrex = oldFile.substring(0, oldFile.indexOf('.')); /** 压缩后的文件名 */ // newImage = filePrex + smallIcon+ // oldFile.substring(filePrex.length()); /** 压缩之后临时存放位置 */ FileOutputStream out = new FileOutputStream(newFile); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag); /** 压缩质量 */ jep.setQuality(quality, true); encoder.encode(tag, jep); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return newImage; } /** * 获得文件类型,即文件扩展名 * * @param filename * @return */ public static String getFileType(String filename) { String ext = ""; if (filename != null && !"".equals(filename)) { ext = filename.substring(filename.lastIndexOf('.') + 1); } return ext; } /** * 获得文件名 * * @param filename * @return */ public static String getFileName(String filename) { String name = ""; if (filename != null && !"".equals(filename)) { name = filename .substring(filename.lastIndexOf(File.separatorChar) + 1); } return name; } /** * 创建路径. * * @param strRealPath * @param strDirName */ public static boolean creatDir(String strRealPath, String strDirName) { try { String[] sub = strDirName.split("/"); File dir = new File(strRealPath); for (int i = 0; i < sub.length; i++) { if (!dir.exists()) { dir.mkdir(); } File dir2 = new File(dir + File.separator + sub[i]); if (!dir2.exists()) { dir2.mkdir(); } dir = dir2; } return true; } catch (Exception err) { return false; } } /** * 创建路径. * * @param fileName */ public static void createPath(String fileName) { int pathpos = fileName.lastIndexOf(File.separatorChar); String path = ""; if (pathpos > 0) { path = fileName.substring(0, pathpos); File file = new File(path); if (!file.exists()) { if (!file.mkdirs()) { return; } } } } /*** * 功能 :调整图片大小 * @param srcImgPath 原图片路径 * @param distImgPath 转换大小后图片路径 * @param width 转换后图片宽度 * @param height 转换后图片高度 */ public static void resizeImage(String srcImgPath, String distImgPath, int width, int height) throws IOException { File srcFile = new File(srcImgPath); Image srcImg = ImageIO.read(srcFile); BufferedImage buffImg = null; buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); buffImg.getGraphics().drawImage( srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null); ImageIO.write(buffImg, "JPEG", new File(distImgPath)); } }
相关文章推荐
- csharp:百度翻译
- IIS部署FTP服务器步骤
- u3d 发布的程序 窗口位置的改变
- autotool 交叉编译
- 这个错误,每个ScrumMaster都犯过
- ios审核加急
- csharp:百度翻译
- 这个错误,每个ScrumMaster都犯过
- UITableVIew 编辑
- 数据分析写作——程序员的另外一种输出
- 提示框插件SweetAlert
- WebService基于SoapHeader的安全性保证
- Hibernate
- SlidingMenu如何导入Android Studio
- 浅谈Supermap iClient for JavaScript弹窗类
- 【移动开发】ListView的setSelection()和setSelectionFromTop()联系
- Linux下Mysql5.5.28的安装
- Gulp插件使用技巧
- 分享:Web应用程序的TWAIN扫描识别工具——Dynamic Web TWAIN
- resotreIpAddress