java提供了图片处理支持,而且使用很简单,功能也比较强大(如支持设置压缩后图片质量),直接看代码即可。
import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGEncodeParam; import com.sun.image.codec.jpeg.JPEGImageEncoder; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; /** * Created by halfsking on 2016/3/29. */ public class ImageCompressUtil { // private static final ILog LOG = LogFactory.getLog(ImageCompressUtil.class); public static byte[] compressImage(byte[] data) { return compressImage(data,600,400,0.85f); } public static byte[] compressImage(byte[] data,int width,int height) { return compressImage(data,width,height,0.85f); } public static byte[] compressImage(byte[] data,int width,int height,float quality) { width = width>600?600:width;//限定宽度为600 height = height>400?400:height;//限定高度为400 quality = quality<0.8f?0.8f:quality; int maxLength = 71*1024;//70K以下不压缩 if(data==null || data.length<maxLength ) { return data; } try { InputStream inputStream = new ByteArrayInputStream(data); Image oriImg = ImageIO.read(inputStream); int ori_width = oriImg.getWidth(null); int ori_height = oriImg.getHeight(null); if(width>ori_width || height>ori_height) { return data; } float ori_rate = (float)ori_width / (float)ori_height; float rate = (float)width / (float)height; if (ori_rate > rate) { return compressByWidth(ori_width, ori_height, width, oriImg, quality); } else { return compressByHeight(ori_width, ori_height, height, oriImg, quality); } } catch (IOException e) { LOG.error("ImageCompressUtil compressImage IOException failed,",e); } LOG.warn("ImageCompressUtil compressImage failed,now return the origin data."); return data; } /** * 以宽度为基准,等比例放缩图片 * @param w int 新宽度 */ private static byte[] compressByWidth(int ori_width,int ori_height,int w,Image oriImg,float quality) throws IOException { int h = (int) (ori_height * w / ori_width); return compress(w, h, oriImg, quality); } /** * 以高度为基准,等比例缩放图片 * @param h int 新高度 */ private static byte[] compressByHeight(int ori_width,int ori_height,int h,Image oriImg,float quality) throws IOException { int w = (int) (ori_width * h / ori_height); return compress(w, h, oriImg, quality); } /** * 压缩/放大图片到固定的大小 * @param w int 新宽度 * @param h int 新高度 */ private static byte[] compress(int w, int h,Image oriImg,float quality) throws IOException { BufferedImage image = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB ); // SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢 //image.getGraphics().drawImage(img, 0, 0, w, h, null); // 绘制缩小后的图 image.getGraphics().drawImage(oriImg.getScaledInstance(w, h, Image.SCALE_SMOOTH), 0,0,null); ByteArrayOutputStream out = new ByteArrayOutputStream(); // 可以正常实现bmp、png、gif转jpg JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(image); /* 压缩质量 */ jep.setQuality(quality, true); encoder.encode(image, jep);// JPEG编码 return out.toByteArray(); } }
相关推荐
Java压缩图片util,可等比例宽高不失真压缩,也可直接指定压缩后的宽高,只能用炫酷来形容,感兴趣就下载看看吧
java各种方式压缩上传图片,包括按指定宽度,高度压缩。指定比例压缩等
java中根据设置的宽高等比例压缩图片文件 先保存原文件,再压缩、上传
java 处理图片大小等比例缩放;图片裁剪;支持图片压缩不失真;支持图片添加水印;支持动态图的处理;支持自定义图片大小的转换 等 压缩包中内附使用说明
java 按比例压缩图片大小
JAVA等比例图片压缩
java方法完美压缩图片,不失帧,不截图.
java 实现按比例压缩图片, 封装好了方法, 传入图片路径就可以测试使用了
支持压缩图片的大小,可以根据想要的高度进行压缩。也支持过大压缩变小。支持各种静态图片和gif动态图片。
java图片压缩,可设置宽度,高度,压缩像素比例
用JAVA获取视频文件中的帧图片并等比缩放,java 获取视频中图片 缩放比例 javacv
主要介绍了Java实现的上传并压缩图片功能,可实现图片的等比例压缩或原尺寸压缩,涉及java文件读写、转换、传输等相关操作技巧,需要的朋友可以参考下
java上传图片功能,图片太大,等比例压缩算法(原尺寸压缩)
java实现图像压缩,图片不失真的等比例压缩算法
高清缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等 功能非常强大,并且很清晰,请给好评:)
实现图片的压缩 1.按照固定的比例缩放图片 2.以宽度为基准等比例放缩图片 3.以高度为基准,等比例缩放图片 4.按照最大高度限制,生成最大的等比例缩略图
java处理的图片,缩放,比例处理,压缩等java处理的图片,缩放,比例处理,压缩等
可以将图片按指定的长宽压缩,根据自己需求制定压缩比例
// 图片比例 double pw = sw; double ph = sh; double m = (double) sw / pw; double n = (double) sh / ph; System.out.println(m); int wth = (int) (destWidth * m); int hth = (int) (destHeight * n); ...
java图片压缩文件thumbnailator-0.4.8.jar.zip Thumbnailator是一个用来对图像进行处理以及缩略图的 Java类库,通过很简单的代码即可生成图片缩略图,也可直接对一整个目录的图片生成缩略图。有了它我们就不用在费...