您的位置:首页 > 编程语言 > Java开发

压缩图片大小(Java源码)

2015-11-14 09:49 477 查看
/**
*
* 直接指定压缩后的宽高:
* @param oldFile
* 要进行压缩的文件
* @param width
* 压缩后的宽度
* @param height
* 压缩后的高度
* @return 返回压缩后的文件的全路径
*/

public static File zipImageFile(File oldFile, int width, int height) {

if (oldFile == null) {
return null;
}
File newImage = null;
try {
/** 对服务器上的临时文件进行处理 */
Image srcFile = ImageIO.read(oldFile);
/** 宽,高设定 */
BufferedImage tag = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null);

/** 压缩后的文件名 可以再自定义 */
newImage = oldFile;

/** 压缩之后临时存放位置 */
FileOutputStream out = new FileOutputStream(newImage);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);

/** 压缩质量 */

jep.setQuality(90, true);
encoder.encode(tag, jep);
out.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return newImage;

}


  

/**
* 方法描述 上传图片
*/
public void uploadImage() {
long beginTime = System.currentTimeMillis();
HttpServletResponse response = ServletActionContext.getResponse();
try {

response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("utf-8");
ResultMsg msg = new ResultMsg();
FileInputStream fis = null;
OutputStream outputStream = null;
try {
String tempPath = "/upload/wechatshake/"+ getUserInfoSession().getAccount()+ "/";
// 生成上传文件
String path = ServletActionContext.getServletContext().getRealPath(tempPath)+"/";

File dir = new File(path);
if (!dir.exists()) {// 判断目录是否存在,否则创建
dir.mkdirs();
}
File file = ImageUtil.zipImageFile(getImage(),140,140); //压缩图片

if (file != null) {
Random random = new Random();
String saveUploadFileName = "upload"
+ System.currentTimeMillis()+ random.nextInt(1000)
+ imageFileName.substring(imageFileName.lastIndexOf("."),imageFileName.length());

fis = new FileInputStream(file);
outputStream = new FileOutputStream(new File(path,saveUploadFileName));
//byte[] buffer = new byte[1024];
int len = 0;
while ((len=fis.read())!=-1) {
outputStream.write(len);
}
//将图片上传到微信端
UserInfoWechat userInfoWechat = iUserInfoWechatServic.getByUser(getUserInfoSession().getId());
String token = iWeixinService.getAccessToken(userInfoWechat);
Material material = new Material();
material.setType("icon");
material.setMedia(tempPath+saveUploadFileName);
material = iWxActivityService.uploadMaterial(material, token);
logger.info("上传图片微信接口返回数据:"+material);
if(null != material){
List<String> list = new ArrayList<String>();
list.add(material.getData().getPic_url());
msg.setDataList(list);
msg.setCode("0");
}else {
msg.setCode("1");
msg.setDesc("发布失败");
}
msg.setDesc(SUCCESS);
}

} catch (Exception e) {
error("上传文件失败", e);
msg.setCode("1");
msg.setDesc(ERROR);
} finally {
try {
if(fis!=null){fis.close();}
if(outputStream!=null){outputStream.close();}
} catch (IOException e) {
e.printStackTrace();
}
writeResult(msg);
}
} catch (Exception e) {
error("上传文件失败", e);
} finally {
printTime(beginTime, getClass(), "uploadImage");
}
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: