发送部分编写完成

This commit is contained in:
jackfiled 2023-01-30 20:48:54 +08:00
parent 780ba6b580
commit bf391ba7c0
3 changed files with 86 additions and 30 deletions

View File

@ -3,6 +3,8 @@
xmlns:tools="http://schemas.android.com/tools">
<!-- For using the Camera -->
<uses-permission android:name="android.permission.CAMERA" />
<!-- For sending data -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-feature android:name="android.hardware.camera" />
<!-- For mediapipe -->

View File

@ -6,6 +6,8 @@ import android.os.Bundle
import android.util.Log
import android.util.Size
import android.view.*
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.DialogFragment
import com.google.mediapipe.components.CameraHelper.CameraFacing
@ -19,6 +21,8 @@ import com.google.mediapipe.framework.PacketGetter
import com.google.mediapipe.framework.ProtoUtil
import com.google.mediapipe.glutil.EglManager
import com.google.protobuf.InvalidProtocolBufferException
import top.rrricardo.motioncapture.models.PoseLandmark
import java.nio.ByteBuffer
class MainActivity : AppCompatActivity(), SendDialogFragment.NoticeDialogListener {
// 一些在设置MediaPipe时会用到的字符串常量
@ -35,6 +39,8 @@ class MainActivity : AppCompatActivity(), SendDialogFragment.NoticeDialogListene
lateinit var converter: ExternalTextureConverter
lateinit var cameraHelper: CameraXPreviewHelper
private var cameraFacing = CameraFacing.BACK
private var isSet = false
private lateinit var sender: UdpSender
private lateinit var toolbar: Toolbar
@ -96,13 +102,27 @@ class MainActivity : AppCompatActivity(), SendDialogFragment.NoticeDialogListene
) {
Log.i(tag, "Received Landmark Packets.")
try {
Log.i(tag, "Receive Pose Packet.")
// 无法采用这种方法获取packet
// 回报Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 15943 (Thread-9), pid 15810 (o.motioncapture)
/*val packetRaw = PacketGetter.getBytes(it)
val landmarks = NormalizedLandmarkList.parseFrom(packetRaw)
Log.i(tag, getPoseLandmarksDebugString(landmarks))*/
val landmarks = PacketGetter.getProto(it, NormalizedLandmarkList.getDefaultInstance())
Log.i(tag, getPoseLandmarksDebugString(landmarks))
val landmarks = PacketGetter.getProto(it,
NormalizedLandmarkList.getDefaultInstance())
val poseLandmarks = PoseLandmark.valueOf(landmarks, it.timestamp)
val buffer = ByteBuffer.allocate(
poseLandmarks.size * PoseLandmark.packetLength)
for (poseLandmark in poseLandmarks) {
buffer.put(poseLandmark.toByteArray())
}
if (isSet) {
sender.sendMessage(buffer.array())
}
} catch (exception: InvalidProtocolBufferException) {
Log.e(tag, "failed to get protocol.", exception)
}
@ -110,6 +130,11 @@ class MainActivity : AppCompatActivity(), SendDialogFragment.NoticeDialogListene
// 检查相机权限
PermissionHelper.checkAndRequestCameraPermissions(this)
// 提示设置服务器
if (!isSet) {
showToastMessage("Server Unset!")
}
}
override fun onResume() {
@ -132,6 +157,11 @@ class MainActivity : AppCompatActivity(), SendDialogFragment.NoticeDialogListene
converter.close()
previewDisplayView.visibility = View.GONE
if (isSet) {
sender.close()
isSet = false
}
}
override fun onRequestPermissionsResult(
@ -145,6 +175,23 @@ class MainActivity : AppCompatActivity(), SendDialogFragment.NoticeDialogListene
override fun onDialogPositiveClick(dialog: DialogFragment) {
Log.i(tag, "Confirm Button Clicked.")
val ipInput = findViewById<EditText>(R.id.targetIP).text.toString()
val portInput = findViewById<EditText>(R.id.targetPort).text.toString()
try {
val port = portInput.toInt()
if (isSet) {
sender.close()
isSet = false
}
sender = UdpSender(ipInput, port)
isSet = true
showToastMessage("Server set successfully!")
} catch (e: java.lang.NumberFormatException) {
Log.e(tag, "Input error: $e")
}
}
override fun onDialogNegativeClick(dialog: DialogFragment) {
@ -277,6 +324,12 @@ class MainActivity : AppCompatActivity(), SendDialogFragment.NoticeDialogListene
dialogFragment.show(supportFragmentManager, "SetIpPortDialog")
}
private fun showToastMessage(message: String) {
val duration = Toast.LENGTH_SHORT
Toast.makeText(this, message, duration).show()
}
/**
* 格式化输出捕捉的标志位置

View File

@ -10,9 +10,6 @@ class PoseLandmark(
private val Z: Float,
private val Visibility: Float,
private val TimeStamp: Long) {
private val tolerance = 10E-6;
private val packetLength = 28;
fun toByteArray(): ByteArray {
val result = ByteBuffer.allocate(packetLength)
@ -27,36 +24,40 @@ class PoseLandmark(
return result.array()
}
fun valueOf(data: ByteArray): PoseLandmark {
val buffer = ByteBuffer.wrap(data)
companion object {
const val packetLength = 28;
val type = buffer.int
val x = buffer.float
val y = buffer.float
val z = buffer.float
val visibility = buffer.float
val timeStamp = buffer.long
fun valueOf(data: ByteArray): PoseLandmark {
val buffer = ByteBuffer.wrap(data)
return PoseLandmark(type, x, y, z, visibility, timeStamp)
}
val type = buffer.int
val x = buffer.float
val y = buffer.float
val z = buffer.float
val visibility = buffer.float
val timeStamp = buffer.long
fun valueOf(poseLandmarks: NormalizedLandmarkList, timeStamp: Long): Collection<PoseLandmark> {
val result = mutableSetOf<PoseLandmark>()
for ((landmarkIndex, landmark) in poseLandmarks.landmarkList.withIndex()) {
val poseLandmark = PoseLandmark(
landmarkIndex,
landmark.x,
landmark.y,
landmark.z,
landmark.visibility,
timeStamp
)
result.add(poseLandmark)
return PoseLandmark(type, x, y, z, visibility, timeStamp)
}
return result
fun valueOf(poseLandmarks: NormalizedLandmarkList, timeStamp: Long): Collection<PoseLandmark> {
val result = mutableSetOf<PoseLandmark>()
for ((landmarkIndex, landmark) in poseLandmarks.landmarkList.withIndex()) {
val poseLandmark = PoseLandmark(
landmarkIndex,
landmark.x,
landmark.y,
landmark.z,
landmark.visibility,
timeStamp
)
result.add(poseLandmark)
}
return result
}
}
override fun toString(): String {