Java實(shí)現(xià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è)置寬度時(高度傳入0,等比例縮放)
* @param height 高度 //設(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ù)器上的臨時文件進(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ù)器上的臨時文件進(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,能夠同時處理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日期與時間操作技巧匯總》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》及《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》。
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Java中JDBC實(shí)現(xiàn)動態(tài)查詢的實(shí)例詳解
從多個查詢條件中隨機(jī)選擇若干個組合成一個DQL語句進(jìn)行查詢,這一過程叫做動態(tài)查詢。下面通過實(shí)例代碼給大家講解JDBC實(shí)現(xiàn)動態(tài)查詢的方法,需要的朋友參考下吧2017-07-07
SpringBoot中讀取application.properties配置文件的方法
這篇文章主要介紹了SpringBoot中讀取application.properties配置文件的三種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02
java遞歸與非遞歸實(shí)現(xiàn)掃描文件夾下所有文件
這篇文章主要為大家詳細(xì)介紹了java遞歸與非遞歸實(shí)現(xiàn)掃描文件夾下所有文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02
Java上傳文件到服務(wù)器指定文件夾實(shí)現(xiàn)過程圖解
這篇文章主要介紹了Java上傳文件到服務(wù)器指定文件夾實(shí)現(xiàn)過程圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08
springboot集成springsecurity 使用OAUTH2做權(quán)限管理的教程
這篇文章主要介紹了springboot集成springsecurity 使用OAUTH2做權(quán)限管理的教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
SpringBoot2整合Redis多數(shù)據(jù)源步驟詳解
這篇文章主要介紹了SpringBoot2整合Redis多數(shù)據(jù)源步驟詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03
Java從內(nèi)存角度帶你理解數(shù)組名實(shí)質(zhì)是個地址的論述
這篇文章主要介紹了Java如何從內(nèi)存解析的角度理解“數(shù)組名實(shí)質(zhì)是一個地址”,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-09-09

