I have an array of integers which represent a RGB image and would like to convert it to a byte array and save it to a file.
What’s the best way to convert an array of integers to the array of bytes in Java?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As Brian says, you need to work out how what sort of conversion you need.
Do you want to save it as a “normal” image file (jpg, png etc)?
If so, you should probably use the Java Image I/O API.
If you want to save it in a “raw” format, the order in which to write the bytes must be specified, and then use an
IntBufferand NIO.As an example of using a ByteBuffer/IntBuffer combination: