package slice;
imp
imp
imp
imp
imp
imp
public class ImageUtils
{
/**
* 缩放图像
* @param srcImageFile 源图像文件地址
* @param result 缩放后的图像地址
* @param scale 缩放比例
* @param flag 缩放选择:true 放大; false 缩小;
*/
public static void scale(String srcImageFile, String result, int scale, boolean flag)
{
try
{
BufferedImage src = ImageIO.read(new File(srcImageFile)); // 读入文件
int width = src.getWidth(); // 得到源图宽
int height = src.getHeight(); // 得到源图长
if (flag)
{
// 放大
width = width * scale;
height = height * scale;
}
else
{
// 缩小
width = width / scale;
height = height / scale;
}
Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 绘制缩小后的图
g.dispose();
ImageIO.write(tag, "JPEG", new File(result));// 输出到文件流
}
catch (IOException e)
{
e.printStackTrace();
}
}
/**
* 图像切割
* @param srcImageFile 源图像地址
* @param descDir 切片目标文件夹
* @param destWidth 目标切片宽度
* @param destHeight 目标切片高度
*/
public static void cut(String srcImageFile, String descDir, int destWidth, int destHeight)
{
try
{
// 读取源图像
BufferedImage bi = ImageIO.read(new File(srcImageFile));
cut(bi, descDir, destWidth, destHeight);
} catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 图像切割
* @param srcImageFile 源图像地址
* @param descDir 切片目标文件夹
* @param destWidth 目标切片宽度
* @param destHeight 目标切片高度
*/
public static void cut(BufferedImage bi, String descDir, int destWidth, int destHeight)
{
try
{
int srcWidth = bi.getWidth(); // 源图宽度
int srcHeight = bi.getHeight(); // 源图高度
if (srcWidth > destWidth && srcHeight > destHeight)
{
int cols = 0; // 切片横向数量
int rows = 0; // 切片纵向数量
// 计算切片的横向和纵向数量
cols = (int) Math.ceil( (double)srcWidth / destWidth);
rows = (int) Math.ceil( (double)srcHeight / destHeight);
// System.out.print(cols + ":" + rows);
BufferedImage tag = null;
// 循环建立切片
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
// 四个参数分别为图像起点坐标和宽高
try {
tag = bi.getSubimage(j * destWidth, i * destHeight, destWidth, destHeight);
} catch (RasterFormatException e) { //宽高不为整数倍时
if (srcWidth % destWidth > 0 || srcHeight % destHeight > 0) {
tag = bi.getSubimage(j * destWidth, i * destHeight, srcWidth - ((cols-1) * destWidth), srcHeight - ((rows-1) * destHeight));
}
}
// 输出为文件
ImageIO.write(tag, "JPEG", new File(descDir + j + "_" + i + ".jpg"));
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
/** *//**
* 图像类型转换 GIF->JPG GIF->PNG PNG->JPG PNG->GIF(X)
*/
public static void convert(String source, String result)
{
try
{
File f = new File(source);
f.canRead();
f.canWrite();
BufferedImage src = ImageIO.read(f);
ImageIO.write(src, "JPG", new File(result));
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/** *//**
* 彩色转为黑白
* @param source
* @param result
*/
public static void gray(String source, String result)
{
try
{
BufferedImage src = ImageIO.read(new File(source));
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op = new ColorConvertOp(cs, null);
src = op.filter(src, null);
ImageIO.write(src, "JPEG", new File(result));
}
catch (IOException e)
{
e.printStackTrace();
}
}
/** *//**
* @param args
*/
public static void main(String[] args)
{
// scale("D:\100CASIO\CIMG0001.JPG","C:\Documents and Settings\ibm\桌面\image.jpg",10,false);
}
}
评论