【问题标题】:Conversion Bluetooth inputstream to an image, java android app将蓝牙输入流转换为图像,java android app
【发布时间】:2022-02-01 11:44:16
【问题描述】:

如何将蓝牙输入流转换为 java android 应用程序中的图像? 数据被分成块。在输入流中逐块捕获,但它不能容纳超过一个块。这个想法是将所有块(逐个流)放在一个字节数组中,然后制作一个位图。

【问题讨论】:

标签: java android image bluetooth inputstream


【解决方案1】:
    private val mmInStream: InputStream = mmSocket.inputStream
           private val mmOutStream: OutputStream = mmSocket.outputStream
           private val mmBuffer: ByteArray = ByteArray(1024) // mmBuffer store for the stream
    
           override fun run() {
               var numBytes: Int // bytes returned from read()
    
               // Keep listening to the InputStream until an exception occurs.
               while (true) {
                   // Read from the InputStream.
                   numBytes = try {
                       mmInStream.read(mmBuffer)
                   } catch (e: IOException) {
                       Log.d(TAG, "Input stream was disconnected", e)
                       break
                   }
    
                   }


// Now the bytes read can be write to a file let's say .png/.jpeg image file.
   fun write(bytes: ByteArray) {
       try {
           mmOutStream.write(bytes)
       } catch (e: IOException) {
           Log.e(TAG, "Error occurred when sending data", e)
          
           }
      
       }
      
   }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    • 2015-05-24
    • 2011-09-30
    相关资源
    最近更新 更多