1

I want to receive a jpeg image in android from a hardware via Bluetooth . I want to read the inputstream and show the actual data from byte array. my actual data looks like:

ff d8 ff e1 63 70 45 78 69 66 00 00 49 49 2a 00
08 00 00 00 0b 00 0f 01 02 00 06 00 00 00 92 00
.........................ff d9

I used this code:

 void openBT() throws IOException
{
    UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard SerialPortService ID
    mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);        
    mmSocket.connect();
    mmOutputStream = mmSocket.getOutputStream();
    mmInputStream = mmSocket.getInputStream();


    beginListenForData();
    //ConnectedThread(mmSocket);
    myLabel.setText("Bluetooth Opened");


}


void beginListenForData()
{
    final Handler handler = new Handler(); 
    final byte delimiter = 10; //This is the ASCII code for a newline character

    stopWorker = false;
    readBufferPosition = 0;
    readBuffer = new byte[1024];
    workerThread = new Thread(new Runnable()
    {
        public void run()
        {                
           while(!Thread.currentThread().isInterrupted() && !stopWorker)
           {
                try 
                {
                    int bytesAvailable = mmInputStream.available();                        
                    if(bytesAvailable > 0)
                    {
                        final byte[] packetBytes = new byte[bytesAvailable];
                        mmInputStream.read(packetBytes);

                        StringBuilder sb = new StringBuilder();
                        for (byte b : packetBytes) {
                            sb.append(String.format("%02X ", b)).append(" ");//there's no need to mask it with 0xFF. Don't forget to leave a " " between bytes so you may distinguish them.
                        }
                        Log.d("data","data"+sb.toString());


                        handler.post(new Runnable()
                        {
                            public void run()
                            {
                                //ByteArrayInputStream imageStream = new ByteArrayInputStream( packetBytes);
                               // Bitmap bitmap = BitmapFactory.decodeStream(imageStream);
                               // image.setImageBitmap(bitmap);
                            }
                        });
                    }


                } 
                catch (IOException ex) 
                {
                    stopWorker = true;
                }
           }
        }
    });

    workerThread.start();


}

What i am getting is that:

6163206339203564203264203138203...........
463206620666620643920

How to show the actual result(hex values) in logcat and then show the jpg image from the hex value??

I send jpg image data from camera using the following code: main.cpp http://pastebin.com/Tx8YkbYF JPEGCamera.cpp http://pastebin.com/0jHQ8WvT

ImonBayazid
  • 1,066
  • 3
  • 16
  • 41

1 Answers1

0

(1) How to show the actual result(hex values) in logcat?

Since you already have the byte values, you just have to format these values according to HEX base. String.format("%02X ", ...) will do just fine:

final byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);

StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
    sb.append(String.format("%02X ", b)).append(" ");//there's no need to mask it with 0xFF. Don't forget to leave a " " between bytes so you may distinguish them.
}
Log.d("data: " + sb.toString());


(2) Then show the jpg image from the hex value??

If you're sure those bytes are the bytes of a JPEG encoded image, you can decode them, as they are, into a Bitmap :

ImageView imageView = ...; //load image view defined in xml file
Bitmap bitmap = BitmapFactory.decodeByteArray(packetBytes, 0 , packetBytes.length);
imageView.setImageBitmap(bitmap);
Daniel
  • 1,861
  • 1
  • 15
  • 23
  • Got the same result as previous... if u want i can provide the code which the camera uses for sending data. @Daniel – ImonBayazid Sep 04 '14 at 12:10
  • May be the bytearray are not correctly received in my app. But i tried with another app it can receive the and show the jpg formatted data which starts ffd8 and ends with ffd9. What is my fault?? @Daniel – ImonBayazid Sep 04 '14 at 12:35
  • For camera: JPEGCamera.cpp-> http://pastebin.com/0jHQ8WvT main.cpp-> http://pastebin.com/Tx8YkbYF – ImonBayazid Sep 04 '14 at 12:38
  • I have edited my question with the code that i used to receive data. @Daniel – ImonBayazid Sep 04 '14 at 13:30