壓縮思路
通過 BufferedImage.getGraphics().drawImage(Image.getScaledInstance()) 壓縮圖片。
Image.getScaledInstance(width, height, hints) 的hints爲壓縮模式,本文采用 Image.SCALE_SMOOTH 生成縮略圖片的平滑度的,優先級比速度高,生成的圖片質量比較好但速度慢
源碼
import com.hyxf.open.common.util.FileUtil;
import com.hyxf.open.common.util.StreamUtils;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
public class ImageTest {
public static void main(String[] args) {
test();
}
public static void test() {
String sPath = "D:\\tmp\\image\\pngFile.png";
String dPath = "D:\\tmp\\image\\tmp33.jpg";
try {
InputStream contentStream = new FileInputStream(sPath);
InputStream result = reduce(contentStream, dPath);
System.out.println(StreamUtils.stream2Base64String(result));
result.close();
FileUtil.delete(dPath);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 下載並壓縮圖片
*
* @param contentStream
* @param localPath
* @return
*/
public static InputStream reduce(InputStream contentStream, String localPath) {
try {
File file = new File(localPath);
inputstreamTofile(contentStream, file);
System.out.println("start reduce");
reduceImg(localPath, localPath);
System.out.println("finished");
InputStream n = new FileInputStream(localPath);
return n;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 下載文件
* @param ins
* @param file
* @throws IOException
*/
public static void inputstreamTofile(InputStream ins, File file) throws IOException {
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
}
public static void reduceImg(String imgsrc, String imgdist) {
try {
File srcfile = new File(imgsrc);
// 檢查圖片文件是否存在
if (!srcfile.exists()) {
System.out.println("文件不存在");
}
int[] results = getImgWidthHeight(srcfile);
int widthdist = results[0];
int heightdist = results[1];
// 開始讀取文件並進行壓縮
Image src = ImageIO.read(srcfile);
// 構造一個類型爲預定義圖像類型之一的 BufferedImage
BufferedImage tag = new BufferedImage( widthdist, heightdist, BufferedImage.TYPE_INT_RGB);
// 這邊是壓縮的模式設置
tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist, Image.SCALE_SMOOTH), 0, 0, null);
//創建文件輸出流
FileOutputStream out = new FileOutputStream(imgdist);
//將圖片按JPEG壓縮,保存到out中
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
//關閉文件輸出流
out.close();
} catch (Exception ef) {
ef.printStackTrace();
}
}
/**
* 獲取圖片寬度和高度
*
* @param file 圖片路徑
* @return 返回圖片的寬度
*/
public static int[] getImgWidthHeight(File file) {
InputStream is = null;
BufferedImage src = null;
int result[] = { 0, 0 };
try {
// 獲得文件輸入流
is = new FileInputStream(file);
// 從流裏將圖片寫入緩衝圖片區
src = ImageIO.read(is);
// 得到源圖片寬
result[0] =src.getWidth(null);
// 得到源圖片高
result[1] =src.getHeight(null);
is.close(); //關閉輸入流
} catch (Exception ef) {
ef.printStackTrace();
}
return result;
}
}
問題及解決方案
在服務器編譯時報 com.sun.image.codec.jpeg 包找不到。
原因:java1.8開始,包名用的是oracle,而不是sun,因此需要特殊引用。在本地編譯時,idea會把jdk1.8自動引入,但服務器maven編譯時不會,需添加maven配置如下。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<!-- 解決maven命令編譯報錯,因爲rt.jar 和jce.jar在jre的lib下面,不在jdk的lib下面,導致maven找不到(java7以後會出現這個問題),將這2個jar包拷貝到jdk的lib下面估計也好使-->
<compilerArguments>
<verbose/>
<bootclasspath>${java.home}/lib/rt.jar${path.separator}${java.home}/lib/jce.jar</bootclasspath>
</compilerArguments>
<encoding>utf-8</encoding>
</configuration>
</plugin>
參考文獻
https://www.cnblogs.com/xsl1995/p/10318499.html
https://blog.csdn.net/pmdream/article/details/84305019
https://blog.csdn.net/x_lord/article/details/74311764