欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實(shí)現(xiàn)的上傳并壓縮圖片功能【可等比例壓縮或原尺寸壓縮】

 更新時(shí)間:2018年07月20日 14:46:22   作者:赤砂之蝎我愛羅  
這篇文章主要介紹了Java實(shí)現(xiàn)的上傳并壓縮圖片功能,可實(shí)現(xiàn)圖片的等比例壓縮或原尺寸壓縮,涉及java文件讀寫、轉(zhuǎn)換、傳輸?shù)认嚓P(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Java實(shí)現(xiàn)的上傳并壓縮圖片功能。分享給大家供大家參考,具體如下:

先看效果:

原圖:1.33M

處理后:27.4kb

關(guān)鍵代碼:

package codeGenerate.util;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageZipUtil {
  public static void main(String[] args) {
    zipWidthHeightImageFile(new File("C:\\spider\\3.png"),new File("C:\\spider\\3-1.jpg"),425,638,0.7f);
    //zipImageFile(new File("C:\\spider\\2.JPG"),new File("C:\\spider\\2-2.JPG"),425,638,0.7f);
    //zipImageFile(new File("C:\\spider\\3.jpg"),new File("C:\\spider\\3-3.jpg"),425,638,0.7f);
    System.out.println("ok");
  }
  /**
   * 根據(jù)設(shè)置的寬高等比例壓縮圖片文件<br> 先保存原文件,再壓縮、上傳
   * @param oldFile 要進(jìn)行壓縮的文件
   * @param newFile 新文件
   * @param width 寬度 //設(shè)置寬度時(shí)(高度傳入0,等比例縮放)
   * @param height 高度 //設(shè)置高度時(shí)(寬度傳入0,等比例縮放)
   * @param quality 質(zhì)量
   * @return 返回壓縮后的文件的全路徑
   */
  public static String zipImageFile(File oldFile,File newFile, int width, int height,float quality) {
    if (oldFile == null) {
      return null;
    }
    try {
      /** 對服務(wù)器上的臨時(shí)文件進(jìn)行處理 */
      Image srcFile = ImageIO.read(oldFile);
      int w = srcFile.getWidth(null);
      int h = srcFile.getHeight(null);
      double bili;
      if(width>0){
        bili=width/(double)w;
        height = (int) (h*bili);
      }else{
        if(height>0){
          bili=height/(double)h;
          width = (int) (w*bili);
        }
      }
      String srcImgPath = newFile.getAbsoluteFile().toString();
      System.out.println(srcImgPath);
      String subfix = "jpg";
      subfix = srcImgPath.substring(srcImgPath.lastIndexOf(".")+1,srcImgPath.length());
      BufferedImage buffImg = null;
      if(subfix.equals("png")){
        buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
      }else{
        buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      }
      Graphics2D graphics = buffImg.createGraphics();
      graphics.setBackground(new Color(255,255,255));
      graphics.setColor(new Color(255,255,255));
      graphics.fillRect(0, 0, width, height);
      graphics.drawImage(srcFile.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
      ImageIO.write(buffImg, subfix, new File(srcImgPath));
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return newFile.getAbsolutePath();
  }
  /**
   * 按設(shè)置的寬度高度壓縮圖片文件<br> 先保存原文件,再壓縮、上傳
   * @param oldFile 要進(jìn)行壓縮的文件全路徑
   * @param newFile 新文件
   * @param width 寬度
   * @param height 高度
   * @param quality 質(zhì)量
   * @return 返回壓縮后的文件的全路徑
   */
  public static String zipWidthHeightImageFile(File oldFile,File newFile, int width, int height,float quality) {
    if (oldFile == null) {
      return null;
    }
    String newImage = null;
    try {
      /** 對服務(wù)器上的臨時(shí)文件進(jìn)行處理 */
      Image srcFile = ImageIO.read(oldFile);
      String srcImgPath = newFile.getAbsoluteFile().toString();
      System.out.println(srcImgPath);
      String subfix = "jpg";
      subfix = srcImgPath.substring(srcImgPath.lastIndexOf(".")+1,srcImgPath.length());
      BufferedImage buffImg = null;
      if(subfix.equals("png")){
        buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
      }else{
        buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      }
      Graphics2D graphics = buffImg.createGraphics();
      graphics.setBackground(new Color(255,255,255));
      graphics.setColor(new Color(255,255,255));
      graphics.fillRect(0, 0, width, height);
      graphics.drawImage(srcFile.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
      ImageIO.write(buffImg, subfix, new File(srcImgPath));
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return newImage;
  }
}

說明:

1、根據(jù)需求大家可以自行設(shè)置質(zhì)量參數(shù)quality,到底設(shè)置成多少,可以先看下效果在取值;

2、網(wǎng)上通用的方法用的是jdk自帶jar包中方法,我這里衍生了一下:用Graphics2D,能夠同時(shí)處理jpg和png格式;

3、new Color(255,255,255)是白色,等同于WHITE,但是用WHITE 的話,Linux下某些圖片會有其它色值;

4、main中的寬425和高638可以根據(jù)自己的需求自行設(shè)置,但是對于長和寬一樣的,按照400(小值的值425)*400來處理;

更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java圖片操作技巧匯總》、《java日期與時(shí)間操作技巧匯總》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》及《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》。

希望本文所述對大家java程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Java實(shí)現(xiàn)五子棋的基礎(chǔ)方法

    Java實(shí)現(xiàn)五子棋的基礎(chǔ)方法

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)五子棋的基礎(chǔ)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • Java+opencv3.2.0實(shí)現(xiàn)輪廓檢測

    Java+opencv3.2.0實(shí)現(xiàn)輪廓檢測

    這篇文章主要為大家詳細(xì)介紹了Java+opencv3.2.0實(shí)現(xiàn)輪廓檢測,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • Java枚舉類使用場景及實(shí)例解析

    Java枚舉類使用場景及實(shí)例解析

    這篇文章主要介紹了Java枚舉類使用場景及實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • springboot如何讀取模板文件

    springboot如何讀取模板文件

    這篇文章主要介紹了springboot如何讀取模版文件的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • SpringBoot手動使用EhCache的方法示例

    SpringBoot手動使用EhCache的方法示例

    本篇文章主要介紹了SpringBoot手動使用EhCache的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-02-02
  • 如何使用IntelliJ IDEA的HTTP Client進(jìn)行接口驗(yàn)證

    如何使用IntelliJ IDEA的HTTP Client進(jìn)行接口驗(yàn)證

    這篇文章主要介紹了如何使用IntelliJ IDEA的HTTP Client進(jìn)行接口驗(yàn)證,本文給大家分享最新完美解決方案,感興趣的朋友跟隨小編一起看看吧
    2024-06-06
  • Java實(shí)現(xiàn)入?yún)?shù)據(jù)批量數(shù)據(jù)校驗(yàn)詳解

    Java實(shí)現(xiàn)入?yún)?shù)據(jù)批量數(shù)據(jù)校驗(yàn)詳解

    在業(yè)務(wù)處理中一般入?yún)⑹菃螚l數(shù)據(jù),這樣數(shù)據(jù)校驗(yàn)比較容易,但是這種方法對于集合數(shù)據(jù)的校驗(yàn)不適用,下面我們就來看看如何對入?yún)?shù)據(jù)進(jìn)行批量數(shù)據(jù)校驗(yàn)吧
    2024-02-02
  • Spring web集成rabbitmq代碼實(shí)例

    Spring web集成rabbitmq代碼實(shí)例

    這篇文章主要介紹了Spring web集成rabbitmq代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • MybatisPlus分頁失效不起作用的解決

    MybatisPlus分頁失效不起作用的解決

    在使用MybatisPlus的selectPage時(shí)發(fā)現(xiàn)分頁不起作用,每次返回的都是全部的數(shù)據(jù),本文就來介紹一下MybatisPlus分頁失效不起作用的解決,感興趣的可以了解一下
    2024-03-03
  • 詳解SpringCloud Finchley Gateway 統(tǒng)一異常處理

    詳解SpringCloud Finchley Gateway 統(tǒng)一異常處理

    這篇文章主要介紹了詳解SpringCloud Finchley Gateway 統(tǒng)一異常處理,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2018-10-10

最新評論