2014-09-04 41 views
1

我想通過藍牙從硬件接收安卓的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

你檢查[字節數組圖像文件](http://stackoverflow.com/questions/1580038/byte-array-to-image-file)? – Daniel 2014-09-04 06:42:50

+0

是的檢查,但沒有結果@Daniel – DarkenShooter 2014-09-04 07:33:24

+0

你會幫忙嗎? @Daniel – DarkenShooter 2014-09-04 07:37:50

回答

0

(1)如何顯示logcat的實際結果(十六進制值)?

既然你已經有字節值,你只需要根據十六進制的基礎來格式化這些值。 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); 
+0

得到了與以前相同的結果...如果你想我可以提供相機用於發送數據的代碼。 @Daniel – DarkenShooter 2014-09-04 12:10:49

+0

可能是我的應用程序沒有正確接收到字節陣列。但我嘗試了另一個應用程序,它可以接收並顯示jpg格式的數據,它啓動ffd8並以ffd9結束。我的錯是什麼? @Daniel – DarkenShooter 2014-09-04 12:35:15

+0

對於攝像頭:JPEGCamera.cpp-> http://pastebin.com/0jHQ8WvT main.cpp-> http://pastebin.com/Tx8YkbYF – DarkenShooter 2014-09-04 12:38:38