Java-将bufferedimage转换为byte []而不写入磁盘 [英] Java- Convert bufferedimage to byte[] without writing to disk

查看:257
本文介绍了Java-将bufferedimage转换为byte []而不写入磁盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用java在套接字上发送多个图像,但我需要一种更快的方法将图像转换为字节数组,以便我可以发送它们。我尝试了以下代码,但它向我的C:\驱动器写了大约10,000张图像。有没有办法在不写入磁盘的情况下进行此转换?谢谢!

I'm trying to send multiple images over a socket using java but I need a faster way to convert the images to a byte array so I can send them. I tried the following code but it wrote about 10,000 images to my C:\ drive. Is there a way to make this conversion without writing to disk? Thanks!

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

                    //ImageIO.setUseCache(false);
                    ImageIO.write(bi.getImage(), "jpg", outputStream);

                    byte[] imageBytes = outputStream.toByteArray();


推荐答案

这应该有效:

byte[] imageBytes = ((DataBufferByte) bufferedImage.getData().getDataBuffer()).getData();

这篇关于Java-将bufferedimage转换为byte []而不写入磁盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