阿里的图片剪裁工具类包含以下7个方法,这都好理解,关键是这个jar包的maven依赖不好找,有人卖这个依赖已经卖到50个下载币,黑心啊,我找了很久终于找到了。

1.按固定长宽进行缩放

2.按固定文件大小进行缩放

3.等比例缩放,以宽或高较大者达到指定长度为准

4.等比例图片压缩,以宽或高较大者达到指定长度为准

5.先等比例缩放,小边缩放至指定长度后, 大边直接裁剪指指定长度

6.先等比例缩放,小边缩放至指定长度后, 大边直接裁剪指指定长度

7.从中间裁切需要的大小

因为jar包冲突,我排除了日志包,你们随意

<!-- 剪裁图片 --><dependency><groupId>javax.media</groupId><artifactId>jai-core</artifactId><version>1.1.3</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>simpleimage</artifactId><version>1.2.3</version><exclusions><exclusion><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId></exclusion></exclusions></dependency>

顺便把工具类也贴出来吧

package com.util;
import java.awt.Color;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.media.jai.PlanarImage;
import org.apache.commons.io.IOUtils;
import com.alibaba.simpleimage.ImageFormat;
import com.alibaba.simpleimage.ImageWrapper;
import com.alibaba.simpleimage.SimpleImageException;
import com.alibaba.simpleimage.render.CropParameter;
import com.alibaba.simpleimage.render.ScaleParameter;
import com.alibaba.simpleimage.render.WriteParameter;
import com.alibaba.simpleimage.render.ScaleParameter.Algorithm;
import com.alibaba.simpleimage.util.ImageCropHelper;
import com.alibaba.simpleimage.util.ImageReadHelper;
import com.alibaba.simpleimage.util.ImageScaleHelper;
import com.alibaba.simpleimage.util.ImageWriteHelper;/*** 图片的压缩、裁剪*/
public class ImgUtils {public static void main(String[] args) throws Exception {// 输入输出文件路径/文件String src = "D:\\1.jpg";String res = "D:\\2.jpg";File srcFile = new File(src);File destFile = new File(res);// 将输入文件转换为字节数组byte[] bytes = getByte(srcFile);// 构造输入输出字节流ByteArrayInputStream is = new ByteArrayInputStream(bytes);ByteArrayOutputStream os = new ByteArrayOutputStream();// 处理图片zoomAndCut2(is, os, 600, 600);// 将字节输出流写到输出文件路径下writeFile(os, destFile);}/*** 按固定长宽进行缩放* @param is      输入流* @param os      输出流* @param width   指定长度* @param height  指定宽度* @throws Exception*/public static void zoomImage(InputStream is, OutputStream os, int width, int height) throws Exception {//读取图片BufferedImage bufImg = ImageIO.read(is); is.close();//获取缩放比例double wRatio = width * 1.0/ bufImg.getWidth();     double hRatio = height * 1.0 / bufImg.getHeight();AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(wRatio, hRatio), null);BufferedImage bufferedImage = ato.filter(bufImg, null);//写入缩减后的图片ImageIO.write(bufferedImage, "jpg", os);}/*** 按固定文件大小进行缩放* @param is     输入流* @param os     输出流* @param size   文件大小指定* @throws Exception*/public static void zoomImage(InputStream is, OutputStream os, Integer size) throws Exception {/*FileInputStream的available()方法返回的是int类型,当数据大于1.99G(2147483647字节)后将无法计量,故求取流文件大小最好的方式是使用FileChannel的size()方法,其求取结果与File的length()方法的结果一致参考:http://blog.csdn.net/chaijunkun/article/details/22387305*/int fileSize = is.available();//文件大于size时,才进行缩放。注意:size以K为单位if(fileSize < size * 1024){return;}   // 获取长*宽(面积)缩放比例double sizeRate = (size * 1024 * 0.5) / fileSize; // 获取长和宽分别的缩放比例,即面积缩放比例的2次方根double sideRate = Math.sqrt(sizeRate);BufferedImage bufImg = ImageIO.read(is);AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(sideRate, sideRate), null);BufferedImage bufferedImage = ato.filter(bufImg, null);ImageIO.write(bufferedImage, "jpg", os);}/*** 等比例缩放,以宽或高较大者达到指定长度为准* @param src      输入文件路径* @param dest     输出文件路径* @param width    指定宽* @param height   指定高*/public static void zoomTo400(String src, String dest, Integer width, Integer height){try {File srcFile = new File(src);File destFile = new File(dest);BufferedImage bufImg = ImageIO.read(srcFile);int w0 = bufImg.getWidth();int h0 = bufImg.getHeight();// 获取较大的一个缩放比率作为整体缩放比率double wRatio = 1.0 * width / w0;double hRatio = 1.0 * height / h0;double ratio = Math.min(wRatio, hRatio);// 缩放AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);BufferedImage bufferedImage = ato.filter(bufImg, null);// 输出ImageIO.write(bufferedImage, dest.substring(dest.lastIndexOf(".")+1), destFile);} catch (IOException e) {e.printStackTrace();} }/*** 等比例图片压缩,以宽或高较大者达到指定长度为准* @param is     输入流* @param os     输出流* @param width  宽* @param height 高* @throws IOException*/public static void changeSize(InputStream is, OutputStream os, int width, int height) throws IOException {BufferedImage bis = ImageIO.read(is); // 构造Image对象is.close();int srcWidth = bis.getWidth(null);   // 得到源图宽int srcHeight = bis.getHeight(null); // 得到源图高if (width <= 0 || width > srcWidth) {width = bis.getWidth();}if (height <= 0 || height > srcHeight) {height = bis.getHeight();}// 若宽高小于指定最大值,不需重新绘制if (srcWidth <= width && srcHeight <= height) {ImageIO.write(bis, "jpg", os);os.close();} else {double scale =((double) width / srcWidth) > ((double) height / srcHeight) ?((double) height / srcHeight): ((double) width / srcWidth);width = (int) (srcWidth * scale);height = (int) (srcHeight * scale);BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);bufferedImage.getGraphics().drawImage(bis, 0, 0, width, height, Color.WHITE, null); // 绘制缩小后的图ImageIO.write(bufferedImage, "jpg", os);os.close();}}/*** 先等比例缩放,小边缩放至指定长度后, 大边直接裁剪指指定长度* @param is* @param os* @param width* @param height*/public final static void zoomAndCut1(InputStream is, OutputStream os, int width, int height) throws SimpleImageException {// 读文件ImageWrapper imageWrapper = ImageReadHelper.read(is);int w = imageWrapper.getWidth();int h = imageWrapper.getHeight();double wRatio = 1.0 * width / w;double hRatio = 1.0 * height / h;double ratio = Math.max(wRatio, hRatio);/*1.缩放*/// 缩放参数  如果图片宽和高都小于目标图片则不做缩放处理ScaleParameter scaleParam = null; if (w < width && h < height) {scaleParam = new ScaleParameter(w, h, Algorithm.LANCZOS);}// 为防止强转int时小数部分丢失,故加1,防止出现异常错误scaleParam = new ScaleParameter((int)(w * ratio) + 1, (int)(h * ratio) + 1, Algorithm.LANCZOS);// 缩放PlanarImage planarImage = ImageScaleHelper.scale(imageWrapper.getAsPlanarImage(), scaleParam);/*2.裁切*/// 获取裁剪偏移量imageWrapper = new ImageWrapper(planarImage);int w2 = imageWrapper.getWidth();int h2 = imageWrapper.getHeight();int x = (w2 - width) / 2;int y = (h2 - height) / 2;// 裁切参数   如果图片宽和高都小于目标图片则处理CropParameter cropParam = new CropParameter(x, y, width, height);if (x < 0 || y < 0) {cropParam = new CropParameter(0, 0, w, h);}// 裁剪planarImage = ImageCropHelper.crop(planarImage, cropParam);/*输出*/imageWrapper = new ImageWrapper(planarImage);String prefix = "jpg";ImageWriteHelper.write(imageWrapper, os, ImageFormat.getImageFormat(prefix), new WriteParameter());}/*** 先等比例缩放,小边缩放至指定长度后, 大边直接裁剪指指定长度* @param is* @param os* @param width* @param height*/public static void zoomAndCut2(InputStream is, OutputStream os, Integer width, Integer height) throws IOException, SimpleImageException{// 读文件BufferedImage bufferedImage = ImageIO.read(is);int w = bufferedImage.getWidth();int h = bufferedImage.getHeight();// 获取缩放比例double wRatio = 1.0 * width / w;double hRatio = 1.0 * height / h;double ratio = Math.max(wRatio, hRatio);// 缩放AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);bufferedImage = ato.filter(bufferedImage, null);// 对象转换ImageWrapper imageWrapper = new ImageWrapper(bufferedImage);// 获得裁剪偏移量int w2 = imageWrapper.getWidth();int h2 = imageWrapper.getHeight();float x = (w2 - width) / 2.0f;float y = (h2 - height) / 2.0f;// 裁剪参数   如果图片宽和高都小于目标图片则处理CropParameter cropParameter = new CropParameter(x, y, width, height);if (x < 0 && y < 0) {cropParameter = new CropParameter(0, 0, width, height);}PlanarImage crop = ImageCropHelper.crop(imageWrapper.getAsPlanarImage(), cropParameter);imageWrapper = new ImageWrapper(crop);// 后缀String prefix = "jpg";// 写文件ImageWriteHelper.write(imageWrapper, os, ImageFormat.getImageFormat(prefix), new WriteParameter());}/*** 从中间裁切需要的大小* @param is* @param os* @param width* @param height*/public static void CutCenter(InputStream is, OutputStream os, Integer width, Integer height) {try {ImageWrapper imageWrapper = ImageReadHelper.read(is);int w = imageWrapper.getWidth();int h = imageWrapper.getHeight();int x = (w - width) / 2;int y = (h - height) / 2;CropParameter cropParam = new CropParameter(x, y, width, height);// 裁切参数if (x < 0 || y < 0) {cropParam = new CropParameter(0, 0, w, h);// 裁切参数}PlanarImage planrImage = ImageCropHelper.crop(imageWrapper.getAsPlanarImage(), cropParam);imageWrapper = new ImageWrapper(planrImage);String prefix = "JPG";ImageWriteHelper.write(imageWrapper, os, ImageFormat.getImageFormat(prefix), new WriteParameter());} catch (Exception e) {e.printStackTrace();} finally {IOUtils.closeQuietly(is);}}/*** 将file文件转为字节数组* @param file* @return*/public static byte[] getByte(File file){byte[] bytes = null;try {FileInputStream fis = new FileInputStream(file);bytes = new byte[fis.available()];fis.read(bytes);fis.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return bytes;}/*** 将字节流写到指定文件* @param os* @param file*/public static void writeFile(ByteArrayOutputStream os, File file){FileOutputStream fos = null;try {byte[] bytes = os.toByteArray();if (file.exists()) {file.delete();}fos = new FileOutputStream(file);fos.write(bytes);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {fos.close();} catch (IOException e) {e.printStackTrace();}}}
}

阿里的图片剪裁工具类及依赖的jar包相关推荐

