为类似朋友圈的需求所做的图片压缩处理

  1 package com.goalwisdom.gwnis.util.imgCompress;
  2 import java.io.*;
  3 import java.util.ArrayList;
  4 import java.util.List;
  5 import java.awt.*;
  6 import java.awt.image.*;
  7 import javax.imageio.ImageIO;
  8
  9 import com.goalwisdom.gwnis.base.ServerConfig;
 10 import com.sun.image.codec.jpeg.*;
 11 /**
 12  * 图片压缩处理
 13  * @author 刘立文
 14  */
 15 public class ImgCompress{
 16     //服务器项目路径
 17     private String serverPath;
 18     //头像压缩前图片文件夹(项目服务器所在文件夹下载的子文件夹,即类似 image/headPath 这种)
 19     private String headPath;
 20     //头像压缩后图片目标文件夹
 21     private String goalHeadPath;
 22     //朋友圈图片压缩前文件夹
 23     private String friendPath;
 24     //朋友圈图片压缩后文件夹
 25     private String goalFriendPath;
 26     //压缩后的宽高
 27     private final static int GOAL_HEIGHT=100;
 28     private final static int GOAL_WIDTH=100;
 29
 30     /** 构造函数 */
 31     public ImgCompress(String serverPath) throws IOException {
 32         this.serverPath=serverPath;
 33         headPath=ServerConfig.getHeadPortraitImagePath();
 34         goalHeadPath=ServerConfig.getCompressionHeadPortraitImagePath();
 35         friendPath=ServerConfig.getImagePath();
 36         goalFriendPath=ServerConfig.getCompressionImagePath();
 37     }
 38
 39     /**
 40      * 方法: compressHeadImage <br>
 41      * 描述: 在头像图片文件夹中搜索对应图片,存放到压缩文件夹 <br>
 42      * 作者: liuliwen <br>
 43      * 时间: 2016-7-10 下午4:44:29
 44      * @param imageName 头像名
 45      * @throws Exception
 46      */
 47     public void compressHeadImage(String imageName) throws Exception{
 48         String imgP=serverPath+"\\"+headPath+"\\"+imageName;
 49         String gImgP=serverPath+"\\"+goalHeadPath+"\\"+imageName;
 50         compressImage(imgP, gImgP);
 51     }
 52
 53     /**
 54      * 方法: compressFriendImage <br>
 55      * 描述: 在朋友圈图片文件夹中搜索对应图片,存放到压缩文件夹 <br>
 56      * 作者: liuliwen <br>
 57      * 时间: 2016-7-10 下午4:44:39
 58      * @param imageNameList 图片名集合
 59      * @throws Exception
 60      */
 61     public void compressFriendImage(List<String> imageNameList) throws Exception{
 62         String imgP,gImgP;
 63         for(String imageName:imageNameList){
 64             imgP=serverPath+"\\"+friendPath+"\\"+imageName;
 65             gImgP=serverPath+"\\"+goalFriendPath+"\\"+imageName;
 66             compressImage(imgP, gImgP);
 67         }
 68     }
 69
 70     /**
 71      * 方法: compressImage <br>
 72      * 描述: 压缩指定图片到生成目标图片文件 <br>
 73      * 作者: liuliwen <br>
 74      * 时间: 2016-7-10 下午4:41:46
 75      * @param imgP 指定图片
 76      * @param gImgP 目标图片
 77      * @throws Exception
 78      */
 79     @SuppressWarnings("restriction")
 80     private void compressImage(String imgP,String gImgP)throws Exception{
 81         File file = new File(imgP);// 读入文件
 82         Image img = ImageIO.read(file);      // 构造Image对象
 83         BufferedImage image=resizeFix(img);
 84         File destFile = new File(gImgP);
 85         FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流
 86         // 可以正常实现bmp、png、gif转jpg
 87         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
 88         encoder.encode(image); // JPEG编码
 89         out.close();
 90     }
 91
 92     /**
 93      * 图片处理,按照宽度还是高度进行压缩
 94      * @param img  Image 图片对象
 95      * @param goalPath  String 压缩后的目标文件夹
 96      * @return 返回压缩后的图片缓存对象
 97      */
 98     private BufferedImage resizeFix(Image img) throws IOException {
 99         int width = img.getWidth(null);    // 得到源图宽
100         int height = img.getHeight(null);  // 得到源图长
101         if (width / height > GOAL_WIDTH / GOAL_HEIGHT) {
102             //以宽度为基准,等比例放缩图片
103             int h = (int) (height * GOAL_WIDTH / width);
104             return resize(GOAL_WIDTH, h,img);
105         } else {
106             //以高度为基准,等比例缩放图片
107             int w = (int) (width * GOAL_HEIGHT / height);
108             return resize(w, GOAL_HEIGHT,img);
109         }
110     }
111     /**
112      * 方法: resize <br>
113      * 描述: 调整大小,返回调整后的图片对象 <br>
114      * 作者: liuliwen <br>
115      * 时间: 2016-7-10 下午4:14:16
116      * @throws IOException
117      */
118     private BufferedImage resize(int w, int h,Image img) throws IOException {
119         // SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
120         BufferedImage image = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB );
121         image.getGraphics().drawImage(img, 0, 0, w, h, null); // 绘制缩小后的图
122         return image;
123     }
124
125
126     public static void main(String[] args){
127         try {
128             ImgCompress imgcomprss=new ImgCompress("G:\\apache-tomcat-7.0.54\\webapps\\extenddemo");
129             try {
130                 List<String> list=new ArrayList<String>();
131                 list.add("7c9b3640f8514ec48a5f5a20724dffe5.jpg");
132                 list.add("9f96e13b8b874325afda0bebede3e1a9.jpg");
133 //                imgcomprss.compressHeadImage("a6cc652ef8d34e2e8c3780510ff81c4e.jpg");
134                 imgcomprss.compressFriendImage(list);
135             } catch (Exception e) {
136                 e.printStackTrace();
137             }
138         } catch (IOException e) {
139             e.printStackTrace();
140         }
141     }
142 }

