2

I'm making a client-server application and I would like to send images using a byte array. I'm using this code to convert the image into a byte array and then to a String:

// converting the image into bytes
BufferedImage bufferedImage = ImageIO.read(new File(filePath));
WritableRaster raster = bufferedImage.getRaster();
DataBufferByte data = (DataBufferByte) raster.getDataBuffer();
byte[] bytes = data.getData();
System.out.println("Size of array is " + bytes.length);
String base64String = Base64.encode(bytes);
System.out.println("size of base64String to send is " + base64String.length());

I'm getting this output:

Size of array is 743904

size of base64String to send is 991872

size of the image that i want to send is 42.4KB

I'm using this code:

byte[] bytearray = Base64.decode(newMessage.getMessageToAdd());
System.out.println("Size of array is " + bytearray.length);
OutputStream outimg = null;
try {
    outimg = new BufferedOutputStream(new FileOutputStream(pathToSave));
    outimg.writewrite(bytearray);
} finally {
    if (outimg != null) {
        outimg.close();
    }
}

I get this output:

size of array base64String recived is 991872
Size of array is 743904
and the size of the image created is 726KB

So from the output I understand that it recived and sent the image without getting a error but for some reason it fails at creating the image.

10
  • Could you clarify the output? I tried to format it but it's confusing, especially the second part. I just left it as is to avoid changing the meaning of your question. Commented May 10, 2015 at 16:43
  • 1
    Why are you not sending the contents of the file? Commented May 10, 2015 at 16:47
  • the output i get from the server is size of array base64String recived is 991872 Size of array is 743904 Commented May 10, 2015 at 16:48
  • it craete a file of size 726KB Commented May 10, 2015 at 16:48
  • 1
    Indeed, why that unnecessary ImageIO fuzz? You don't need to manipulate (resize, crop, colorize, etc) the image, right? Just use Files#readAllBytes() or so. Commented May 10, 2015 at 16:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.