通过对图片重绘,达到图片缩放、压缩编码转换功能。
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;
/**
*
* @author 梁栋
* @version 0
* @since 0
*/
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:\\png",
"c:\\png.jpg"));
System.out.println(ImageUtils.convert(400, 300, "c:\\jpg",
"c:\\jpg.jpg"));
System.out.println(ImageUtils.convert(400, 300, "c:\\jpg",
"c:\\jpg.png"));
System.out.println(ImageUtils.convert(50, 50, "c:\\jpg",
"c:\\jpg.gif"));
System.out.println(ImageUtils.convert(40, 30, "c:\\bmp",
"c:\\bmp.gif"));
System.out.println(ImageUtils
.convert(40, 30, "c:\\bmp", "c:\\jpeg"));
System.out.println(ImageUtils.equimultipleConvert(1600, 1400, new File(
"c:\\bmp"), new File("c:\\1Equimultiple.jpeg")));
}
}
原文地址:http://snowolf.iteye.com/blog/250517
分享到:
相关推荐
很多的一个图片批量缩放转换器,能比例转换图片大小,
c#实现的小工具,用于批量图片缩放,格式转换。
可以批量压缩图片的工具,有按比例压缩与按大小压缩两种方式,最小最快最简洁的绿色图片批量缩放转换工具,可以根据比例和特定长宽进行图片文件的缩放,可以对PNG、BMP、JPG、GIF、TIF图片文件相互进行转换。
VB图片缩放转换精灵源程序,可以对图片进行放大缩小操作。针对图片处理。
c#实现批量图片缩放,格式转换程序 (源码)
基于java的图片文件格式转换和线性缩放 图片文件格式转换最核心的问题就是要解决各种图片格式的编码和解码,
1.对图片进行缩放处理2.截取图片中的一部分3.将图片缩放并嵌入到另一张纯色图片中4.图片添加水印:添加图片水印和文字水印5.控制Jpg图片的输出质量6.将文本信息隐藏在图片文件中(建议使用BMP图片)
非常简单而又非常实用的图像处理工具,只需事先设置好自己目标图像的参数(文件大小,图像格式),点击“批量转换”图像处理很快处理完毕。如果批处理后没有满足自己的要求,可以改变参数值重新处理,直到满意为止,...
实现了对上传的图片进行缩放显示,并将图片转换为二进制流(将其保存到数据库中,及实现了将图片存储到数据库中),之后通过动态页面显示生产的图片,避免了上传图片显示的路径问题
包含最近邻插值、双线性插值和三次插值的matlab代码,自己已经测试过,使用的时候注意将图片转换为位图,否则会出现错误。
高清缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等 功能非常强大,并且很清晰,请给好评:)
图形转换与缩放(平滑缩放)
图片裁剪,缩放,转换格式等小工具,含源代码
simpleImageTool又一个简单、好用的图片格式转换、缩放水印叠加等功能的纯Java图片工具库。 simpleImageTool的由来,近期需要用到图片处理,通过网上的图片流直接进行缩放水印叠加等,需要一个纯java的处理库,在...
本工具是一个jar包,通过java -jar的方式运行即可。实现了图片大小缩放、格式转换(tif、png、jpg、bmp等)、pdf转图片等功能。 适用人群:简单了解java使用的人群均可,需要电脑上安装jdk。
一个小工具,用易语言制作,界面友好,使用简单,既能实现对图片尺寸的批量缩放,又能实现图片格式的转换。
PB中图像转换、放大、缩小、旋转、打印等内容
图片文件格式转换最核心的问题就是要解决各种图片格式的编码和解码,推荐用jimi包,可从http://java.sun.com/products/jimi/下载该包及其源码和demo。本文介绍了用jimi进行图片文件格式转换和线性缩放的方法。
PB中图像图像的转换、放大、缩小、旋转、打印等内容,支持PB9开发,打开即用。此代码仅供参考。
图像缩放大师是一款专业的,支持批量修改图片大小软件,支持多种图片格式,如JPG, PNG, GIF, BMP, TIFF等;并且可以自由设置修改后的宽度、高度、是否保留纵横比等; 支持图片格式转换;操作简单,转换速度极快。 ...