詳解Java中使用ImageIO類對(duì)圖片進(jìn)行壓縮的方法
最近做項(xiàng)目需要圖片壓縮處理,網(wǎng)上找的方法大都使用了 com.sun.image.codec.jpeg.* 這個(gè)包中的JPEGImageEncoder類,引入這個(gè)包后一直報(bào)錯(cuò),各種google百度,嘗試了各種方法,包括手動(dòng)引jre中的rt.jar,以及在eclipse中把受訪問限制的API提示從ERROR改為WARNING,等等,然而這些都是不好使的,因?yàn)楹髞砦野l(fā)現(xiàn)我的java-7-openjdk-amd64中的rt.jar里邊根本就沒有com.sun.image.*,貌似這個(gè)類在java7中已經(jīng)被徹底remove了,至少我這個(gè)版本是沒有了。然后搜了個(gè)使用ImageIO類來進(jìn)行處理的替代方案,代碼如下:
可以壓縮為任意大小,壓縮后高清,不變形(留白),可以改后綴名,可以修改壓縮分辨率。
可能有朋友也有這個(gè)需要,參考一下吧,有問題還請(qǐng)指證!
package cn.com.images;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.MathContext;
import java.util.ArrayList;
import javax.imageio.ImageIO;
/***
* 對(duì)圖片進(jìn)行操作
*
* @author chenzheng_java
* @since 2011/7/29
*
*/
public class ImageHelper {
private static ImageHelper imageHelper = null;
public static ImageHelper getImageHelper() {
if (imageHelper == null) {
imageHelper = new ImageHelper();
}
return imageHelper;
}
/***
* 按指定的比例縮放圖片
*
* @param sourceImagePath
* 源地址
* @param destinationPath
* 改變大小后圖片的地址
* @param scale
* 縮放比例,如1.2
*/
public static void scaleImage(String sourceImagePath,
String destinationPath, double scale,String format) {
File file = new File(sourceImagePath);
BufferedImage bufferedImage;
try {
bufferedImage = ImageIO.read(file);
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
width = parseDoubleToInt(width * scale);
height = parseDoubleToInt(height * scale);
Image image = bufferedImage.getScaledInstance(width, height,
Image.SCALE_SMOOTH);
BufferedImage outputImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics graphics = outputImage.getGraphics();
graphics.drawImage(image, 0, 0, null);
graphics.dispose();
ImageIO.write(outputImage, format, new File(destinationPath));
} catch (IOException e) {
System.out.println("scaleImage方法壓縮圖片時(shí)出錯(cuò)了");
e.printStackTrace();
}
}
/***
* 將圖片縮放到指定的高度或者寬度
* @param sourceImagePath 圖片源地址
* @param destinationPath 壓縮完圖片的地址
* @param width 縮放后的寬度
* @param height 縮放后的高度
* @param auto 是否自動(dòng)保持圖片的原高寬比例
* @param format 圖圖片格式 例如 jpg
*/
public static void scaleImageWithParams(String sourceImagePath,
String destinationPath, int width, int height, boolean auto,String format) {
try {
File file = new File(sourceImagePath);
BufferedImage bufferedImage = null;
bufferedImage = ImageIO.read(file);
if (auto) {
ArrayList<Integer> paramsArrayList = getAutoWidthAndHeight(bufferedImage,width,height);
width = paramsArrayList.get(0);
height = paramsArrayList.get(1);
System.out.println("自動(dòng)調(diào)整比例,width="+width+" height="+height);
}
Image image = bufferedImage.getScaledInstance(width, height,
Image.SCALE_DEFAULT);
BufferedImage outputImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics graphics = outputImage.getGraphics();
graphics.drawImage(image, 0, 0, null);
graphics.dispose();
ImageIO.write(outputImage, format, new File(destinationPath));
} catch (Exception e) {
System.out.println("scaleImageWithParams方法壓縮圖片時(shí)出錯(cuò)了");
e.printStackTrace();
}
}
/**
* 將double類型的數(shù)據(jù)轉(zhuǎn)換為int,四舍五入原則
*
* @param sourceDouble
* @return
*/
private static int parseDoubleToInt(double sourceDouble) {
int result = 0;
result = (int) sourceDouble;
return result;
}
/***
*
* @param bufferedImage 要縮放的圖片對(duì)象
* @param width_scale 要縮放到的寬度
* @param height_scale 要縮放到的高度
* @return 一個(gè)集合,第一個(gè)元素為寬度,第二個(gè)元素為高度
*/
private static ArrayList<Integer> getAutoWidthAndHeight(BufferedImage bufferedImage,int width_scale,int height_scale){
ArrayList<Integer> arrayList = new ArrayList<Integer>();
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
double scale_w =getDot2Decimal( width_scale,width);
System.out.println("getAutoWidthAndHeight width="+width + "scale_w="+scale_w);
double scale_h = getDot2Decimal(height_scale,height);
if (scale_w<scale_h) {
arrayList.add(parseDoubleToInt(scale_w*width));
arrayList.add(parseDoubleToInt(scale_w*height));
}
else {
arrayList.add(parseDoubleToInt(scale_h*width));
arrayList.add(parseDoubleToInt(scale_h*height));
}
return arrayList;
}
/***
* 返回兩個(gè)數(shù)a/b的小數(shù)點(diǎn)后三位的表示
* @param a
* @param b
* @return
*/
public static double getDot2Decimal(int a,int b){
BigDecimal bigDecimal_1 = new BigDecimal(a);
BigDecimal bigDecimal_2 = new BigDecimal(b);
BigDecimal bigDecimal_result = bigDecimal_1.divide(bigDecimal_2,new MathContext(4));
Double double1 = new Double(bigDecimal_result.toString());
System.out.println("相除后的double為:"+double1);
return double1;
}
}
相關(guān)文章
SpringBoot文件上傳功能的實(shí)現(xiàn)方法
這篇文章主要介紹了SpringBoot文件上傳功能的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Java番外雜談之每天掃的二維碼你了解它內(nèi)含的信息嗎
二維碼已經(jīng)成為我們?nèi)粘I钪斜夭豢缮俚慕M成部分了,登錄需要掃一掃二維碼、買東西付錢需要掃一掃二維碼、開會(huì)簽到也需要掃一掃二維碼,那么如此使用的二維碼技術(shù),背后的原理是怎樣的呢?本文將結(jié)合二維碼的發(fā)展歷程以及典型應(yīng)用場景,分析二維碼背后的技術(shù)原理2022-02-02
idea報(bào)錯(cuò)之找不到符號(hào):類的問題及解決
這篇文章主要介紹了idea報(bào)錯(cuò)之找不到符號(hào):類的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
java實(shí)現(xiàn)往hive 的map類型字段寫數(shù)據(jù)
這篇文章主要介紹了java實(shí)現(xiàn)往hive 的map類型字段寫數(shù)據(jù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Java實(shí)現(xiàn)定時(shí)器的4種方法超全總結(jié)
對(duì)于一些特殊的代碼是需要定時(shí)執(zhí)行的,下面來看看定時(shí)器該如何編寫吧,下面這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)定時(shí)器的4種方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
使用Feign實(shí)現(xiàn)微服務(wù)間文件下載
這篇文章主要為大家詳細(xì)介紹了使用Feign實(shí)現(xiàn)微服務(wù)間文件下載,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04

