BufferedImage byte[] 互转

BufferedImage byte[] 互转

BufferedImage 转 byte[]

    /**
     * BufferedImage 转 byte[]
     *
     * @param bImage
     * @return byte[]
     * @author 云深小麦
     */
    public static byte[] imageToBytes(BufferedImage bImage) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            ImageIO.write(bImage, "jpg", byteArrayOutputStream);
            return byteArrayOutputStream.toByteArray();
        } catch (IOException ignored) {
            return null;
        }
    }

byte[] 转 BufferedImage

    /**
     * byte[] 转 BufferedImage
     *
     * @param bytes
     * @return BufferedImage
     * @author 云深小麦
     */
    public static BufferedImage bytesToImage(byte[] bytes) {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        try {
            return ImageIO.read(byteArrayInputStream);
        } catch (IOException ignored) {
            return null;
        }
    }
posted @ 2022-01-04 10:36  云深小麦  阅读(137)  评论(0编辑  收藏  举报
Mac免费软件下载: macrank.com