0

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?

MWiesner
  • 8,868
  • 11
  • 36
  • 70
John Ng
  • 869
  • 3
  • 15
  • 28
  • How are you taking the Picture from Glass? Are you using an `Intent` or the `Camera` class? – Alain Oct 09 '14 at 15:29
  • @Alain I'm using the Camera class. I've updated my post. It probably doesn't have anything to do with the byte array and BT communication because the phone is able to save the image to file storage perfectly. – John Ng Oct 09 '14 at 18:25
  • Thanks for the update, seems to be more related to the Android code running on the phone than Glass itself. Have you tried taking a picture from the phone, getting a byte array from it and using the same code path to decode it? – Alain Oct 09 '14 at 23:02

0 Answers0