java實(shí)現(xiàn)創(chuàng)建縮略圖、伸縮圖片比例生成的方法
本文實(shí)例講述了java實(shí)現(xiàn)創(chuàng)建縮略圖、伸縮圖片比例生成的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
該實(shí)例支持將Image的寬度、高度縮放到指定width、height,并保存在指定目錄 通過(guò)目標(biāo)對(duì)象的大小和標(biāo)準(zhǔn)(指定)大小計(jì)算出圖片縮小的比例,可以設(shè)置圖片縮放質(zhì)量,并且可以根據(jù)指定的寬高縮放圖片。
具體代碼如下所示:
package com.hoo.util;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* <b>function:</b> 縮放圖片工具類,創(chuàng)建縮略圖、伸縮圖片比例
* @author hoojo
* @createDate 2012-2-3 上午10:08:47
* @file ScaleImageUtils.java
* @package com.hoo.util
* @version 1.0
*/
public abstract class ScaleImageUtils {
private static final float DEFAULT_SCALE_QUALITY = 1f;
private static final String DEFAULT_IMAGE_FORMAT = ".jpg"; // 圖像文件的格式
private static final String DEFAULT_FILE_PATH = "C:/temp-";
/**
* <b>function:</b> 設(shè)置圖片壓縮質(zhì)量枚舉類;
* Some guidelines: 0.75 high quality、0.5 medium quality、0.25 low quality
* @author hoojo
* @createDate 2012-2-7 上午11:31:45
* @file ScaleImageUtils.java
* @package com.hoo.util
* @project JQueryMobile
* @version 1.0
*/
public enum ImageQuality {
max(1.0f), high(0.75f), medium(0.5f), low(0.25f);
private Float quality;
public Float getQuality() {
return this.quality;
}
ImageQuality(Float quality) {
this.quality = quality;
}
}
private static Image image;
/**
* <b>function:</b> 通過(guò)目標(biāo)對(duì)象的大小和標(biāo)準(zhǔn)(指定)大小計(jì)算出圖片縮小的比例
* @author hoojo
* @createDate 2012-2-6 下午04:41:48
* @param targetWidth 目標(biāo)的寬度
* @param targetHeight 目標(biāo)的高度
* @param standardWidth 標(biāo)準(zhǔn)(指定)寬度
* @param standardHeight 標(biāo)準(zhǔn)(指定)高度
* @return 最小的合適比例
*/
public static double getScaling(double targetWidth, double targetHeight, double standardWidth, double standardHeight) {
double widthScaling = 0d;
double heightScaling = 0d;
if (targetWidth > standardWidth) {
widthScaling = standardWidth / (targetWidth * 1.00d);
} else {
widthScaling = 1d;
}
if (targetHeight > standardHeight) {
heightScaling = standardHeight / (targetHeight * 1.00d);
} else {
heightScaling = 1d;
}
return Math.min(widthScaling, heightScaling);
}
/**
* <b>function:</b> 將Image的寬度、高度縮放到指定width、height,并保存在savePath目錄
* @author hoojo
* @createDate 2012-2-6 下午04:54:35
* @param width 縮放的寬度
* @param height 縮放的高度
* @param savePath 保存目錄
* @param targetImage 即將縮放的目標(biāo)圖片
* @return 圖片保存路徑、名稱
* @throws ImageFormatException
* @throws IOException
*/
public static String resize(int width, int height, String savePath, Image targetImage) throws ImageFormatException, IOException {
width = Math.max(width, 1);
height = Math.max(height, 1);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
image.getGraphics().drawImage(targetImage, 0, 0, width, height, null);
if (savePath == null || "".equals(savePath)) {
savePath = DEFAULT_FILE_PATH + System.currentTimeMillis() + DEFAULT_IMAGE_FORMAT;
}
FileOutputStream fos = new FileOutputStream(new File(savePath));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
encoder.encode(image);
image.flush();
fos.flush();
fos.close();
return savePath;
}
/**
* <b>function:</b> 可以設(shè)置圖片縮放質(zhì)量,并且可以根據(jù)指定的寬高縮放圖片
* @author hoojo
* @createDate 2012-2-7 上午11:01:27
* @param width 縮放的寬度
* @param height 縮放的高度
* @param quality 圖片壓縮質(zhì)量,最大值是1; 使用枚舉值:{@link ImageQuality}
* Some guidelines: 0.75 high quality、0.5 medium quality、0.25 low quality
* @param savePath 保存目錄
* @param targetImage 即將縮放的目標(biāo)圖片
* @return 圖片保存路徑、名稱
* @throws ImageFormatException
* @throws IOException
*/
public static String resize(int width, int height, Float quality, String savePath, Image targetImage) throws ImageFormatException, IOException {
width = Math.max(width, 1);
height = Math.max(height, 1);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
image.getGraphics().drawImage(targetImage, 0, 0, width, height, null);
if (savePath == null || "".equals(savePath)) {
savePath = DEFAULT_FILE_PATH + System.currentTimeMillis() + DEFAULT_IMAGE_FORMAT;
}
FileOutputStream fos = new FileOutputStream(new File(savePath));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
JPEGEncodeParam encodeParam = JPEGCodec.getDefaultJPEGEncodeParam(image);
if (quality == null || quality <= 0) {
quality = DEFAULT_SCALE_QUALITY;
}
/** 設(shè)置圖片壓縮質(zhì)量 */
encodeParam.setQuality(quality, true);
encoder.encode(image, encodeParam);
image.flush();
fos.flush();
fos.close();
return savePath;
}
/**
* <b>function:</b> 通過(guò)指定大小和圖片的大小,計(jì)算出圖片縮小的合適大小
* @author hoojo
* @createDate 2012-2-6 下午05:53:10
* @param width 指定的寬度
* @param height 指定的高度
* @param image 圖片文件
* @return 返回寬度、高度的int數(shù)組
*/
public static int[] getSize(int width, int height, Image image) {
int targetWidth = image.getWidth(null);
int targetHeight = image.getHeight(null);
double scaling = getScaling(targetWidth, targetHeight, width, height);
long standardWidth = Math.round(targetWidth * scaling);
long standardHeight = Math.round(targetHeight * scaling);
return new int[] { Integer.parseInt(Long.toString(standardWidth)), Integer.parseInt(String.valueOf(standardHeight)) };
}
/**
* <b>function:</b> 通過(guò)指定的比例和圖片對(duì)象,返回一個(gè)放大或縮小的寬度、高度
* @author hoojo
* @createDate 2012-2-7 上午10:27:59
* @param scale 縮放比例
* @param image 圖片對(duì)象
* @return 返回寬度、高度
*/
public static int[] getSize(float scale, Image image) {
int targetWidth = image.getWidth(null);
int targetHeight = image.getHeight(null);
long standardWidth = Math.round(targetWidth * scale);
long standardHeight = Math.round(targetHeight * scale);
return new int[] { Integer.parseInt(Long.toString(standardWidth)), Integer.parseInt(String.valueOf(standardHeight)) };
}
public static int[] getSize(int width, Image image) {
int targetWidth = image.getWidth(null);
int targetHeight = image.getHeight(null);
long height = Math.round((targetHeight * width) / (targetWidth * 1.00f));
return new int[] { width, Integer.parseInt(String.valueOf(height)) };
}
public static int[] getSizeByHeight(int height, Image image) {
int targetWidth = image.getWidth(null);
int targetHeight = image.getHeight(null);
long width = Math.round((targetWidth * height) / (targetHeight * 1.00f));
return new int[] { Integer.parseInt(String.valueOf(width)), height };
}
/**
*
* <b>function:</b> 將指定的targetFile圖片文件的寬度、高度大于指定width、height的圖片縮小,并保存在savePath目錄
* @author hoojo
* @createDate 2012-2-6 下午04:57:02
* @param width 縮小的寬度
* @param height 縮小的高度
* @param savePath 保存目錄
* @param targetImage 改變的目標(biāo)圖片
* @return 圖片保存路徑、名稱
* @throws ImageFormatException
* @throws IOException
*/
public static String resize(int width, int height, String savePath, File targetFile) throws ImageFormatException, IOException {
image = ImageIO.read(targetFile);
int[] size = getSize(width, height, image);
return resize(size[0], size[1], savePath, image);
}
/**
*
* <b>function:</b> 將指定的targetURL網(wǎng)絡(luò)圖片文件的寬度、高度大于指定width、height的圖片縮小,并保存在savePath目錄
* @author hoojo
* @createDate 2012-2-6 下午04:57:07
* @param width 縮小的寬度
* @param height 縮小的高度
* @param savePath 保存目錄
* @param targetImage 改變的目標(biāo)圖片
* @return 圖片保存路徑、名稱
* @throws ImageFormatException
* @throws IOException
*/
public static String resize(int width, int height, String savePath, URL targetURL) throws ImageFormatException, IOException {
image = ImageIO.read(targetURL);
int[] size = getSize(width, height, image);
return resize(size[0], size[1], savePath, image);
}
/**
* <b>function:</b> 將一個(gè)本地的圖片文件按照指定的比例進(jìn)行縮放
* @author hoojo
* @createDate 2012-2-7 上午10:29:18
* @param scale 縮放比例
* @param savePath 保存文件路徑、名稱
* @param targetFile 本地圖片文件
* @return 新的文件名稱
* @throws ImageFormatException
* @throws IOException
*/
public static String resize(float scale, String savePath, File targetFile) throws ImageFormatException, IOException {
image = ImageIO.read(targetFile);
int[] size = getSize(scale, image);
return resize(size[0], size[1], savePath, image);
}
/**
* <b>function:</b> 將一個(gè)網(wǎng)絡(luò)圖片文件按照指定的比例進(jìn)行縮放
* @author hoojo
* @createDate 2012-2-7 上午10:30:56
* @param scale 縮放比例
* @param savePath 保存文件路徑、名稱
* @param targetFile 本地圖片文件
* @return 新的文件名稱
* @throws ImageFormatException
* @throws IOException
*/
public static String resize(float scale, String savePath, URL targetURL) throws ImageFormatException, IOException {
image = ImageIO.read(targetURL);
int[] size = getSize(scale, image);
return resize(size[0], size[1], savePath, image);
}
/**
* <b>function:</b> 按照固定寬度進(jìn)行等比縮放本地圖片
* @author hoojo
* @createDate 2012-2-7 上午10:49:56
* @param width 固定寬度
* @param savePath 保存路徑、名稱
* @param targetFile 本地目標(biāo)文件
* @return 返回保存路徑
* @throws ImageFormatException
* @throws IOException
*/
public static String resize(int width, String savePath, File targetFile) throws ImageFormatException, IOException {
image = ImageIO.read(targetFile);
int[] size = getSize(width, image);
return resize(size[0], size[1], savePath, image);
}
/**
* <b>function:</b> 按照固定寬度進(jìn)行等比縮放網(wǎng)絡(luò)圖片
* @author hoojo
* @createDate 2012-2-7 上午10:50:52
* @param width 固定寬度
* @param savePath 保存路徑、名稱
* @param targetFile 本地目標(biāo)文件
* @return 返回保存路徑
* @throws ImageFormatException
* @throws IOException
*/
public static String resize(int width, String savePath, URL targetURL) throws ImageFormatException, IOException {
image = ImageIO.read(targetURL);
int[] size = getSize(width, image);
return resize(size[0], size[1], savePath, image);
}
/**
*
* <b>function:</b> 按照固定高度進(jìn)行等比縮放本地圖片
* @author hoojo
* @createDate 2012-2-7 上午10:51:17
* @param height 固定高度
* @param savePath 保存路徑、名稱
* @param targetFile 本地目標(biāo)文件
* @return 返回保存路徑
* @throws ImageFormatException
* @throws IOException
*/
public static String resizeByHeight(int height, String savePath, File targetFile) throws ImageFormatException, IOException {
image = ImageIO.read(targetFile);
int[] size = getSizeByHeight(height, image);
return resize(size[0], size[1], savePath, image);
}
/**
* <b>function:</b> 按照固定高度進(jìn)行等比縮放網(wǎng)絡(luò)圖片
* @author hoojo
* @createDate 2012-2-7 上午10:52:23
* @param height 固定高度
* @param savePath 保存路徑、名稱
* @param targetFile 本地目標(biāo)文件
* @return 返回保存路徑
* @throws ImageFormatException
* @throws IOException
*/
public static String resizeByHeight(int height, String savePath, URL targetURL) throws ImageFormatException, IOException {
image = ImageIO.read(targetURL);
int[] size = getSizeByHeight(height, image);
return resize(size[0], size[1], savePath, image);
}
/**
* <b>function:</b>
* @author hoojo
* @createDate 2012-2-3 上午10:08:47
* @param args
* @throws IOException
* @throws MalformedURLException
* @throws ImageFormatException
*/
public static void main(String[] args) throws ImageFormatException, MalformedURLException, IOException {
System.out.println(ScaleImageUtils.resize(140, 140, null, new URL("http://www.open-open.com/lib/images/logo.jpg")));
ScaleImageUtils.resize(100, 100, ImageQuality.high.getQuality(), null, ImageIO.read(new URL("http://www.open-open.com/lib/images/logo.jpg")));
}
}
希望本文所述對(duì)大家的Java程序設(shè)計(jì)有所幫助。
- Java 截取視頻資料中的某一幀作為縮略圖
- Java 讀取網(wǎng)絡(luò)圖片存儲(chǔ)到本地并生成縮略圖
- 詳解Java實(shí)現(xiàn)批量壓縮圖片裁剪壓縮多種尺寸縮略圖一鍵批量上傳圖片
- Java實(shí)現(xiàn)的不同圖片居中剪裁生成同一尺寸縮略圖功能示例
- java生成縮略圖的方法示例
- Java圖片裁剪和生成縮略圖的實(shí)例方法
- java根據(jù)url抓取并生成縮略圖的示例
- 用java實(shí)現(xiàn)的獲取優(yōu)酷等視頻縮略圖的實(shí)現(xiàn)代碼
- Java縮略圖生成庫(kù)之Thumbnailator應(yīng)用說(shuō)明
- Java實(shí)現(xiàn)自動(dòng)生成縮略圖片
相關(guān)文章
java 遍歷Map及Map轉(zhuǎn)化為二維數(shù)組的實(shí)例
這篇文章主要介紹了java 遍歷Map及Map轉(zhuǎn)化為二維數(shù)組的實(shí)例的相關(guān)資料,希望通過(guò)本文能幫助到大家,實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-08-08
springboot項(xiàng)目編譯提示無(wú)效的源發(fā)行版17解決辦法
這篇文章主要給大家介紹了關(guān)于springboot項(xiàng)目編譯提示無(wú)效的源發(fā)行版17解決辦法,這個(gè)錯(cuò)誤意味著你的Spring Boot項(xiàng)目正在使用Java 17這個(gè)版本,但是你的項(xiàng)目中未配置正確的Java版本,需要的朋友可以參考下2023-06-06
SpringBoot利用jackson格式化時(shí)間的三種方法
日常開(kāi)發(fā)過(guò)程中經(jīng)常會(huì)使用json進(jìn)行數(shù)據(jù)的傳輸,這就涉及到了對(duì)象和json的相互轉(zhuǎn)化,常用的解決方案有:Jackson(推薦)、谷歌的Gson、阿里的Fastjson,這篇文章主要給大家介紹了關(guān)于SpringBoot如何利用jackson格式化時(shí)間的相關(guān)資料,需要的朋友可以參考下2021-06-06
Java如何使用itext向PDF插入數(shù)據(jù)和圖片
最近項(xiàng)目中使用到Java實(shí)現(xiàn)寫(xiě)入PDF文件,這篇文章主要給大家介紹了關(guān)于Java如何使用itext向PDF插入數(shù)據(jù)和圖片的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
深入理解java異常處理機(jī)制的原理和開(kāi)發(fā)應(yīng)用
Java異常處理機(jī)制在日常開(kāi)發(fā)中應(yīng)用頻繁,本篇文章主要在基礎(chǔ)的使用方法上,更進(jìn)一步的,如何更加合理的使用異常機(jī)制,希望可以對(duì)各位朋友能有所幫助。2017-04-04
使用springmvc參數(shù)接收boolean類型參數(shù)的問(wèn)題
這篇文章主要介紹了使用springmvc參數(shù)接收boolean類型參數(shù)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
springboot發(fā)送郵件功能的實(shí)現(xiàn)代碼
發(fā)郵件是一個(gè)很常見(jiàn)的功能,在java中實(shí)現(xiàn)需要依靠JavaMailSender這個(gè)接口,今天通過(guò)本文給大家分享springboot發(fā)送郵件功能的實(shí)現(xiàn)代碼,感興趣的朋友跟隨小編一起看看吧2021-07-07
淺析javax.servlet.Servlet,ServletContext接口
本篇文章是對(duì)javax.servlet.Servlet,ServletContext接口進(jìn)行了纖細(xì)的分析介紹,需要的朋友參考下2013-07-07
關(guān)于Spring中的@Configuration中的proxyBeanMethods屬性
這篇文章主要介紹了關(guān)于Spring中的@Configuration中的proxyBeanMethods屬性,需要的朋友可以參考下2023-07-07