感谢 java小强 http://cuisuqiang.iteye.com/ 的原创分享,

自己加入到项目中使用,需求是实现类似朋友圈的功能,修改了一下代码,做成了一个工具类,可以分别处理单张图片(头像),和多张图片的压缩和存放。

目录统一配置在项目的配置文件application.properties中,使用一个统一的工具类ServerConfig进行读取。在用户图片上传时,异步处理图片压缩。

application.properties中的配置和读取工具类

1 #friend dynamic image
2 imagePath=image
3 compressionImage=image/compression
4 #head portrait image
5 headPortraitImage=headPortraitImage
6 compressionHeadPortraitImage=headPortraitImage/compression
 1 package com.goalwisdom.gwnis.base;
 2
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.util.Properties;
 6
 7 /**
 8  * Created by tzw on 15/2/11.
 9  */
10 public class ServerConfig {
11     private static Properties prop;
12     static{
13         prop = new Properties();
14         InputStream inputStream = Thread.currentThread()
15                 .getContextClassLoader()
16                 .getResourceAsStream("application.properties");
17         try {
18             prop.load(inputStream);
19         } catch (IOException e) {
20             e.printStackTrace();
21         }
22     }
23
24     /**
25      * 获取朋友圈图片
26      * */
27     public static String getImagePath() {
28         return prop.getProperty("imagePath");
29     }
30
31     /**
32      * 获取朋友圈压缩后图片
33      * */
34     public static String getCompressionImagePath() {
35         return prop.getProperty("compressionImage");
36     }
37
38     /**
39      * 获取头像图片
40      * */
41     public static String getHeadPortraitImagePath() {
42         return prop.getProperty("headPortraitImage");
43     }
44
45     /**
46      * 获取头像压缩后图片
47      * */
48     public static String getCompressionHeadPortraitImagePath() {
49         return prop.getProperty("compressionHeadPortraitImage");
50     }
51
52
53 }

和进行异步处理的类

 1 package com.goalwisdom.gwnis.util.imgCompress;
 2
 3 import java.io.IOException;
 4 import java.util.List;
 5
 6 public class ImgCompressThread extends Thread{
 7     private final String path;
 8     private final String imageName;
 9     private final List<String> imageNameList;
10
11     public void run(){
12         ImgCompress imgcomprss;
13         try {
14             imgcomprss = new ImgCompress(path);
15             imgcomprss.compressHeadImage(imageName);
16         } catch (IOException e) {
17             e.printStackTrace();
18         }catch (Exception e) {
19             e.printStackTrace();
20         }
21     }
22
23     public String getPath() {
24         return path;
25     }
26
27     public String getImageName() {
28         return imageName;
29     }
30
31     public List<String> getImageNameList() {
32         return imageNameList;
33     }
34
35     public ImgCompressThread(String path, String imageName) {
36         super();
37         this.path = path;
38         this.imageName = imageName;
39         this.imageNameList = null;
40     }
41
42     public ImgCompressThread(String path,List<String> imageNameList) {
43         super();
44         this.path = path;
45         this.imageName = null;
46         this.imageNameList = imageNameList;
47     }
48 }

调用代码

//压缩图片
            Thread t = new ImgCompressThread(contextPath,imageName);
            t.start();

写给自己这种伸手党,虽然没有过找人求代码,但是这种想法还是有的(万一要是有人给你写了呢),命名不怎么规范,英文不好,所以平时自己尽量多写中文注释

时间: 2024-08-11 05:58:36

为类似朋友圈的需求所做的图片压缩处理的相关文章

ios开发-日期处理(类似朋友圈,微博等的发送时间)

