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: