java 實現(xiàn)圖片像素質(zhì)量壓縮與圖片長寬縮放
更新時間:2021年11月15日 09:28:28 作者:千丈
這篇文章主要介紹了java 實現(xiàn)圖片像素質(zhì)量壓縮與圖片長寬縮放,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
java 圖片像素質(zhì)量壓縮與圖片長寬縮放
今天找到的這個方法比以前項目用到的方法更好,這里記錄下,方便日后使用!
/**
* 縮放圖片(壓縮圖片質(zhì)量,改變圖片尺寸)
* 若原圖寬度小于新寬度,則寬度不變!
* @param newWidth 新的寬度
* @param quality 圖片質(zhì)量參數(shù) 0.7f 相當(dāng)于70%質(zhì)量
* 2015年12月11日
*/
public static void resize(File originalFile, File resizedFile,
int newWidth, float quality) throws IOException {
if (quality > 1) {
throw new IllegalArgumentException(
"Quality has to be between 0 and 1");
}
ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
Image i = ii.getImage();
Image resizedImage = null;
int iWidth = i.getWidth(null);
int iHeight = i.getHeight(null);
if(iWidth < newWidth){
newWidth = iWidth;
}
if (iWidth > iHeight) {
resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight)
/ iWidth, Image.SCALE_SMOOTH);
} else {
resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight,
newWidth, Image.SCALE_SMOOTH);
}
// This code ensures that all the pixels in the image are loaded.
Image temp = new ImageIcon(resizedImage).getImage();
// Create the buffered image.
BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),
temp.getHeight(null), BufferedImage.TYPE_INT_RGB);
// Copy image to buffered image.
Graphics g = bufferedImage.createGraphics();
// Clear background and paint the image.
g.setColor(Color.white);
g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
g.drawImage(temp, 0, 0, null);
g.dispose();
// Soften.
float softenFactor = 0.05f;
float[] softenArray = { 0, softenFactor, 0, softenFactor,
1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 };
Kernel kernel = new Kernel(3, 3, softenArray);
ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
bufferedImage = cOp.filter(bufferedImage, null);
// Write the jpeg to a file.
FileOutputStream out = new FileOutputStream(resizedFile);
// Encodes image as a JPEG data stream
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder
.getDefaultJPEGEncodeParam(bufferedImage);
param.setQuality(quality, true);
encoder.setJPEGEncodeParam(param);
encoder.encode(bufferedImage);
} // Example usage
public static void main(String[] args) throws IOException {
// File originalImage = new File("C:\\11.jpg");
// resize(originalImage, new File("c:\\11-0.jpg"),150, 0.7f);
// resize(originalImage, new File("c:\\11-1.jpg"),150, 1f);
File originalImage = new File("d:\\testImg\\1.jpg");
System.out.println("源文件大小" + originalImage.length());
// File resizedImg = new File("d:\\testImg\\11.jpg");
// resize(originalImage, resizedImg, 850, 1f);
// System.out.println("0.5轉(zhuǎn)換后文件大小" + resizedImg.length());
// File resizedImg1 = new File("d:\\testImg\\111.jpg");
File resizedImg1 = new File("/alidata/zkyj/dashixiong/tempImgFile/11.jpg");
resize(originalImage, resizedImg1, 1550, 0.7f);
System.out.println("0.7轉(zhuǎn)換后文件大小" + resizedImg1.length());
}
java 修改圖片dpi(像素/大小)
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class DPIHandleHelper {
private static int DPI = 300;
public static void main(String[] args) {
String path = "C:\\test.jpg";
File file = new File(path);
handleDpi(file, 300, 300);
}
/**
* 改變圖片DPI
*
* @param file
* @param xDensity
* @param yDensity
*/
public static void handleDpi(File file, int xDensity, int yDensity) {
try {
BufferedImage image = ImageIO.read(file);
JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(new FileOutputStream(file));
JPEGEncodeParam jpegEncodeParam = jpegEncoder.getDefaultJPEGEncodeParam(image);
jpegEncodeParam.setDensityUnit(JPEGEncodeParam.DENSITY_UNIT_DOTS_INCH);
jpegEncoder.setJPEGEncodeParam(jpegEncodeParam);
jpegEncodeParam.setQuality(0.75f, false);
jpegEncodeParam.setXDensity(xDensity);
jpegEncodeParam.setYDensity(yDensity);
jpegEncoder.encode(image, jpegEncodeParam);
image.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Spring boot/Spring 統(tǒng)一錯誤處理方案的使用
這篇文章主要介紹了詳解Spring boot/Spring 統(tǒng)一錯誤處理方案的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06
Spring Boot中的WebSocketMessageBrokerConfigurer接口使用
在SpringBoot中,我們可以使用 WebSocketMessageBrokerConfigurer接口來配置WebSocket消息代理,以實現(xiàn)實時通信,具有一定的參考價值,感興趣的可以了解一下2023-11-11
Java?實現(xiàn)訂單未支付超時自動取消功能(京東商城為例)
本文以京東網(wǎng)上商城為例,給大家介紹商品在下單后沒有支付的情況下,超時自動取消功能,超過24小時,就會自動取消訂單,下面使用 Java 定時器實現(xiàn)超時取消訂單功能,感興趣的朋友一起看看吧2022-01-01
Java二叉搜索樹基礎(chǔ)原理與實現(xiàn)方法詳解
這篇文章主要介紹了Java二叉搜索樹基礎(chǔ)原理與實現(xiàn)方法,結(jié)合圖文與實例形式詳細(xì)分析了Java二叉搜索樹的基本概念、原理、實現(xiàn)方法與操作注意事項,需要的朋友可以參考下2020-03-03
SpringCloud @RefreshScope注解源碼層面深入分析
@RefreshScope注解能幫助我們做局部的參數(shù)刷新,但侵入性較強,需要開發(fā)階段提前預(yù)知可能的刷新點,并且該注解底層是依賴于cglib進行代理的,所以不要掉入cglib的坑,出現(xiàn)刷了也不更新情況2023-04-04

