詳解Java中應用ImageIO類對圖片停止緊縮的辦法。本站提示廣大學習愛好者:(詳解Java中應用ImageIO類對圖片停止緊縮的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是詳解Java中應用ImageIO類對圖片停止緊縮的辦法正文
比來做項目須要圖片緊縮處置,網上找的辦法年夜都應用了 com.sun.image.codec.jpeg.* 這個包中的JPEGImageEncoder類,引入這個包後一向報錯,各類谷歌百度,測驗考試了各類辦法,包含手動引jre中的rt.jar,和在eclipse中把受拜訪限制的API提醒從ERROR改成WARNING,等等,但是這些都是欠好使的,由於後來我發明我的java-7-openjdk-amd64中的rt.jar裡邊基本就沒有com.sun.image.*,貌似這個類在java7中曾經被完全remove了,至多我這個版本是沒有了。然後搜了個應用ImageIO類來停止處置的替換計劃,代碼以下:
可以緊縮為隨意率性年夜小,緊縮後高清,不變形(留白),可以改後綴名,可以修正緊縮分辯率。
能夠有同伙也有這個須要,參考一下吧,有成績還請指證!
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;
/***
* 對圖片停止操作
*
* @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辦法緊縮圖片時失足了");
e.printStackTrace();
}
}
/***
* 將圖片縮放到指定的高度或許寬度
* @param sourceImagePath 圖片源地址
* @param destinationPath 緊縮完圖片的地址
* @param width 縮放後的寬度
* @param height 縮放後的高度
* @param auto 能否主動堅持圖片的原高寬比例
* @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("主動調劑比例,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辦法緊縮圖片時失足了");
e.printStackTrace();
}
}
/**
* 將double類型的數據轉換為int,四捨五入准繩
*
* @param sourceDouble
* @return
*/
private static int parseDoubleToInt(double sourceDouble) {
int result = 0;
result = (int) sourceDouble;
return result;
}
/***
*
* @param bufferedImage 要縮放的圖片對象
* @param width_scale 要縮放到的寬度
* @param height_scale 要縮放到的高度
* @return 一個聚集,第一個元素為寬度,第二個元素為高度
*/
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;
}
/***
* 前往兩個數a/b的小數點後三位的表現
* @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;
}
}