通过蓝牙从 ESP32 发送图像到 Android Studio 时出现“Lost bytes”。

5

首次在这里提问。我目前遇到了一个问题,即将从ESP32 CAM拍摄的图片发送到我的Android Studio应用程序时出现问题。

尽管图片被接收,但它大多数时候不完整,或者像附加图片中所示,显示一些灰色区域。

我注意到可用字节从图片到图片会有所变化,所以在绝望之下,我循环输出/输入流以拍摄图片,直到可用字节超过14000。尽管有很多可用字节,但图片很多时候(虽然不总是)显示了一个大块灰色像素。

我读到过这可能是因为蓝牙套接字必须在“finally”异常中关闭,但我无法使其正常工作。

非常感谢您的帮助!

private fun tomarFoto() { //Function to take the picture by sending an OutputStream and receiving the taken picture bytes through InputStream
  var bytes : ByteArray? = null
  val fotoEsp : ImageView = findViewById(R.id.fotoESP)
  var available = 0

  if (m_bluetoothSocket != null) {
    try {
      CoroutineScope(IO).launch {
        for (i in 0..4) {
          async {            
            m_bluetoothSocket!!.outputStream.write("a".toByteArray())
            delay(1500)
          }.await()

          available = m_bluetoothSocket!!.inputStream.available()
          println("Available1: ${available}")
          if (available > 14000) {
            break
          }
        }

        println("Available2: ${available}")
        bytes = ByteArray(available)
        m_bluetoothSocket!!.inputStream.read(bytes, 0, available)
        val bmp = BitmapFactory.decodeByteArray(bytes, 0, available)

        if (bmp != null) { //Flip image upside down
          fun Bitmap.flip(x: Float, y: Float, cx: Float, cy: Float): Bitmap {
            val matrix = Matrix().apply { postScale(x, y, cx, cy) }
          return Bitmap.createBitmap(this, 0, 0, width, height, matrix, true)
        }

        val cx = bmp.width / 2f
        val cy = bmp.height / 2f
        val flippedBitmap = bmp.flip(1f, -1f, cx, cy)
        runOnUiThread {
          fotoEsp.setImageBitmap(flippedBitmap)
        }
      } else {
        runOnUiThread {
          fotoEsp.setImageBitmap(bmp)
        }
      }
    }
  } catch(e: IOException) {
      Log.e("client", "Cannot read data", e)
      e.printStackTrace()
    }
  }
}

图片显示接收到的照片中存在灰色区域:

在此输入图片描述


检查nread。它不会等于available,而是小于它。var nread = m_bluetoothSocket!!.inputStream.read(bytes, 0, available) - blackapps
我从两者中实际上得到了相同的值。 我还检查了Arduino Sketch在串行监视器中所占用的字节数,它与接收到的相同,这意味着问题不在Android应用程序中,而是在Sketch/ESP32CAM中。 - Alejandro Monsalve Krause
2个回答

0

看起来framebuffer不是解决方案。 我正在使用:

Cconfig.frame_size = FRAMESIZE_SVGA;
Cconfig.jpeg_quality = 12;
Cconfig.fb_count = 1;

并且照片从底部向上变灰,大约到1/4的高度。

但是只有在白天非常明亮(外部视图)时才会发生这种情况。 在早晨和晚上,图片是完整的。 还有当存在灰色、黑暗的中午。


你认为太阳的亮度会影响你的BLE传输吗? - Michael Kotzjan

0

对于任何遇到这个问题的人,我终于找到了问题/解决方案。

如果您正在使用ESP32库的“CameraWebServer”示例进行图片配置,则可能需要以下参数:

  if (psramFound()) {
   Serial.println("psramFound() true");
   config.frame_size = FRAMESIZE_UXGA;
   config.jpeg_quality = 10;
   config.fb_count = 2;
  } else { ........

然而,我发现将“fb_count”设置为1解决了我的问题。

我还不得不将jpeg_quality更改为20,您可以尝试调整此参数以找到最佳的图像质量,该数字从0到63,其中0是最佳质量。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接