ios开发中,我们经常要处理从服务器获取的时间.类似朋友圈,微博这些应用.我们经常可以看到“刚刚”,“31分钟前发表”,“昨天5点”,之类的字样. 当时我们从服务器端获取的都是那条朋友圈信息,或者微博的创建时间.所以我们每次显示数据的时候,都需要根据跟当前的时间做对比,按照格式,算出 我们想要的结果. 例如,我们从服务器获取的时间,大概都类似于  Sun May 24 12:12:00 +0800 2015 我们还是先详细说明一下日期格式吧 年 y 将年份 (0-9) 显示为不带前导零的数字 y

Masonry 布局 cell 高度适应的一种方案(实现类似朋友圈简单布局)

来源:伯乐在线 - 夏天然后 链接:http://ios.jobbole.com/89298/ 点击 → 申请加入伯乐在线专栏作者 前言: 我模仿的是微博的布局所以也就没有 评论动态刷新cell. 1.什么人群适合看? 好奇Masonry使用的, 听过没用过, 没有深入的接触过的 可以看. 2.为什么要写? 很多文章都是这个原因 1 备忘 2 给需要的人 -.- 3.这篇可以了解哪些? Masonry + HYBMasonryAutoCellHeight + TTTAttributedLabel

iOS开发——项目实战总结&amp;类微信朋友圈发动态功能初步-图片与视频上传

类微信朋友圈发动态功能初步-图片与视频上传 最近在做一个新的项目,涉及到了关于图片和视频上传和显示的功能,研究了一段时间,总结一下. 使用AFNetworking上传图片(可一次上传多张图片,包含不同类型png, jpeg)和视频 1 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 2 3 AFHTTPRequestOperation *operation = [manager P

Android 仿微信朋友圈发动态功能(相册图片多选)

代码分享 代码名称: 仿微信朋友圈发动态功能(相册图片多选) 代码描述: 仿微信朋友圈发动态功能(相册图片多选) 代码托管地址: http://www.apkbus.com/android-152760-1-1.html 代码作者: 楼主 代码效果图: 本帖最后由 ^.^ 于 2014-7-8 16:23 编辑 <ignore_js_op> <ignore_js_op> <ignore_js_op> DEMO一共13个类 大约2000行代码,童鞋们耐心点看基本思路是:1

Android:NineGridLayout — 仿微信朋友圈和QQ空间的九宫格图片展示自定义控件

NineGridLayout 一个仿微信朋友圈和QQ空间的九宫格图片展示自定义控件. GitHub:https://github.com/HMY314/NineGridLayout 一.介绍 1.当只有1张图时,可以自己定制图片宽高,也可以使用默认九宫格的宽高: 2.当只有4张图时,以2*2的方式显示: 3.除以上两种情况下,都是按照3列方式显示,但这时有一些细节: a.如果只有9张图,当然是以3*3的方式显示: b.如果超过9张图,可以设置是否全部显示. 如果设置不完全显示,则按照3*3的方式

周记6——css实现类似朋友圈九宫格缩略图完美展示

公司有在做一个类似qq空间的开发,发表说说避免不了的要有图片展示. 产品提出的空间缩略图的展示类似*信朋友圈那种效果--图片不变形.能看到中间部分. 这里给出3种解决方案(jsbin地址失效时可复制代码到jsbin.com看效果): 1. img + position + translate <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="v

iOS 类似朋友圈的图片浏览器SDPhotoBrowser

SDPhotoBrowser.Demo 1.在文件SDBrowserImageView.m中有用SDWebImage到网络加载图片 需要的注释去掉即可 #import "ViewController.h" #import "SDPhotoBrowser.h" @interface ViewController ()<SDPhotoBrowserDelegate> { NSMutableArray * arrayImage; } @end @impleme

微信与朋友圈分享点击无响应/图片大小超过32k

1.微信分享不成功的原因: (1):应用是否通过审核.android平台必须要通过审核后才可以使用. (2):确认AppId是否填写正确. (3):是否是导出带签名的apk文件,ran然后再安装运行. (4):包名是否申请应用填写的包名一致. (5):签名问题.检查签名是否一致,请按照以下步骤检查: 1>:在命令行运行以下命令: keytool -list -alias aliasName -keystore xxx.keystore -storepass xxx -keypass xxx 说明

面试题:软件测试,如何测微信的朋友圈?

任何一个东西你都可以这么测:记住sfdipot: s,structure,结构.考虑其组成部分,微信朋友圈的代码组成,客户端是怎么样的,服务端是怎么样的. f,function,功能.考虑单个功能是否符合预期,比如显示一条别人发的朋友圈,又比如自己发一条朋友圈. d,data,数据.考虑每个功能对应的不同数据.比如发一条有文字有图片的朋友圈,发一条只有图片的朋友圈,显示十条新增的朋友圈信息等. i,interface,接口.考虑各内部和外部接口,比如朋友圈客户端和服务端的交互接口的功能.朋友圈和