package com.github.elizabetht.controller;import java.awt.Color;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;public class ConvertImageFile { public static void main(String[] args) { BufferedImage bufferedImage; try { //read image file bufferedImage = ImageIO.read(new File("c://a.png")); //conver BufferedImage newBufferedImage = toOpaqueImage(bufferedImage); // write to jpeg file ImageIO.write(newBufferedImage, "jpg", new File("c://b.jpg")); System.out.PRintln("finished"); } catch (IOException e) { e.printStackTrace(); } } public static BufferedImage toOpaqueImage(BufferedImage input){ //set color return toOpaqueImage(input,Color.WHITE); } /** * * @param input source * @param bgColor backgroundColor * @return */ public static BufferedImage toOpaqueImage(BufferedImage input,Color bgColor){//create a blank, RGB, same width and height, and a white background BufferedImage output=new BufferedImage(input.getWidth(), input.getHeight(), BufferedImage.TYPE_INT_RGB); output.createGraphics().drawImage(input, 0, 0, input.getWidth(), input.getHeight(), bgColor, null); return output; }}
新闻热点
疑难解答