java 获取字节数组并创建和写入 JPEG
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7035095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Taking a bytearray and creating and writing a JPEG
提问by Bill Lee
I have a program that
我有一个程序
- Accepts an encoded base64 string
- Converts it to a byte array.
- 接受编码的 base64 字符串
- 将其转换为字节数组。
It does this fine.
它做得很好。
The final step involves writing this byte array to a file. For example C:\example.jpg. I know simply writing the bytes won't work so was not sure what I would need to do to take the byte array and create a jpg with the picture that is coming in.
最后一步涉及将此字节数组写入文件。例如 C:\example.jpg。我知道简单地写字节是行不通的,所以不确定我需要做什么来获取字节数组并使用传入的图片创建一个 jpg。
I have to actually send the picture as an attachment in email, but for testing purposes wanted to see if I can see a file saved and when I open up the jpg opens up. Thanks.
我实际上必须将图片作为电子邮件的附件发送,但出于测试目的,我想看看我是否可以看到保存的文件,以及当我打开 jpg 时。谢谢。
回答by DeusExMachina25
I'm guessing you're in need of something like this?
我猜你需要这样的东西?
BufferedImage image = ImageIO.read( new ByteArrayInputStream( byteArray ) );
ImageIO.write(image, "BMP", new File("filename.bmp"));
Check out this question and answer, that's where I acquired it from: How to create a BMP file from raw byte[] in Java
看看这个问题和答案,这是我从那里获得的: How to create a BMP file from raw byte[] in Java