首页 > 学院 > 开发设计 > 正文

图片类型转换,如png转换为jpg

2019-11-06 08:59:58
字体:
来源:转载
供稿:网友
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;    }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表