java實(shí)現(xiàn)圖片壓縮的思路與代碼
本文實(shí)例為大家分享了java實(shí)現(xiàn)圖片壓縮的相關(guān)代碼,供大家參考,具體內(nèi)容如下
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
public class ImageProcess {
/**
* 圖片
*/
private Image img;
/**
* 寬度
*/
private int width;
/**
* 高度
*/
private int height;
/**
* 文件格式
*/
private String imageFormat;
/**
* 構(gòu)造函數(shù)
* @throws Exception
*/
public ImageProcess(InputStream in,String fileName) throws Exception{
//構(gòu)造Image對(duì)象
img = ImageIO.read(in);
//得到源圖寬
width = img.getWidth(null);
//得到源圖長(zhǎng)
height = img.getHeight(null);
//文件格式
imageFormat = fileName.substring(fileName.lastIndexOf(".")+1);
}
/**
* 按照寬度還是高度進(jìn)行壓縮
* @param w int 最大寬度
* @param h int 最大高度
*/
public byte[] resizeFix(int w, int h) throws IOException {
if (width / height > w / h) {
return resizeByWidth(w);
} else {
return resizeByHeight(h);
}
}
/**
* 以寬度為基準(zhǔn),等比例放縮圖片
* @param w int 新寬度
*/
public byte[] resizeByWidth(int w) throws IOException {
int h = (int) (height * w / width);
return resize(w, h);
}
/**
* 以高度為基準(zhǔn),等比例縮放圖片
* @param h int 新高度
*/
public byte[] resizeByHeight(int h) throws IOException {
int w = (int) (width * h / height);
return resize(w, h);
}
/**
* 強(qiáng)制壓縮/放大圖片到固定的大小
* @param w int 新寬度
* @param h int 新高度
*/
public byte[] resize(int w, int h) throws IOException {
// SCALE_SMOOTH 的縮略算法 生成縮略圖片的平滑度的 優(yōu)先級(jí)比速度高 生成的圖片質(zhì)量比較好 但速度慢
BufferedImage image = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB );
image.getGraphics().drawImage(img, 0, 0, w, h, null); // 繪制縮小后的圖
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, imageFormat, baos);
return baos.toByteArray();
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,輕松實(shí)現(xiàn)圖片壓縮操作。
相關(guān)文章
Spring3?中?RabbitMQ?的使用與常見場(chǎng)景分析
本文介紹了Spring3中RabbitMQ的使用,涵蓋了RabbitMQ的基本知識(shí)、五種模式、數(shù)據(jù)隔離、消費(fèi)者確認(rèn)、死信交換機(jī)、延遲功能、消息堆積解決方法、高可用性以及消息重復(fù)消費(fèi)問題的解決方案,感興趣的朋友跟隨小編一起看看吧2025-02-02
利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能
這篇文章主要給大家介紹了關(guān)于如何利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Spring?Boot?集成JWT實(shí)現(xiàn)前后端認(rèn)證的示例代碼
小程序、H5應(yīng)用的快速發(fā)展,使得前后端分離已經(jīng)成為了趨勢(shì),本文主要介紹了Spring?Boot?集成JWT實(shí)現(xiàn)前后端認(rèn)證,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Java中關(guān)于String StringBuffer StringBuilder特性深度解析
這篇文章主要介紹了Java中關(guān)于String StringBuffer StringBuilder特性深度解析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
SpringBoot 統(tǒng)一公共返回類的實(shí)現(xiàn)
本文主要介紹了SpringBoot 統(tǒng)一公共返回類的實(shí)現(xiàn),配置后臺(tái)的統(tǒng)一公共返回類,這樣做目的是為了統(tǒng)一返回信息,文中示例代碼介紹的很詳細(xì),感興趣的可以了解一下2022-01-01
Springboot項(xiàng)目消費(fèi)Kafka數(shù)據(jù)的方法
本文詳細(xì)介紹了如何在Spring Boot項(xiàng)目中配置和實(shí)現(xiàn)Kafka消費(fèi)者和生產(chǎn)者,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2025-01-01

