【问题标题】:get byte array of image from inputstream of Bluetooth从蓝牙的输入流中获取图像的字节数组
【发布时间】:2014-10-28 19:17:50
【问题描述】:

我想通过蓝牙从硬件接收 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 图像数据: 主.cpp http://pastebin.com/Tx8YkbYF JPEGCamera.cpp http://pastebin.com/0jHQ8WvT

【问题讨论】:

  • 你检查Byte Array to Image File了吗?
  • 是的检查但没有结果@Daniel
  • 你会帮忙吗? @丹尼尔
  • “显示 jpg 图像”到底是什么意思?您希望在哪里显示此图片?
  • 我想将图像显示到图像视图中。我尝试了超过 4 天。但我失败了。 @丹尼尔

标签: java android bluetooth


【解决方案1】:

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

由于您已经有了字节值,您只需根据 HEX 基数格式化这些值。 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);

【讨论】:

  • 得到与之前相同的结果...如果您愿意,我可以提供相机用于发送数据的代码。 @丹尼尔
  • 可能是我的应用程序没有正确接收到字节数组。但我尝试使用另一个应用程序,它可以接收并显示以 ffd8 开头并以 ffd9 结尾的 jpg 格式数据。我的错是什么?? @丹尼尔
  • 对于相机:JPEGCamera.cpp-> pastebin.com/0jHQ8WvTmain.cpp-> pastebin.com/Tx8YkbYF
  • 我用我用来接收数据的代码编辑了我的问题。 @丹尼尔
猜你喜欢
  • 2016-03-27
  • 1970-01-01
  • 1970-01-01
  • 2012-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多