【发布时间】:2022-02-01 11:44:16
【问题描述】:
如何将蓝牙输入流转换为 java android 应用程序中的图像? 数据被分成块。在输入流中逐块捕获,但它不能容纳超过一个块。这个想法是将所有块(逐个流)放在一个字节数组中,然后制作一个位图。
【问题讨论】:
标签: java android image bluetooth inputstream
如何将蓝牙输入流转换为 java android 应用程序中的图像? 数据被分成块。在输入流中逐块捕获,但它不能容纳超过一个块。这个想法是将所有块(逐个流)放在一个字节数组中,然后制作一个位图。
【问题讨论】:
标签: java android image bluetooth inputstream
private val mmInStream: InputStream = mmSocket.inputStream
private val mmOutStream: OutputStream = mmSocket.outputStream
private val mmBuffer: ByteArray = ByteArray(1024) // mmBuffer store for the stream
override fun run() {
var numBytes: Int // bytes returned from read()
// Keep listening to the InputStream until an exception occurs.
while (true) {
// Read from the InputStream.
numBytes = try {
mmInStream.read(mmBuffer)
} catch (e: IOException) {
Log.d(TAG, "Input stream was disconnected", e)
break
}
}
// Now the bytes read can be write to a file let's say .png/.jpeg image file.
fun write(bytes: ByteArray) {
try {
mmOutStream.write(bytes)
} catch (e: IOException) {
Log.e(TAG, "Error occurred when sending data", e)
}
}
}
【讨论】: