通过图片的IO流穿入, 即可输出一张同样高和宽, 大小小很多的图片!!
/**
* 穿入Inputstream图片IO流即可
*
* @param stream
*/
public void SavePngImage(InputStream stream) {
int w = 0;
int h = 0;
BufferedImage bufImg = null;
try {
BufferedImage image = ImageIO.read(stream);
w = image.getWidth();
h = image.getHeight();
bufImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
//bufImg = ImageUtil.createCompatibleImage(w, h, Transparency.OPAQUE);
Graphics2D g2d = bufImg.createGraphics();
//File file = new File("e:\\2.gif");
//javax.imageio.ImageIO.read(file);
g2d.drawImage(image, 0, 0, null);
g2d.dispose();
} catch (IOException e) {
e.printStackTrace();
}
//输出的图片路径
saveImage(bufImg, "c:/test.jpg");
}
// 保存图片
public void saveImage(BufferedImage img, String path) {
try {
String extension = path.substring(path.lastIndexOf('.') + 1);
extension = isFormatSupported(extension) ? extension : "png";
ImageIO.write(img, extension, new File(path));
} catch (Exception e) {
e.printStackTrace();
}
}
// 判断ImageIO是否支持指定的图片格式
public static boolean isFormatSupported(String format) {
for (String f : ImageIO.getWriterFormatNames()) {
if (f.equalsIgnoreCase(format)) { return true; }
}
return false;
}
public static void main(String[] args){
File file = new File("c:/input.jpg");
xxx.SavePngImage(file.getInputStream());
}