java读取、生成图片

全栈工程师开发手册 (作者:栾鹏)

java教程全解

java读取、生成图片

测试代码

    public static void main(String[] args) {
        BufferedImage bi=file2img("test.jpg");  //读取图片
        img2file(bi,"jpg","test1.jpg");  //生成图片
    }

函数实现代码

    //读取图片
    public static BufferedImage file2img(String imgpath) {
        try {
            BufferedImage bufferedImage=ImageIO.read(new File(imgpath));
            return bufferedImage;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //保存图片,extent为格式,"jpg"、"png"等
    public static void img2file(BufferedImage img,String extent,String newfile) {
        try {
            ImageIO.write(img, extent, new File(newfile));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

你可能感兴趣的:(java,java开发手册)