首页 > 编程语言 > 详细

java图片压缩

时间:2019-08-12 18:12:39      阅读:217      评论:0      收藏:0      [点我收藏+]

1、首先从Fast服务器获取图片流,然后将流转化成BufferedImage

2、从BufferedImage根据需要的宽度和高度获取压缩图片实例

3、对压缩图片实例进行重绘,将图片绘制到BufferedImage

4、使用ImageIO对BufferedImage进行流读写的操作,输出到本地磁盘系统或上传到fast服务器

public class FileUtilsFastDfsAdapter{
    @Resource
    private FastDfsUtils fastDfsUtils;

    @Value("${fastdfs.fileServer}")
    private String fastUrl;

/**
*按照指定宽度进行压缩
*
*/
public String getScaleImage(String url, Integer width, Integer height) throws IOException, MyException { byte[] fileByte = fastDfsUtils.getFileByte(url); BufferedImageVo bufferedImageVo = getBufferedImage(fileByte); BufferedImage bufferedImage = bufferedImageVo.getBufferedImage(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); String jpgId = getUrl(bufferedImageVo, bufferedImage, (int) height, (int) width); return jpgId; } private BufferedImageVo getBufferedImage(byte[] fileByte) throws IOException { BufferedImageVo bufferedImageVo = new BufferedImageVo(); // 将字节数组转为InputStream,再转为MemoryCacheImageInputStream ImageInputStream imageInputstream = new MemoryCacheImageInputStream(new ByteArrayInputStream(fileByte)); // 获取所有能识别数据流格式的ImageReader对象 Iterator<ImageReader> it = ImageIO.getImageReaders(imageInputstream); BufferedImage bufferedImage= null; int originalWidth = 0,originalHeight = 0; // 迭代器遍历尝试用ImageReader对象进行解码 while (it.hasNext()) { ImageReader imageReader = it.next(); // 设置解码器的输入流 imageReader.setInput(imageInputstream, true, true); // 图像文件格式后缀 String suffix = imageReader.getFormatName().trim().toLowerCase(); // 图像宽度 originalWidth = imageReader.getWidth(0); // 图像高度 originalHeight = imageReader.getHeight(0); System.out.printf("format %s,%dx%d\n", suffix, originalWidth, originalHeight); try { // 解码成功返回BufferedImage对象 // 0即为对第0张图像解码(gif格式会有多张图像),前面获取宽度高度的方法中的参数0也是同样的意思 bufferedImage = imageReader.read(0, imageReader.getDefaultReadParam()); } catch (Exception e) { imageReader.dispose(); // 如果解码失败尝试用下一个ImageReader解码 } bufferedImageVo.setSuffix(suffix); } bufferedImageVo.setBufferedImage(bufferedImage); return bufferedImageVo; }
/**
*根据提供的最大边进行等比例压缩
*
*/
public String getScaleImage(String url, Integer maxLength) throws IOException, MyException { byte[] fileByte = fastDfsUtils.getFileByte(url); BufferedImageVo bufferedImageVo = getBufferedImage(fileByte); BufferedImage bufferedImage = bufferedImageVo.getBufferedImage(); // 图像宽度 int originalWidth = bufferedImage.getWidth(); // 图像高度 int originalHeight = bufferedImage.getHeight(); //按压缩压缩比例获取宽度和高度 Float bl; if(originalHeight>originalWidth){ bl = maxLength/Convert.toFloat(originalHeight); }else{ bl = maxLength/Convert.toFloat(originalWidth); } float height = originalHeight * bl; float width = originalWidth * bl; String jpgId = getUrl(bufferedImageVo, bufferedImage, (int) height, (int) width); return jpgId; } /** * 对图像进行压缩并上传到fast * @param bufferedImageVo 提供图片后缀 * @param bufferedImage 原图片buffered,提供压缩模板 * @param height 压缩后的高度 * @param width 压缩后的宽度 * @return * @throws IOException * @throws MyException */ private String getUrl(BufferedImageVo bufferedImageVo, BufferedImage bufferedImage, int height, int width) throws IOException, MyException { ByteArrayOutputStream bos = new ByteArrayOutputStream();
     //获取压缩图片实例 Image scaledInstance
= bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH); BufferedImage outImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics graphics = outImage.getGraphics();
     //进行重绘 graphics.drawImage(scaledInstance,
0,0,null); graphics.dispose(); bufferedImage.getType(); ImageIO.write(outImage,bufferedImageVo.getSuffix(),bos); outImage.flush(); return fastDfsUtils.upload(bos.toByteArray(), bufferedImageVo.getSuffix()); } }

 

java图片压缩

原文:https://www.cnblogs.com/nangonghui/p/11341606.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!