"Lost bytes" 当通过蓝牙将图像从 ESP32 发送到 Android Studio 时
"Lost bytes" when sending image from ESP32 to Android Studio via Bluetooth
第一次在这里问。我目前遇到的问题是将 ESP32 CAM 拍摄的照片发送到我的 Android Studio 应用程序。
虽然收到了图片,但大部分时间都不完整,或者显示一些灰色区域,如附图所示。
我注意到可用字节因图片而异,所以在绝望的尝试中,我循环 Output/Input 流来拍照,直到可用字节超过 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()
}
}
}
显示收到照片中灰色区域的图片:
对于遇到此问题的任何人,我终于到了 problem/solution。
如果您使用 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 最好质量。
framebuffer 似乎不是解决方案。
我正在使用:
Cconfig.frame_size = FRAMESIZE_SVGA;
Cconfig.jpeg_quality = 12;
Cconfig.fb_count = 1;
而且照片从底部到大约 1/4 高度都是灰色的。
但这只发生在日光非常明亮的时候(外景)。在早上和晚上,照片是完整的。还有灰色的时候,黑暗的正午。
第一次在这里问。我目前遇到的问题是将 ESP32 CAM 拍摄的照片发送到我的 Android Studio 应用程序。
虽然收到了图片,但大部分时间都不完整,或者显示一些灰色区域,如附图所示。
我注意到可用字节因图片而异,所以在绝望的尝试中,我循环 Output/Input 流来拍照,直到可用字节超过 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()
}
}
}
显示收到照片中灰色区域的图片:
对于遇到此问题的任何人,我终于到了 problem/solution。
如果您使用 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 最好质量。
framebuffer 似乎不是解决方案。 我正在使用:
Cconfig.frame_size = FRAMESIZE_SVGA;
Cconfig.jpeg_quality = 12;
Cconfig.fb_count = 1;
而且照片从底部到大约 1/4 高度都是灰色的。
但这只发生在日光非常明亮的时候(外景)。在早上和晚上,照片是完整的。还有灰色的时候,黑暗的正午。