关键字: java, image, imageio, 缩放, 编码转换
通过对图片重绘,达到图片缩放、压缩编码转换功能。
给出一个简单的测试类:
- import java.awt.Image;
- import java.awt.image.BufferedImage;
- import java.awt.image.RenderedImage;
- import java.io.File;
- import java.io.InputStream;
- import java.io.OutputStream;
- import javax.imageio.ImageIO;
- public abstract class ImageUtils {
- /**
- * 缩放图片
- *
- * @param width
- * 输出宽度
- * @param height
- * 输出高度
- * @param input
- * 输入流
- * @param output
- * 输出流
- * @param format
- * 输出格式
- * @return
- * @throws Exception
- */
- public static boolean convert(int width, int height, InputStream input,
- OutputStream output, String format) throws Exception {
- // 输入
- BufferedImage inputImage = ImageIO.read(input);
- // 转换
- RenderedImage im = (RenderedImage) convert(height, height, inputImage);
- // 输出
- return ImageIO.write(im, format, output);
- }
- /**
- * 转换压缩算法
- *
- * @param input
- * 输入文件
- * @param output
- * 输出文件
- * @return
- * @throws Exception
- */
- public static boolean convert(File input, File output) throws Exception {
- // 输入
- BufferedImage inputImage = ImageIO.read(input);
- // 转换
- int width = inputImage.getWidth();
- int height = inputImage.getHeight();
- RenderedImage im = (RenderedImage) convert(width, height, inputImage);
- String outputFilename = output.getName();
- String format = outputFilename.substring(outputFilename
- .lastIndexOf('.') + 1);
- // 输出
- return ImageIO.write(im, format, output);
- }
- /**
- * 缩放图片
- *
- * @param width
- * 输出宽度
- * @param height
- * 输出高度
- * @param input
- * 输入文件
- * @param output
- * 输出文件
- * @return
- * @throws Exception
- */
- public static boolean convert(int width, int height, File input, File output)
- throws Exception {
- // 输入
- BufferedImage inputImage = ImageIO.read(input);
- // 转换
- RenderedImage im = (RenderedImage) convert(width, height, inputImage);
- String outputFilename = output.getName();
- String format = outputFilename.substring(outputFilename
- .lastIndexOf('.') + 1);
- // 输出
- return ImageIO.write(im, format, output);
- }
- /**
- * 缩放图片
- *
- * @param width
- * 输出宽度
- * @param height
- * 输出高度
- * @param input
- * 输入路径
- * @param output
- * 输出路径
- * @return
- * @throws Exception
- */
- public static boolean convert(int width, int height, String inputPath,
- String outputPath) throws Exception {
- return convert(width, height, new File(inputPath), new File(outputPath));
- }
- /**
- * 转换
- *
- * @param width
- * 输出宽度
- * @param height
- * 输出高度
- * @param input
- * BufferedImage
- * @return BufferedImage
- * @throws Exception
- */
- private static BufferedImage convert(int width, int height,
- BufferedImage input) throws Exception {
- // 初始化输出图片
- BufferedImage output = new BufferedImage(width, height,
- BufferedImage.TYPE_INT_RGB);
- // 重新绘图
- Image image = input.getScaledInstance(output.getWidth(), output
- .getHeight(), output.getType());
- output.createGraphics().drawImage(image, null, null);
- return output;
- }
- /**
- * 等比缩放图片
- *
- * @param width
- * 输出宽度
- * @param height
- * 输出高度
- * @param input
- * 输入流
- * @param output
- * 输出流
- * @return
- * @throws Exception
- */
- public static boolean equimultipleConvert(int width, int height,
- String input, String output) throws Exception {
- return equimultipleConvert(width, height, new File(input), new File(
- output));
- }
- /**
- * 等比缩放图片
- *
- * @param width
- * 输出宽度
- * @param height
- * 输出高度
- * @param input
- * 输入流
- * @param output
- * 输出流
- * @return
- *
- * @throws Exception
- */
- public static boolean equimultipleConvert(int width, int height,
- File input, File output) throws Exception {
- // 输入
- BufferedImage image = ImageIO.read(input);
- // 重新核算尺寸
- if (image.getWidth() > 0 && image.getHeight() > 0) {
- if ((image.getWidth() / image.getHeight()) >= (width / height)) {
- if (image.getWidth() > width) {
- height = (image.getHeight() * width) / image.getWidth();
- } else {
- width = image.getWidth();
- height = image.getHeight();
- }
- } else {
- if (image.getHeight() > height) {
- width = (image.getWidth() * height) / image.getHeight();
- } else {
- width = image.getWidth();
- height = image.getHeight();
- }
- }
- }
- // 转换 输出
- return convert(width, height, input, output);
- }
- }
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
/**
*
* @author 梁栋
* @version 1.0
* @since 1.0
*/
public abstract class ImageUtils {
/**
* 缩放图片
*
* @param width
* 输出宽度
* @param height
* 输出高度
* @param input
* 输入流
* @param output
* 输出流
* @param format
* 输出格式
* @return
* @throws Exception
*/
public static boolean convert(int width, int height, InputStream input,
OutputStream output, String format) throws Exception {
// 输入
BufferedImage inputImage = ImageIO.read(input);
// 转换
RenderedImage im = (RenderedImage) convert(height, height, inputImage);
// 输出
return ImageIO.write(im, format, output);
}
/**
* 转换压缩算法
*
* @param input
* 输入文件
* @param output
* 输出文件
* @return
* @throws Exception
*/
public static boolean convert(File input, File output) throws Exception {
// 输入
BufferedImage inputImage = ImageIO.read(input);
// 转换
int width = inputImage.getWidth();
int height = inputImage.getHeight();
RenderedImage im = (RenderedImage) convert(width, height, inputImage);
String outputFilename = output.getName();
String format = outputFilename.substring(outputFilename
.lastIndexOf('.') + 1);
// 输出
return ImageIO.write(im, format, output);
}
/**
* 缩放图片
*
* @param width
* 输出宽度
* @param height
* 输出高度
* @param input
* 输入文件
* @param output
* 输出文件
* @return
* @throws Exception
*/
public static boolean convert(int width, int height, File input, File output)
throws Exception {
// 输入
BufferedImage inputImage = ImageIO.read(input);
// 转换
RenderedImage im = (RenderedImage) convert(width, height, inputImage);
String outputFilename = output.getName();
String format = outputFilename.substring(outputFilename
.lastIndexOf('.') + 1);
// 输出
return ImageIO.write(im, format, output);
}
/**
* 缩放图片
*
* @param width
* 输出宽度
* @param height
* 输出高度
* @param input
* 输入路径
* @param output
* 输出路径
* @return
* @throws Exception
*/
public static boolean convert(int width, int height, String inputPath,
String outputPath) throws Exception {
return convert(width, height, new File(inputPath), new File(outputPath));
}
/**
* 转换
*
* @param width
* 输出宽度
* @param height
* 输出高度
* @param input
* BufferedImage
* @return BufferedImage
* @throws Exception
*/
private static BufferedImage convert(int width, int height,
BufferedImage input) throws Exception {
// 初始化输出图片
BufferedImage output = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 重新绘图
Image image = input.getScaledInstance(output.getWidth(), output
.getHeight(), output.getType());
output.createGraphics().drawImage(image, null, null);
return output;
}
/**
* 等比缩放图片
*
* @param width
* 输出宽度
* @param height
* 输出高度
* @param input
* 输入流
* @param output
* 输出流
* @return
* @throws Exception
*/
public static boolean equimultipleConvert(int width, int height,
String input, String output) throws Exception {
return equimultipleConvert(width, height, new File(input), new File(
output));
}
/**
* 等比缩放图片
*
* @param width
* 输出宽度
* @param height
* 输出高度
* @param input
* 输入流
* @param output
* 输出流
* @return
*
* @throws Exception
*/
public static boolean equimultipleConvert(int width, int height,
File input, File output) throws Exception {
// 输入
BufferedImage image = ImageIO.read(input);
// 重新核算尺寸
if (image.getWidth() > 0 && image.getHeight() > 0) {
if ((image.getWidth() / image.getHeight()) >= (width / height)) {
if (image.getWidth() > width) {
height = (image.getHeight() * width) / image.getWidth();
} else {
width = image.getWidth();
height = image.getHeight();
}
} else {
if (image.getHeight() > height) {
width = (image.getWidth() * height) / image.getHeight();
} else {
width = image.getWidth();
height = image.getHeight();
}
}
}
// 转换 输出
return convert(width, height, input, output);
}
}
给出一个简单的测试类:
- import org.junit.Test;
- public class ImageUtilsTest {
- /**
- * Test method for
- * {@link org.zlex.common.image.ImageUtils#main(java.lang.String[])}.
- */
- @Test
- public void test() throws Exception {
- System.out.println(ImageUtils.convert(1650, 1024, "c://1.png",
- "c://1.png.jpg"));
- System.out.println(ImageUtils.convert(400, 300, "c://1.jpg",
- "c://1.jpg.jpg"));
- System.out.println(ImageUtils.convert(400, 300, "c://1.jpg",
- "c://1.jpg.png"));
- System.out.println(ImageUtils.convert(50, 50, "c://1.jpg",
- "c://1.jpg.gif"));
- System.out.println(ImageUtils.convert(40, 30, "c://1.bmp",
- "c://1.bmp.gif"));
- System.out.println(ImageUtils
- .convert(40, 30, "c://1.bmp", "c://1.jpeg"));
- System.out.println(ImageUtils.equimultipleConvert(1600, 1400, new File(
- "c://1.bmp"), new File("c://1Equimultiple.jpeg")));
- }
- }