  1. 基于阿里云的短信接口的工具类及三个jar包下载地址

    https://blog.csdn.net/Mr_zzr/article/details/100168118 上面这个写的很好! 三个jar包下载地址在我的github: https://github ...

  2. Java实现pdf转图片的工具类(三种方法实现PDF转图片的案例)【亲测可用】

    提示:有些时候我们需要在项目中展示PDF,所以我们可以将PDF转为图片,然后已图片的方式展示,效果很好.Java使用各种技术将pdf转换成图片格式,并且内容不失帧.清晰可见,该工具类也是开发中常用到的 ...

  3. ImageView可直接调用的,根据URL设置图片的工具类

    ImageView 是Android编程中最常用的组件之一. 但是根据图片的URL设置图片却很麻烦.因为获取网络图片的操作必须在异步线程中进行,根据URL设置ImageView图片就可能需要进行线程间 ...

  4. linux 图片编辑 java_Java的图片处理工具类

    Java的图片处理工具类: 可实现以下常用功能:缩放图像.切割图像.图像类型转换.彩色转黑白.文字水印.图片水印等 import java.awt.AlphaComposite; import jav ...

  5. 图片处理工具类 - ImageUtils.java

    纯JAVA实现的图片处理工具类,提供图片的裁剪.压缩.获取尺寸.制作圆角等方法. 源码如下:(点击下载 -ImageUtils.java .FolderUtils.java .commons-io-2 ...

  6. java 图片合成 工具类_Java实现的微信图片处理工具类【裁剪,合并,等比例缩放等】...

    本文实例讲述了Java实现的微信图片处理工具类.分享给大家供大家参考,具体如下: 现在 外面核心,图片文章比较少,看了拷贝代码,而用不了,用相应jar包处理,很多等比例缩放,达不到 想要的给予的期望: ...

  7. 基于vue的图片剪裁工具vue-croppe

    基于vue的图片剪裁工具vue-croppe 安装 // npm安装 npm install --save vue-croppa // yarn 安装 yarn add vue-croppa 使用 引 ...

  8. Android 图片处理工具类封装2

    http://www.2cto.com/kf/201312/263638.html Android 图片处理工具类封装 2013-12-10     0个评论   来源:Wiker Yong 的专栏  ...

  9. java图片处理工具类,很实用哦

    笔者以前在项目里要求处理图片,当时在博客里看到这篇不错的帖子,但是没有看到原作的出处,于是就不客气的转载下来了...同时感谢原创写出这么好的东西. 这个图像工具类可实现以下常用功能:缩放图像.切割图像 ...

最新文章

  1. ubuntu报错解决:The following packages have unmet dependencies:
  2. 《Python核心编程》第二版第36页第二章练习 续一 -Python核心编程答案-自己做的-...
  3. js 获取 屏幕 可用高度...
  4. 怎么在vue的@click里面直接写js_【转】为 Node.js 贡献你的力量 ———— 调试代码
  5. js判断鼠标旋转度数以及顺逆方向详解
  6. 服务器安装报告linux,linux – 在ubuntu服务器上安装了2TB磁盘,dmesg将其报告为9444732965540666 MB...
  7. 【转】服务器维护工程师悲惨的一个星期
  8. (转)漫画:什么是分布式事务?
  9. 1346. 检查整数及其两倍数是否存在 golang
  10. mysql自增id获取失败
  11. 《大道至简》阅读笔记02
  12. 海量数据挖掘MMDS week5: 聚类clustering
  13. [ASP.NET AJAX]Function对象及Type类的方法介绍
  14. 锐捷设备AC旁挂核心交换机②
  15. 太经典了,不转不行淘宝上面的对话
  16. 麻将判断胡牌 java_麻将基本胡的算法——Java
  17. c语言计算10以内之和,求一个C语言程序,随机产生50道10以内的加法算术题
  18. Catagory用法
  19. gif动态图太大如何发微信?手机如何快速压缩动图?
  20. 从蓄水池问题思考异步FIFO深度设计

热门文章

  1. linux串口驱动及应用程序,基于华邦W90P710处理器的Linux内核应用及串口驱动的实现-嵌入式系统-与非网...
  2. JavaWeb-新版
  3. UICC 之 USIM 详解全系列——UICC协议层结构
  4. Nginx工作原理和优化、漏洞(转)
  5. matlab导入数据后画图_visio结合matlab画图
  6. 专业英语(计算机)司爱侠练习版,专业英语(计算机)司爱侠练习版.doc
  7. 股票入门基础知识19:使用贴现现金流(DCF)计算公司估值
  8. [转]宽带网络速度计算方法
  9. SWIProlog之动物识别
  10. 从100场腾讯面试中,抽出来经典面试题,腾讯技术职业等级丨C++后端开发丨Linux服务器开发丨面试经验丨面试总结