5

First time asking here. I'm currently facing an issue with sent pictures taken from and ESP32 CAM to my Android Studio app.

Although the picture is received, it's most of the time not complete, or it shows some gray areas as shown on here in the attached picture.

I noticed the available bytes vary from picture to picture so in a desperate attempt I looped the Output/Input Streams to take pictures until the available bytes are over 14000. Still with a high number of available bytes, the picture many times (not always though) displays a big chunk of gray pixels.

I read this could be because the Bluetooth socket must be closed in a "finally" Exception, but I couldn't get that working though.

I'd really appreciate your help!

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

Picture showing gray area in received photo:

enter image description here

ocrdu
  • 2,172
  • 6
  • 15
  • 22
  • `var nread = m_bluetoothSocket!!.inputStream.read(bytes, 0, available)` Check `nread`. It wil not be equal to `available` but less. – blackapps Oct 06 '20 at 10:09
  • I actually got the same value from both. I also checked the amount of bytes taken on the Arduino Sketch from the serial monitor and it's the same that's received, meaning the problem is not in the Android Application, but in the Sketch/ESP32CAM. – Alejandro Monsalve Krause Dec 03 '20 at 21:46

2 Answers2

0

For anyone facing this issue, I finally got to the problem/solution.

If you're using the ESP32 Library's "CameraWebServer" example for the picture's configuration, you might aswell have the following parameters:

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

However, I found out that by setting the "fb_count" to 1 solved my issue.

I also had to change the jpeg_quality to 20, you can play around with this one to find the sweetspot in order to optimize your image quality, this number goes from 0 to 63, being 0 the best quality.

0

Seem that framebuffer is not the solution. I am using:

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

And photos are grayed from bottom up to about 1/4 height.

But this happens only when daylight is very bright (outside view). In mornings and evenings pictures are complete. Also when there is gray, dark midday.

Saku
  • 11
  • 3