• java图片压缩


    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 ", 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()); } }
  • 相关阅读:
    用mvc实现增删查改
    hibernate Annotation版本的helloworld
    hibernate 级联操作
    Hibrenate一对一外键关联
    hibernate主键生成
    Action属性接收参数
    WildCard的使用
    Struts 路径分析以及是否一定要执行excute()方法
    学习Struts2经验总结
    基于Struts分层web框架,研究传值问题
  • 原文地址:https://www.cnblogs.com/nangonghui/p/11341606.html
Copyright © 2020-2023  润新知