发送部分编写完成
This commit is contained in:
		@@ -3,6 +3,8 @@
 | 
				
			|||||||
    xmlns:tools="http://schemas.android.com/tools">
 | 
					    xmlns:tools="http://schemas.android.com/tools">
 | 
				
			||||||
    <!-- For using the Camera -->
 | 
					    <!-- For using the Camera -->
 | 
				
			||||||
    <uses-permission android:name="android.permission.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" />
 | 
					    <uses-feature android:name="android.hardware.camera" />
 | 
				
			||||||
    <!-- For mediapipe -->
 | 
					    <!-- For mediapipe -->
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,6 +6,8 @@ import android.os.Bundle
 | 
				
			|||||||
import android.util.Log
 | 
					import android.util.Log
 | 
				
			||||||
import android.util.Size
 | 
					import android.util.Size
 | 
				
			||||||
import android.view.*
 | 
					import android.view.*
 | 
				
			||||||
 | 
					import android.widget.EditText
 | 
				
			||||||
 | 
					import android.widget.Toast
 | 
				
			||||||
import androidx.appcompat.widget.Toolbar
 | 
					import androidx.appcompat.widget.Toolbar
 | 
				
			||||||
import androidx.fragment.app.DialogFragment
 | 
					import androidx.fragment.app.DialogFragment
 | 
				
			||||||
import com.google.mediapipe.components.CameraHelper.CameraFacing
 | 
					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.framework.ProtoUtil
 | 
				
			||||||
import com.google.mediapipe.glutil.EglManager
 | 
					import com.google.mediapipe.glutil.EglManager
 | 
				
			||||||
