Ale*_*use 5 android image bluetooth kotlin esp32
第一次在这里提问。目前,我在将 ESP32 CAM 拍摄的照片发送到 Android Studio 应用程序时遇到问题。
虽然图片已收到,但大多数时候并不完整,或者显示一些灰色区域,如附图所示。
我注意到可用字节因图片而异,因此我绝望地尝试循环输出/输入流来拍照,直到可用字节超过 14000。仍然有大量可用字节,图片多次(并非总是如此) )显示一大块灰色像素。
我读到这可能是因为蓝牙套接字必须在“最后”异常中关闭,但我无法让它工作。
我非常感谢你的帮助!
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()
}
}
}
Run Code Online (Sandbox Code Playgroud)
显示收到照片中灰色区域的图片:
对于任何面临这个问题的人来说,我终于找到了问题/解决方案。
如果您使用 ESP32 库的“CameraWebServer”示例进行图片配置,您可能还需要以下参数:
if (psramFound()) {
Serial.println("psramFound() true");
config.frame_size = FRAMESIZE_UXGA;
config.jpeg_quality = 10;
config.fb_count = 2;
} else { ........
Run Code Online (Sandbox Code Playgroud)
然而,我发现通过将“fb_count”设置为 1 解决了我的问题。
我还必须将 jpeg_quality 更改为 20,您可以使用这个来找到最佳点以优化图像质量,该数字从 0 到 63,其中 0 是最佳质量。
| 归档时间: |
|
| 查看次数: |
761 次 |
| 最近记录: |