Me and a group of friends are working on a project in Java, and we need some help regarding sending objects through sockets.
So far, we have achieved to send simple
You can convert BufferedImage to byte array on client side and send then as normal byte array and on the server side create BufferedImage from that byte array. Below is the code to convert BufferedImage to byte array and vice versa.
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
public class ImageTest {
public static void main(String[] args) {
try {
byte[] imageInByte;
BufferedImage originalImage = ImageIO.read(new File(
"c:/darksouls.jpg"));
// convert BufferedImage to byte array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(originalImage, "jpg", baos);
baos.flush();
imageInByte = baos.toByteArray();
baos.close();
// convert byte array back to BufferedImage
InputStream in = new ByteArrayInputStream(imageInByte);
BufferedImage bImageFromConvert = ImageIO.read(in);
ImageIO.write(bImageFromConvert, "jpg", new File(
"c:/new-darksouls.jpg"));
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}