import com.google.protobuf.InvalidProtocolBufferException
 | 
					import com.google.protobuf.InvalidProtocolBufferException
 | 
				
			||||||
 | 
					import top.rrricardo.motioncapture.models.PoseLandmark
 | 
				
			||||||
 | 
					import java.nio.ByteBuffer
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class MainActivity : AppCompatActivity(), SendDialogFragment.NoticeDialogListener {
 | 
					class MainActivity : AppCompatActivity(), SendDialogFragment.NoticeDialogListener {
 | 
				
			||||||
    // 一些在设置MediaPipe时会用到的字符串常量
 | 
					    // 一些在设置MediaPipe时会用到的字符串常量
 | 
				
			||||||
@@ -35,6 +39,8 @@ class MainActivity : AppCompatActivity(), SendDialogFragment.NoticeDialogListene
 | 
				
			|||||||
    lateinit var converter: ExternalTextureConverter
 | 
					    lateinit var converter: ExternalTextureConverter
 | 
				
			||||||
    lateinit var cameraHelper: CameraXPreviewHelper
 | 
					    lateinit var cameraHelper: CameraXPreviewHelper
 | 
				
			||||||
    private var cameraFacing = CameraFacing.BACK
 | 
					    private var cameraFacing = CameraFacing.BACK
 | 
				
			||||||
 | 
					    private var isSet = false
 | 
				
			||||||
 | 
					    private lateinit var sender: UdpSender
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private lateinit var toolbar: Toolbar
 | 
					    private lateinit var toolbar: Toolbar
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -96,13 +102,27 @@ class MainActivity : AppCompatActivity(), SendDialogFragment.NoticeDialogListene
 | 
				
			|||||||
        ) {
 | 
					        ) {
 | 
				
			||||||
            Log.i(tag, "Received Landmark Packets.")
 | 
					            Log.i(tag, "Received Landmark Packets.")
 | 
				
			||||||
            try {
 | 
					            try {
 | 
				
			||||||
 | 
					                Log.i(tag, "Receive Pose Packet.")
 | 
				
			||||||
                // 无法采用这种方法获取packet
 | 
					                // 无法采用这种方法获取packet
 | 
				
			||||||
                // 回报Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 15943 (Thread-9), pid 15810 (o.motioncapture)
 | 
					                // 回报Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 15943 (Thread-9), pid 15810 (o.motioncapture)
 | 
				
			||||||
                /*val packetRaw = PacketGetter.getBytes(it)
 | 
					                /*val packetRaw = PacketGetter.getBytes(it)
 | 
				
			||||||
                val landmarks = NormalizedLandmarkList.parseFrom(packetRaw)
 | 
					                val landmarks = NormalizedLandmarkList.parseFrom(packetRaw)
 | 
				
			||||||
                Log.i(tag, getPoseLandmarksDebugString(landmarks))*/
 | 
					                Log.i(tag, getPoseLandmarksDebugString(landmarks))*/
 | 
				
			||||||
                val landmarks = PacketGetter.getProto(it, NormalizedLandmarkList.getDefaultInstance())
 | 
					                val landmarks = PacketGetter.getProto(it,
 | 
				
			||||||
                Log.i(tag, getPoseLandmarksDebugString(landmarks))
 | 
					                    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) {
 | 
					            } catch (exception: InvalidProtocolBufferException) {
 | 
				
			||||||
                Log.e(tag, "failed to get protocol.", exception)
 | 
					                Log.e(tag, "failed to get protocol.", exception)
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
@@ -110,6 +130,11 @@ class MainActivity : AppCompatActivity(), SendDialogFragment.NoticeDialogListene
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        // 检查相机权限
 | 
					        // 检查相机权限
 | 
				
			||||||
        PermissionHelper.checkAndRequestCameraPermissions(this)
 | 
					        PermissionHelper.checkAndRequestCameraPermissions(this)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // 提示设置服务器
 | 
				
			||||||
 | 
					        if (!isSet) {
 | 
				
			||||||
 | 
					            showToastMessage("Server Unset!")
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    override fun onResume() {
 | 
					    override fun onResume() {
 | 
				
			||||||
@@ -132,6 +157,11 @@ class MainActivity : AppCompatActivity(), SendDialogFragment.NoticeDialogListene
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        converter.close()
 | 
					        converter.close()
 | 
				
			||||||
        previewDisplayView.visibility = View.GONE
 | 
					        previewDisplayView.visibility = View.GONE
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (isSet) {
 | 
				
			||||||
 | 
					            sender.close()
 | 
				
			||||||
 | 
					            isSet = false
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    override fun onRequestPermissionsResult(
 | 
					    override fun onRequestPermissionsResult(
 | 
				
			||||||
@@ -145,6 +175,23 @@ class MainActivity : AppCompatActivity(), SendDialogFragment.NoticeDialogListene
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    override fun onDialogPositiveClick(dialog: DialogFragment) {
 | 
					    override fun onDialogPositiveClick(dialog: DialogFragment) {
 | 
				
			||||||
        Log.i(tag, "Confirm Button Clicked.")
 | 
					        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) {
 | 
					    override fun onDialogNegativeClick(dialog: DialogFragment) {
 | 
				
			||||||
@@ -277,6 +324,12 @@ class MainActivity : AppCompatActivity(), SendDialogFragment.NoticeDialogListene
 | 
				
			|||||||
        dialogFragment.show(supportFragmentManager, "SetIpPortDialog")
 | 
					        dialogFragment.show(supportFragmentManager, "SetIpPortDialog")
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private fun showToastMessage(message: String) {
 | 
				
			||||||
 | 
					        val duration = Toast.LENGTH_SHORT
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Toast.makeText(this, message, duration).show()
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * 格式化输出捕捉的标志位置
 | 
					     * 格式化输出捕捉的标志位置
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,9 +10,6 @@ class PoseLandmark(
 | 
				
			|||||||
    private val Z: Float,
 | 
					    private val Z: Float,
 | 
				
			||||||
    private val Visibility: Float,
 | 
					    private val Visibility: Float,
 | 
				
			||||||
    private val TimeStamp: Long) {
 | 
					    private val TimeStamp: Long) {
 | 
				
			||||||
    private val tolerance = 10E-6;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private val packetLength = 28;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fun toByteArray(): ByteArray {
 | 
					    fun toByteArray(): ByteArray {
 | 
				
			||||||
        val result = ByteBuffer.allocate(packetLength)
 | 
					        val result = ByteBuffer.allocate(packetLength)
 | 
				
			||||||
@@ -27,6 +24,9 @@ class PoseLandmark(
 | 
				
			|||||||
        return result.array()
 | 
					        return result.array()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    companion object {
 | 
				
			||||||
 | 
					        const val packetLength = 28;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        fun valueOf(data: ByteArray): PoseLandmark {
 | 
					        fun valueOf(data: ByteArray): PoseLandmark {
 | 
				
			||||||
            val buffer = ByteBuffer.wrap(data)
 | 
					            val buffer = ByteBuffer.wrap(data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -58,6 +58,7 @@ class PoseLandmark(
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            return result
 | 
					            return result
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    override fun toString(): String {
 | 
					    override fun toString(): String {
 | 
				
			||||||
        val builder = StringBuilder()
 | 
					        val builder = StringBuilder()
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user