内容含:图片处理工具类;以图片压缩为例 ,编写调用实例
第一步:框架中加入配置文件application.properties图片压缩后指定的高宽在此配置文件中配置
内容为:
- image.width=150
- image.height=150
第二步:框架中加入配置文件加载操作类,详情见:http://lucien-zzy.iteye.com/admin/blogs/2009495
第三步:编写图片处理工具类
- /**
- * 图片处理工具
- * @author ziyuzhang
- *
- */
- public class ImageUtil {
- private static final int BUFFER_SIZE = 16 * 1024;
- /**
- * 处理图片并保存 一张原图和一张缩小后的图片 小图可用于手机端
- * @param upload 大图对象
- * @param uploadFileName 图片原名
- * @param webPath 工程部署的绝对地址
- * @param filePath 图片目录
- * @return 为一字符数字,0位置 为原图片位置,1位置为压缩后图片位置,2位置为压缩后图片高度,3位置为压缩后图片宽度,4位置为压缩后图片大小
- */
- public static String[] uploadImages(File upload, String uploadFileName,String webPath,String filePath) {
- StringTokenizer tokenizer = new StringTokenizer(uploadFileName, ".");
- String ext="";
- while(tokenizer.hasMoreTokens()){
- ext = tokenizer.nextToken();
- }
- //大图的名字
- String filename = ImageUtil.getUUID()+"."+ext;
- //保存大图
- if(!ImageUtil.saveFile(upload,webPath,filePath,filename)){
- return null;
- }
- String afterFileName = ImageUtil.getUUID();
- //小图的名字
- String smallname = afterFileName + "." + ext;
- String smallPath = webPath + filePath + smallname;
- // 产生一张新的截图
- String[] fileinfo = ImageUtil.resizeImage(upload, smallPath,ConfigUtil.getIntValue("image.width"),
- ConfigUtil.getIntValue("image.height"),ext);
- if(null == fileinfo){
- return new String[]{"/" + filePath + filename,"/" + filePath + filename,
- null,null,null};
- }else{
- return new String[]{"/" + filePath + filename,"/" + filePath + smallname,
- fileinfo[0],fileinfo[1],fileinfo[2]};
- }
- }
- /**
- * 对应图片key为 upload
- * 保存附件 限制大小100M
- * @param response
- * @param request
- * @return
- */
- public static String getfile(HttpServletResponse response,HttpServletRequest request) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
- // 根据服务器的文件保存地址找到项目部署的绝对地址
- String webPath = ServletActionContext.getServletContext().getRealPath("/") ;
- String filePath ="upload/"+sdf.format(new Date())+"/";
- //文件保存目录路径
- String savePath = webPath+ "upload/";
- //最大文件大小
- long maxSize = 1024*1024*100;
- response.setContentType("text/html; charset=UTF-8");
- if (!ServletFileUpload.isMultipartContent(request)) {
- return "erro";
- }
- //如果目录不存在则创建
- if (!(new File(savePath).exists())) {
- if (!(new File(savePath).mkdirs())) {
- return "erro";
- }
- }
- //检查目录写权限
- if (!(new File(savePath)).canWrite()) {
- return "erro";
- }
- MultiPartRequestWrapper mul = (MultiPartRequestWrapper)request;
- File[] imageFiles = mul.getFiles("upload");
- String[] filesss = mul.getFileNames("upload");
- if (imageFiles != null && imageFiles.length >0) {
- String fileName = filesss[0];
- long fileSize = imageFiles[0].length();
- //检查文件大小
- if (fileSize > maxSize) {
- return "erro";
- }
- //检查扩展名
- String fileExt = fileName.substring(
- fileName.lastIndexOf(".") + 1).toLowerCase();
- SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
- String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
- //保存图片到硬盘
- ImageUtil.saveFile(imageFiles[0], webPath, filePath, newFileName);
- filePath = webPath + filePath+newFileName ;
- }
- return filePath;
- }
- /**
- * 将文件保存到制定位置,路径不存在自动创建
- *
- * @param file
- * 要保存的文件
- * @param webPath
- * 工程部署的绝对路径
- * @param filePath
- * 文件夹的相对路径
- * @param filename
- * 文件名
- * @return
- */
- public static boolean saveFile(File file, String webPath, String filePath,
- String filename) {
- if (new File(webPath + filePath).exists()) {
- webPath = webPath + filePath + "/" + filename;
- File dstFile = new File(webPath);
- if (copy(file, dstFile)) {
- return true;
- }
- } else {
- if (new File(webPath + filePath).mkdirs()) {
- webPath = webPath + filePath + "/" + filename;
- File dstFile = new File(webPath);
- if (copy(file, dstFile)) {
- return true;
- }
- }
- }
- return false;
- }
- /**
- * 把源文件对象复制成目标文件对象
- *
- * @param src
- * 源文件
- * @param dst
- * 目标文件
- * @return
- */
- public static boolean copy(File src, File dst) {
- boolean result = false;
- InputStream in = null;
- OutputStream out = null;
- try {
- in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
- out = new BufferedOutputStream(new FileOutputStream(dst),
- BUFFER_SIZE);
- byte[] buffer = new byte[BUFFER_SIZE];
- int len = 0;
- while ((len = in.read(buffer)) > 0) {
- out.write(buffer, 0, len);
- }
- result = true;
- } catch (Exception e) {
- e.printStackTrace();
- result = false;
- } finally {
- if (null != in) {
- try {
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (null != out) {
- try {
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return result;
- }
- /**
- * 接收File输出图片
- * 以图片高为标准 按比例缩减图片
- * @param file
- * 原图片对象
- * @param writePath
- * 小图片存放的路径
- * @param width
- * 宽
- * @param height
- * 高
- * @param format
- * 图片格式
- * @return
- */
- public static String[] resizeImage(File file, String writePath,
- Integer width, Integer height, String format) {
- try {
- BufferedImage inputBufImage = ImageIO.read(file);
- inputBufImage.getType();
- System.out.println("转前图片高度和宽度:" + inputBufImage.getHeight() + ":"
- + inputBufImage.getWidth());
- if(height >=inputBufImage.getHeight() || width >= inputBufImage.getWidth()){
- return null;
- }else{
- //double dd = inputBufImage.getHeight() / height;
- //width = (int) (inputBufImage.getWidth() / dd);
- //height = (int) (inputBufImage.getHeight() / dd);
- // 转换
- ResampleOp resampleOp = new ResampleOp(width, height);
- BufferedImage rescaledTomato = resampleOp.filter(inputBufImage,null);
- File fil = new File(writePath);
- ImageIO.write(rescaledTomato, format,fil);
- System.out.println("转后图片高度和宽度:" + rescaledTomato.getHeight() + ":"
- + rescaledTomato.getWidth());
- return new String[]{rescaledTomato.getHeight()+"",rescaledTomato.getWidth()+"",fil.length()+""};
- }
- } catch (IOException e) {
- e.printStackTrace();
- return null;
- }
- }
- public static String lastname(String name) {
- String[] ls = name.split("\\.");
- return ls[ls.length - 1];
- }
- public static String getUUID() {
- return UUID.randomUUID().toString().replaceAll("-", "");
- }
- }
第四步:Action中调用工具中的图片压缩功能
属性:
- // 图片文件上传
- private File[] image;
- private String[] imageFileName;
- private String[] imageContentType;
并提供get set
具体调用:
- Nursetb nursetb = new Nursetb();
- // 根据服务器的文件保存地址找到项目部署的绝对地址
- String webPath = ServletActionContext.getServletContext().getRealPath("/");
- System.out.println(webPath);
- String filePath = "upload/nurimage/"
- + new SimpleDateFormat("yyyyMMdd").format(new Date()) + "/";
- String names[] = null;
- if(null != this.getImage() && this.getImage()[0].length() > 0){
- names = ImageUtil.uploadImages(this.getImage()[0],
- this.getImageFileName()[0], webPath, filePath);
- }
- String imgurl = null;
- if(null!=names){
- imgurl = request.getContextPath()+names[1];
- nursetb.setImgurl(imgurl);
- }
相关推荐
应用ImageUtil进行图片压缩生成缩略图的相关源代码
Android 图片和字节数组相互转换、图片保存工具类
IOS 图片渲染第三方类,可以直接有13个效果,直接导入就可以使用
D:\002 我的工具类\010 操作图片\整体图片操作\ImageUtil.java D:\002 我的工具类\010 操作图片\整体图片操作\OperateImage.java D:\002 我的工具类\010 操作图片\整体图片操作\Quant.java D:\002 我的工具类\010 ...
ImageUtil 图片高保真缩放与裁剪,不依赖于任何第三方库
图片工具类,可以保存图片到SD卡,将图片缓存到SD卡,从网上加载图片,MD5加密
1.此工具类为整理工具类,可能会有重复,大家可一起维护整理,添加和删除一些常用的工具类,并写上注释. 2.希望大家积极整理维护,争取创建一个强大的android工具类. 3.本项目已上传到jitpack.io,使依赖更方便. 4.依赖jar...
图片自动压缩成设置的大小,根据参数配置,保持图片不失真,使用的java自带的图片处理类,工具类直接运行main方法
Android imageUtil 裁剪 基于第三方开源修改,希望对您有帮助。
Studio开发安卓APP,使用C++(OpenCV)编写图片处理算法。主要处理图片的对比度,饱和度以及对图片进行一定程度的清晰度增强。 在网上找了许多关于开发Android APP时引入OpenCV库的资料,大多数都只说了导入OpenCV...
ImageUtil提供 图片转换为tif格式 和 获取图片类型 ScaleImageUtils提供图片的缩放方法 TesseractOCR使用惠普开发的图像识别类库tesseract-ocr识别图片中的文字,可以用于识别验证码 LogUtil日志工具 AES256...
Android实现手机拍照和手机相册选取图片的工具类
3、将source文件夹下的ImageUtil类拷贝到新建的包smart.css.util里面。 4、将JSP文件夹里面的jsp页面拷贝到工程的WebRoot下面。 5、将Enviroument里面的jmagick.jar拷贝到WebRoot-->Web-Info-->lib里面。 6、将...
java_utils常用工具类打包,欲善其事必先利其器,本资源内容为Java开发常用工具类打包,如BeanUtil,ArrayUtil,HtmlUtil,ClassUtil,DateUtil,FileUtil,FontUtil,ImageUtil,IOUtil, IPUtil, MapUtil,MailUtil,...
在手机应用程序开发中,常会需要加载一些图片,许多图片在手机屏幕上显示时会占用很大一块区域, 这时,调用ImageUtil中的方法可实现图片缩小或放大效果。获取适当大小的图片
图片处理类 ImageUtil.cs 缩略图、水印等操作 授权类 Licence.cs 生成授权码,验政授权码 链接辅助类 ListBuilder.cs 动态化url,target,css等属性 分页类 PageListUtil.cs 分页使用 反射辅助类 ReflectHelper.cs 获取...