最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java對圖片進(jìn)行壓縮和resize縮放的方法

 更新時間:2017年07月31日 10:49:16   作者:xixicat  
本篇文章主要介紹了java對圖片進(jìn)行壓縮和resize調(diào)整的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下


這里展示一下如何對圖片進(jìn)行壓縮和resize。分享給大家,具體如下:

壓縮

public static boolean compress(String src,String to, float quality) {
    boolean rs = true;

    // Build param
    JPEGEncodeParam param = null;

    // Build encoder
    File destination = new File(to);
    FileOutputStream os = null;
    try {
      BufferedImage image = ImageIO.read(new File(src));
      param = JPEGCodec.getDefaultJPEGEncodeParam(image);
      param.setQuality(quality, false);

      os = FileUtils.openOutputStream(destination);
      JPEGImageEncoder encoder;
      if (param != null) {
        encoder = JPEGCodec.createJPEGEncoder(os, param);
      } else {
        return false;
      }
      encoder.encode(image);
    } catch(Exception e){
      e.printStackTrace();
      rs = false;
    }finally {
      IOUtils.closeQuietly(os);
    }
    return rs;
  }

resize

public static boolean resize(String src,String to,int newWidth,int newHeight) {
    try {
      File srcFile = new File(src);
      File toFile = new File(to);
      BufferedImage img = ImageIO.read(srcFile);
      int w = img.getWidth();
      int h = img.getHeight();
      BufferedImage dimg = new BufferedImage(newWidth, newHeight, img.getType());
      Graphics2D g = dimg.createGraphics();
      g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
      g.drawImage(img, 0, 0, newWidth, newHeight, 0, 0, w, h, null);
      g.dispose();
      ImageIO.write(dimg, "jpg", toFile);
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
    return true;
  }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

宜昌市| 云龙县| 邻水| 罗山县| 阜新| 鸡西市| 和平县| 康马县| 鄂伦春自治旗| 堆龙德庆县| 临漳县| 嘉善县| 临清市| 温泉县| 秦皇岛市| 遂昌县| 开封县| 汤阴县| 吴川市| 理塘县| 年辖:市辖区| 枞阳县| 金寨县| 龙岩市| 桐庐县| 晋州市| 平安县| 灵山县| 武宣县| 丹阳市| 承德县| 开封市| 密山市| 云浮市| 江西省| 临潭县| 阿坝县| 黎川县| 石林| 彭山县| 宁德市|