java学习-BufferedImage转byte[]

小白学习使用,直接上代码

public class ImagesTest {
    //BufferedImage转流
    public ByteArrayOutputStream image2Stream(BufferedImage image) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, format, out);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return out;
    }
    //BufferedImage转byte[]
    public byte[] image2byte(BufferedImage image) {
        ByteArrayOutputStream out = image2Stream(image);
        byte[] outToByteArray = out.toByteArray();
        return outToByteArray;
    }

    public static void main(String[] args) throws IOException {
        File dir =new File("imgs");
        BufferedImage image =ImageIO.read(new File(dir,"jietu.png"));
        byte[] res = image2byte(image);

        OutputStream out = new FileOutputStream(dir+"\\b.png");
        out.write(res);
        out.flush();
        out.close();

    }
}

 

发布了4 篇原创文章 · 获赞 0 · 访问量 2344
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览