Java-Konwersja bufferedimage na bajt[] bez zapisu na dysk

Próbuję wysłać wiele obrazów przez gniazdo za pomocą Javy, ale potrzebuję szybszego sposobu konwersji obrazów do tablicy bajtów, aby móc je wysłać. Próbowałem poniższego kodu, ale napisał około 10,000 obrazów do mojego C:\ drive. Czy istnieje sposób na wykonanie tej konwersji bez zapisu na dysk? Dzięki!

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

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

                    byte[] imageBytes = outputStream.toByteArray();
Author: tier1, 2012-04-20

6 answers

To powinno zadziałać:

byte[] imageBytes = ((DataBufferByte) bufferedImage.getData().getDataBuffer()).getData();
 41
Author: stacker,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-20 13:26:24

Poniższy kod jest naprawdę szybki (kilka milisekund)

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public byte[] toByteArray(BufferedImage image) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();            
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(baos);
    encoder.encode(image);            
    return baos.toByteArray();
}
 2
Author: EderBaum,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-07 14:46:51

Spróbuj użyć:

ImageIO.setUseCache(false);

Przed napisaniem, może to pomoże.

 1
Author: soulcheck,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-20 13:28:29
BufferedImage img = ImageIO.read(new ByteArrayInputStream(bytes));
byte[] bytes = new byte[buf.capacity()];
buf.get(bytes, 0, bytes.length);
 0
Author: Zaz Gmy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-20 13:28:44
ByteArrayOutputStream baos;
ImageIO.write(bufferedImage, "png", baos);
byte[] imageBytes = baos.toByteArray();
 0
Author: bugCracker,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-20 04:13:06

Use Apache Commons IO Utils Apache Commons

IOUtils.copy(inputStream,outputStream);

IO Utils API z łatwością obsługuje duże bufory

 -2
Author: Phani,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-20 13:26:52