java中對圖片進行壓縮以及放大。

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;

import javax.imageio.ImageIO;

public class ImageUtils
{

/**
* 對圖片進行放大
*
* @param originalImage 原始圖片
* @param times 放大倍數
* @return
*/
public static BufferedImage zoomInImage(BufferedImage originalImage, Integer times)
{
int width = originalImage.getWidth() * times;
int height = originalImage.getHeight() * times;
BufferedImage newImage = new BufferedImage(width, height, originalImage.getType());
Graphics g = newImage.getGraphics();
g.drawImage(originalImage, 0, 0, width, height, null);
g.dispose();
return newImage;
}

/**
* 對圖片進行放大
*
* @param srcPath 原始圖片路徑(絕對路徑)
* @param newPath 放大後圖片路徑(絕對路徑)
* @param times 放大倍數
* @param format 圖片格式
* @return 是否放大成功
*/
public static boolean zoomInImage(String srcPath, String newPath, Integer times, String format)
{
BufferedImage bufferedImage = null;
try
{
File of = new File(srcPath);
if (of.canRead())
{
bufferedImage = ImageIO.read(of);
}
} catch (IOException e)
{
// TODO: 打印日誌
return false;
}
if (bufferedImage != null)
{
bufferedImage = zoomInImage(bufferedImage, times);
try
{
// TODO: 這個保存路徑需要配置下子好一點
ImageIO.write(bufferedImage, format, new File(newPath)); // 保存修改後的圖像
} catch (IOException e)
{
// TODO 打印錯誤信息
return false;
}
}
return true;
}

/**
* 對圖片進行縮小
*
* @param originalImage 原始圖片
* @param times 縮小倍數
* @return 縮小後的Image
*/
public static BufferedImage zoomOutImage(BufferedImage originalImage, int timeshieght)
{
int width = originalImage.getWidth() / timeshieght;
int height = originalImage.getHeight() / timeshieght;
BufferedImage newImage = new BufferedImage(width, height, originalImage.getType());
Graphics g = newImage.getGraphics();
g.drawImage(originalImage, 0, 0, width, height, null);
g.dispose();
return newImage;
}

/**
* 對圖片進行縮小
*
* @param srcPath 源圖片路徑(絕對路徑)
* @param newPath 新圖片路徑(絕對路徑)
* @param times 縮小倍數
* @param format 圖片格式
* @return 保存是否成功
*/
public static boolean zoomOutImage(String srcPath, String newPath, int timeshieght,
String format)
{
BufferedImage bufferedImage = null;
try
{
File of = new File(srcPath);
if (of.canRead())
{
bufferedImage = ImageIO.read(of);
}
} catch (IOException e)
{
// TODO: 打印日誌
return false;
}
if (bufferedImage != null)
{
bufferedImage = zoomOutImage(bufferedImage, timeshieght);
try
{
// TODO: 這個保存路徑需要配置下子好一點
ImageIO.write(bufferedImage, format, new File(newPath)); // 保存修改後的圖像,全部保存爲JPG格式
} catch (IOException e)
{
// TODO 打印錯誤信息
return false;
}
}
return true;
}

public static int getHeight(String path)
{
BufferedImage bufferedImage = null;
int height = 0;
try
{
bufferedImage = ImageIO.read(new File(path));
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
height = bufferedImage.getHeight();
return height;
}

public static void main(String[] args)
{
int heighttimes = (new BigDecimal((double) ImageUtils.getHeight("E:/1.bmp") / (double) 260)).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
int heighttime = (new BigDecimal( (double) 260 / ImageUtils.getHeight("E:/bell_add.png"))).setScale(0, BigDecimal.ROUND_HALF_UP)
.intValue();
boolean testIn = zoomInImage("E:/bell_add.png", "E:\\in2.jpg", heighttime, "png");
if (testIn)
{
System.out.println("in ok");
}
// boolean testOut = zoomOutImage("E:/11.jpg", "E:\\out.jpg",
// heighttimes, "jpg");
// if (testOut)
// {
// System.out.println("out ok");
// }

}

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章