从蓝牙的输入流获取图像的字节数组

时间:2014-09-04 06:36:38

标签: java android bluetooth

我想通过蓝牙从硬件接收android中的jpeg图像。 我想读取输入流并显示字节数组中的实际数据。 我的实际数据如下:

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

我使用了这段代码:

 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();


}

我得到的是:

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

如何在logcat中显示实际结果(十六进制值),然后从十六进制值显示jpg图像?

我使用以下代码从相机发送jpg图像数据: main.cpp http://pastebin.com/Tx8YkbYF JPEGCamera.cpp http://pastebin.com/0jHQ8WvT

1 个答案:

答案 0 :(得分:0)

  

(1)如何在logcat中显示实际结果(十六进制值)?

由于您已经拥有了字节值,因此您只需根据HEX base格式化这些值。 String.format("%02X ", ...)会做得很好:

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)然后从十六进制值显示jpg图像

如果您确定这些字节是JPEG编码图像的字节,您可以按原样将它们解码为位图:

ImageView imageView = ...; //load image view defined in xml file
Bitmap bitmap = BitmapFactory.decodeByteArray(packetBytes, 0 , packetBytes.length);
imageView.setImageBitmap(bitmap);