I am developing a client app in the google glass that takes a picture and sends the byte array via bluetooth to an android phone that has a server app installed in it.
After sending all data to the phone, I tried to create a bitmap from the byte array and display it in the ImageView
. The exact number of bytes that is sent is received in the server. When I decode the byte array to create a bitmap, the image looks corrupted. The overall image looks correct but there are rectangular green/blue/red bars across the image. Sometimes a lot, sometimes just a few. Sometimes, the lower part of the image is just gray.
What could be the issue here? Or what can I try to do to find the problem.
Client code for writing to the outputstream:
int bufferSize = 512;
ByteBuffer bb = ByteBuffer.allocate(bufferSize);
bb.asLongBuffer().put(bytes.length);
mmOutStream.write(bb.array(), 0, bufferSize); //write size of data to be sent first
mmOutStream.write(bytes); //write the actual byte data
Server code for reading from the inputstream (this is in an infinite while loop):
len = mmInStream.read(buffer);
if (len > 0) {
ByteBuffer bb = ByteBuffer.wrap(buffer);
bytesToDownload = (int) bb.getLong();
totalBytes = 0;
byteBuffer = ByteBuffer.allocate(bufferSize * bufferSize * 2);
while (totalBytes < bytesToDownload) {
len = mmInStream.read(buffer);
totalBytes += len;
byteBuffer.put(buffer);
outputStream.write(buffer, 0, len);
}
//display image
Bitmap bmp = BitmapFactory.decodeByteArray(byteBuffer.array(), 0, totalBytes);
imageView.setImageBitmap(bmp);
}
UPDATE: When I save the completed image byte array to filoutputstream, the image looks perfect. Does it have something to do with BitmapFactory.decodeByteArray
?