本文实例讲述了java实现创建缩略图、伸缩图片比例生成的方法。分享给大家供大家参考。具体实现方法如下:
该实例支持将Image的宽度、高度缩放到指定width、height,并保存在指定目录 通过目标对象的大小和标准(指定)大小计算出图片缩小的比例,可以设置图片缩放质量,并且可以根据指定的宽高缩放图片。
具体代码如下所示:
复制代码 代码如下:
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;
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-";
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;
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);
}
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;
}
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;
}
encodeParam.setQuality(quality, true);
encoder.encode(image, encodeParam);
image.flush();
fos.flush();
fos.close();
return savePath;
}
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)) };
}
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 };
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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")));
}
}
希望本文所述对大家的Java程序设计有所帮助。



