利用java开发中遇到图片转换格式需求怎么办,利用以下方法轻松搞定。
1.利用流的方式,ImageIO调用write方法写出,选写需要的图片格式,例如”png,jpg等“
/**
* 转png格式
*
* @param inputStream
* @throws IOException
*/
public static void toPng(ImageInputStream inputStream) throws IOException {
BufferedImage bufferedImage = ImageIO.read(inputStream);
BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.white, null);
ImageIO.write(newBufferedImage, "png", new File("D:/" + UUID.randomUUID(true).toString() + ".png"));
}
2.传入原图的保存地址
/**
* 转jpg格式
*
* @param picPath
* @throws IOException
*/
public static void toJpg(String picPath) throws IOEx