Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9818dfc438 | ||
|
|
cb9d68d2e3 | ||
|
|
b4087e435e | ||
|
|
4abc855b36 | ||
|
|
c898cbb3e2 | ||
|
|
0a09814a61 | ||
|
|
cc2fd2d743 | ||
|
|
cd883af92f | ||
|
|
9c60049843 | ||
|
|
e47b362883 | ||
|
|
ed6c5e83be | ||
|
|
a27cc5f7ee | ||
|
|
dd51448aea | ||
|
|
b62738aa15 | ||
|
|
7a60f4b34d | ||
|
|
c23aedf464 | ||
|
|
795e685a3a | ||
|
|
d9b725cb63 | ||
|
|
bb63b47b97 | ||
|
|
caefbd0944 | ||
|
|
d48f41e917 | ||
|
|
18fbdc9875 | ||
|
|
f055441340 | ||
|
|
e93550a24b | ||
|
|
fde2cd0544 | ||
|
|
fc326a8f42 | ||
|
|
4086f9c0b7 | ||
|
|
4a29b3adc9 | ||
|
|
78a682b664 | ||
|
|
2e20e7deab |
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="CompilerConfiguration">
|
||||||
|
<bytecodeTargetLevel target="17" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
@@ -4,10 +4,10 @@
|
|||||||
<selectionStates>
|
<selectionStates>
|
||||||
<SelectionState runConfigName="app">
|
<SelectionState runConfigName="app">
|
||||||
<option name="selectionMode" value="DROPDOWN" />
|
<option name="selectionMode" value="DROPDOWN" />
|
||||||
<DropdownSelection timestamp="2025-08-05T03:50:06.075639Z">
|
<DropdownSelection timestamp="2025-11-07T10:55:54.677927600Z">
|
||||||
<Target type="DEFAULT_BOOT">
|
<Target type="DEFAULT_BOOT">
|
||||||
<handle>
|
<handle>
|
||||||
<DeviceId pluginId="PhysicalDevice" identifier="serial=2FD2511004054749" />
|
<DeviceId pluginId="Default" identifier="serial=192.168.1.56:5555;connection=2687c52a" />
|
||||||
</handle>
|
</handle>
|
||||||
</Target>
|
</Target>
|
||||||
</DropdownSelection>
|
</DropdownSelection>
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
<GradleProjectSettings>
|
<GradleProjectSettings>
|
||||||
<option name="testRunner" value="CHOOSE_PER_TEST" />
|
<option name="testRunner" value="CHOOSE_PER_TEST" />
|
||||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||||
<option name="gradleJvm" value="#GRADLE_LOCAL_JAVA_HOME" />
|
<option name="gradleJvm" value="azul-17" />
|
||||||
<option name="modules">
|
<option name="modules">
|
||||||
<set>
|
<set>
|
||||||
<option value="$PROJECT_DIR$" />
|
<option value="$PROJECT_DIR$" />
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="jbr-21" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="zulu-17" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/build/classes" />
|
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectType">
|
<component name="ProjectType">
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
import org.gradle.kotlin.dsl.implementation
|
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
alias(libs.plugins.android.application)
|
// alias(libs.plugins.android.application)
|
||||||
alias(libs.plugins.kotlin.android)
|
// alias(libs.plugins.kotlin.android)
|
||||||
id("com.google.devtools.ksp")
|
id("com.android.application")
|
||||||
|
id("org.jetbrains.kotlin.android")
|
||||||
id("com.google.dagger.hilt.android")
|
id("com.google.dagger.hilt.android")
|
||||||
|
id("com.google.devtools.ksp")
|
||||||
id("kotlin-parcelize")
|
id("kotlin-parcelize")
|
||||||
|
kotlin("kapt")
|
||||||
}
|
}
|
||||||
|
|
||||||
android {
|
android {
|
||||||
@@ -31,6 +32,7 @@ android {
|
|||||||
ndk {
|
ndk {
|
||||||
abiFilters.addAll(listOf("armeabi-v7a"/*, "arm64-v8a"*/))
|
abiFilters.addAll(listOf("armeabi-v7a"/*, "arm64-v8a"*/))
|
||||||
}
|
}
|
||||||
|
setProperty("archivesBaseName", "档口双屏_${versionName}")
|
||||||
}
|
}
|
||||||
|
|
||||||
buildTypes {
|
buildTypes {
|
||||||
@@ -55,14 +57,7 @@ android {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
// implementation(
|
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.aar", "*.jar"))))
|
||||||
// fileTree(
|
|
||||||
// mapOf(
|
|
||||||
// "dir" to "libs",
|
|
||||||
// "include" to listOf("*.aar", "*.jar")
|
|
||||||
// )
|
|
||||||
// )
|
|
||||||
// )
|
|
||||||
implementation(libs.androidx.core.ktx)
|
implementation(libs.androidx.core.ktx)
|
||||||
implementation(libs.androidx.appcompat)
|
implementation(libs.androidx.appcompat)
|
||||||
implementation(libs.material)
|
implementation(libs.material)
|
||||||
@@ -109,4 +104,16 @@ dependencies {
|
|||||||
|
|
||||||
implementation(libs.core)
|
implementation(libs.core)
|
||||||
implementation(libs.android.core)
|
implementation(libs.android.core)
|
||||||
|
|
||||||
|
implementation(libs.pytorch.android)
|
||||||
|
implementation(libs.pytorch.android.torchvision)
|
||||||
|
|
||||||
|
val objectboxVersion = "5.0.1"
|
||||||
|
debugImplementation("io.objectbox:objectbox-android-objectbrowser:$objectboxVersion")
|
||||||
|
// releaseImplementation("io.objectbox:objectbox-android:$objectboxVersion")
|
||||||
|
|
||||||
|
implementation(libs.androidx.recyclerview)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
apply(plugin = "io.objectbox")
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
{
|
||||||
|
"_note1": "KEEP THIS FILE! Check it into a version control system (VCS) like git.",
|
||||||
|
"_note2": "ObjectBox manages crucial IDs for your object model. See docs for details.",
|
||||||
|
"_note3": "If you have VCS merge conflicts, you must resolve them according to ObjectBox docs.",
|
||||||
|
"entities": [
|
||||||
|
{
|
||||||
|
"id": "1:594331511073099531",
|
||||||
|
"lastPropertyId": "4:1528837175750321569",
|
||||||
|
"name": "Food",
|
||||||
|
"properties": [
|
||||||
|
{
|
||||||
|
"id": "1:2317727855243736226",
|
||||||
|
"name": "id",
|
||||||
|
"type": 6,
|
||||||
|
"flags": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "2:340951913928211738",
|
||||||
|
"name": "name",
|
||||||
|
"type": 9
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "3:5484846939472684412",
|
||||||
|
"name": "foodIdx",
|
||||||
|
"type": 5
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "4:1528837175750321569",
|
||||||
|
"name": "foodVector",
|
||||||
|
"indexId": "1:6010838397086628487",
|
||||||
|
"type": 28,
|
||||||
|
"flags": 8
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"relations": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"lastEntityId": "1:594331511073099531",
|
||||||
|
"lastIndexId": "1:6010838397086628487",
|
||||||
|
"lastRelationId": "0:0",
|
||||||
|
"lastSequenceId": "0:0",
|
||||||
|
"modelVersion": 5,
|
||||||
|
"modelVersionParserMinimum": 5,
|
||||||
|
"retiredEntityUids": [],
|
||||||
|
"retiredIndexUids": [],
|
||||||
|
"retiredPropertyUids": [],
|
||||||
|
"retiredRelationUids": [],
|
||||||
|
"version": 1
|
||||||
|
}
|
||||||
@@ -14,6 +14,7 @@
|
|||||||
<uses-permission
|
<uses-permission
|
||||||
android:name="android.permission.READ_EXTERNAL_STORAGE"
|
android:name="android.permission.READ_EXTERNAL_STORAGE"
|
||||||
android:maxSdkVersion="32" />
|
android:maxSdkVersion="32" />
|
||||||
|
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:name=".MyApp"
|
android:name=".MyApp"
|
||||||
@@ -25,8 +26,18 @@
|
|||||||
android:roundIcon="@mipmap/ic_launcher_round"
|
android:roundIcon="@mipmap/ic_launcher_round"
|
||||||
android:supportsRtl="true"
|
android:supportsRtl="true"
|
||||||
android:theme="@style/Theme.DualScreen"
|
android:theme="@style/Theme.DualScreen"
|
||||||
|
android:networkSecurityConfig="@xml/network_security_config"
|
||||||
android:usesCleartextTraffic="true"
|
android:usesCleartextTraffic="true"
|
||||||
tools:targetApi="31">
|
tools:targetApi="31">
|
||||||
|
<!-- 注册BootReceiver,监听开机完成广播 -->
|
||||||
|
<receiver
|
||||||
|
android:name=".receiver.BootReceiver"
|
||||||
|
android:enabled="true"
|
||||||
|
android:exported="true">
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.intent.action.BOOT_COMPLETED" />
|
||||||
|
</intent-filter>
|
||||||
|
</receiver>
|
||||||
<activity
|
<activity
|
||||||
android:name=".activity.InitActivity"
|
android:name=".activity.InitActivity"
|
||||||
android:exported="true">
|
android:exported="true">
|
||||||
@@ -40,6 +51,22 @@
|
|||||||
android:name=".activity.MainActivity"
|
android:name=".activity.MainActivity"
|
||||||
android:exported="true">
|
android:exported="true">
|
||||||
</activity>
|
</activity>
|
||||||
|
<activity
|
||||||
|
android:name=".activity.FoodCollectionActivity"
|
||||||
|
android:exported="true">
|
||||||
|
|
||||||
|
</activity>
|
||||||
|
<activity android:name="com.sw.dualscreen.activity.CollectedDataActivity" />
|
||||||
|
|
||||||
|
<provider
|
||||||
|
android:name="androidx.core.content.FileProvider"
|
||||||
|
android:authorities="${applicationId}.fileprovider"
|
||||||
|
android:exported="false"
|
||||||
|
android:grantUriPermissions="true">
|
||||||
|
<meta-data
|
||||||
|
android:name="android.support.FILE_PROVIDER_PATHS"
|
||||||
|
android:resource="@xml/file_paths" />
|
||||||
|
</provider>
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
||||||
|
After Width: | Height: | Size: 515 KiB |
@@ -0,0 +1,41 @@
|
|||||||
|
{
|
||||||
|
"class_names": [
|
||||||
|
"回锅肉",
|
||||||
|
"手撕包菜",
|
||||||
|
"炒细面",
|
||||||
|
"红烧肉",
|
||||||
|
"肉沫酸豆角",
|
||||||
|
"蒜苔炒肉",
|
||||||
|
"西红柿鸡蛋",
|
||||||
|
"豆角茄子",
|
||||||
|
"麻婆豆腐",
|
||||||
|
"麻辣小面"
|
||||||
|
],
|
||||||
|
"class_to_idx": {
|
||||||
|
"回锅肉": 0,
|
||||||
|
"手撕包菜": 1,
|
||||||
|
"炒细面": 2,
|
||||||
|
"红烧肉": 3,
|
||||||
|
"肉沫酸豆角": 4,
|
||||||
|
"蒜苔炒肉": 5,
|
||||||
|
"西红柿鸡蛋": 6,
|
||||||
|
"豆角茄子": 7,
|
||||||
|
"麻婆豆腐": 8,
|
||||||
|
"麻辣小面": 9
|
||||||
|
},
|
||||||
|
"idx_to_class": {
|
||||||
|
"0": "回锅肉",
|
||||||
|
"1": "手撕包菜",
|
||||||
|
"2": "炒细面",
|
||||||
|
"3": "红烧肉",
|
||||||
|
"4": "肉沫酸豆角",
|
||||||
|
"5": "蒜苔炒肉",
|
||||||
|
"6": "西红柿鸡蛋",
|
||||||
|
"7": "豆角茄子",
|
||||||
|
"8": "麻婆豆腐",
|
||||||
|
"9": "麻辣小面"
|
||||||
|
},
|
||||||
|
"embedding_dim": 512,
|
||||||
|
"total_images": 500,
|
||||||
|
"build_time": "2025-10-16T17:51:06.285784"
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
|
||||||
@@ -11,6 +11,7 @@ object GlobalData {
|
|||||||
* 设备id
|
* 设备id
|
||||||
*/
|
*/
|
||||||
var deviceId: String = ""
|
var deviceId: String = ""
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* app版本号
|
* app版本号
|
||||||
*/
|
*/
|
||||||
@@ -38,7 +39,7 @@ object GlobalData {
|
|||||||
|
|
||||||
var appId = "Hkz1rBk6PZXbS8KwKr67K2eZtsz8bRoMHLg64bUdgCZj"
|
var appId = "Hkz1rBk6PZXbS8KwKr67K2eZtsz8bRoMHLg64bUdgCZj"
|
||||||
var sdkKey = "AabXs3sHM8UhhE7oCGf4LVMLrdkGb1nJNksbjjTdVt7k"
|
var sdkKey = "AabXs3sHM8UhhE7oCGf4LVMLrdkGb1nJNksbjjTdVt7k"
|
||||||
var activeKey = "085F-118G-Q3AB-1WVJ"
|
var activeKey = "085F-118G-Q3LH-HPGZ"//2号楼"085F-118G-Q3AB-1WVJ" 5号楼"085F-118G-Q3LH-HPGZ"
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -59,4 +60,5 @@ object GlobalKey {
|
|||||||
const val KEY_TOKEN = "tokenKey"
|
const val KEY_TOKEN = "tokenKey"
|
||||||
const val KEY_FIRST_RUN = "firstRun"
|
const val KEY_FIRST_RUN = "firstRun"
|
||||||
const val KEY_USER_INFO = "userInfoKey"
|
const val KEY_USER_INFO = "userInfoKey"
|
||||||
|
const val KEY_PICKUP_MODE = "pickupMode"
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.sw.dualscreen
|
package com.sw.dualscreen
|
||||||
|
|
||||||
|
import com.sw.dualscreen.objbox.ObjectBox
|
||||||
|
import com.sw.dualscreen.utils.CrashHandler
|
||||||
import com.sw.plate.App
|
import com.sw.plate.App
|
||||||
import com.sw.plate.utils.AppUtil
|
import com.sw.plate.utils.AppUtil
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
@@ -7,14 +9,19 @@ import timber.log.Timber
|
|||||||
class MyApp : App() {
|
class MyApp : App() {
|
||||||
companion object {
|
companion object {
|
||||||
const val DEBUG: Boolean = true
|
const val DEBUG: Boolean = true
|
||||||
|
var instance: MyApp?=null
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
|
instance = this
|
||||||
Timber.plant(Timber.DebugTree())
|
Timber.plant(Timber.DebugTree())
|
||||||
var deviceId = AppUtil.getUDID(this)
|
var deviceId = AppUtil.getUDID(this)
|
||||||
Timber.d("UDID = ${AppUtil.getUDID(this)}")
|
Timber.d("UDID = ${AppUtil.getUDID(this)}")
|
||||||
deviceId = "3ea47dc0-3cf0-3c2f-909c-265a9a65572e"
|
deviceId = "3ea47dc0-3cf0-3c2f-909c-265a9a65572e"
|
||||||
GlobalData.deviceId = deviceId
|
GlobalData.deviceId = deviceId
|
||||||
|
ObjectBox.init(this)
|
||||||
|
// 初始化崩溃处理器
|
||||||
|
CrashHandler.init(this)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8,14 +8,14 @@ import android.view.View
|
|||||||
import android.view.WindowManager
|
import android.view.WindowManager
|
||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
import androidx.activity.enableEdgeToEdge
|
import androidx.activity.enableEdgeToEdge
|
||||||
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.core.view.WindowCompat
|
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import androidx.viewbinding.ViewBinding
|
import androidx.viewbinding.ViewBinding
|
||||||
import com.sw.dualscreen.R
|
import com.sw.dualscreen.R
|
||||||
|
import com.sw.dualscreen.model.IActivityResult
|
||||||
import com.sw.dualscreen.view.CustomDialog
|
import com.sw.dualscreen.view.CustomDialog
|
||||||
import com.sw.dualscreen.viewmodel.BaseViewModel
|
import com.sw.dualscreen.viewmodel.BaseViewModel
|
||||||
import kotlinx.coroutines.Job
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
|
||||||
@@ -81,4 +81,20 @@ abstract class BaseActivity<VB : ViewBinding> : AppCompatActivity() {
|
|||||||
mDialogWaiting?.dismiss()
|
mDialogWaiting?.dismiss()
|
||||||
mDialogWaiting = null
|
mDialogWaiting = null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private var launchPermissionCallback: IActivityResult.RequestPermissionCallback? = null
|
||||||
|
|
||||||
|
fun requestSinglePermissionResult(
|
||||||
|
permission: String,
|
||||||
|
permissionCallback: IActivityResult.RequestPermissionCallback?
|
||||||
|
) {
|
||||||
|
this.launchPermissionCallback = permissionCallback
|
||||||
|
permissionLauncher.launch(permission)
|
||||||
|
}
|
||||||
|
|
||||||
|
private val permissionLauncher = registerForActivityResult(
|
||||||
|
ActivityResultContracts.RequestPermission()
|
||||||
|
) { result: Boolean ->
|
||||||
|
launchPermissionCallback?.callback(result)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,134 @@
|
|||||||
|
package com.sw.dualscreen.activity
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
|
import androidx.activity.viewModels
|
||||||
|
import androidx.core.widget.addTextChangedListener
|
||||||
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
|
import com.sw.dualscreen.R
|
||||||
|
import com.sw.dualscreen.adapter.CollectedFoodAdapter
|
||||||
|
import com.sw.dualscreen.databinding.ActivityCollectedDataBinding
|
||||||
|
import com.sw.dualscreen.databinding.LayoutEmptySearchBinding
|
||||||
|
import com.sw.dualscreen.dialog.WarnDialog
|
||||||
|
import com.sw.dualscreen.ext.addOnActionSearchListener
|
||||||
|
import com.sw.dualscreen.ext.hideKeyboard
|
||||||
|
import com.sw.dualscreen.objbox.CollectedFoodBean
|
||||||
|
import com.sw.dualscreen.objbox.Food
|
||||||
|
import com.sw.dualscreen.objbox.ObjectBox
|
||||||
|
import com.sw.dualscreen.viewmodel.BaseViewModel
|
||||||
|
import com.sw.dualscreen.viewmodel.UserViewModel
|
||||||
|
import com.sw.plate.utils.ToastUtils
|
||||||
|
import io.objectbox.Box
|
||||||
|
import io.objectbox.kotlin.boxFor
|
||||||
|
import kotlin.getValue
|
||||||
|
|
||||||
|
class CollectedDataActivity : BaseActivity<ActivityCollectedDataBinding>() {
|
||||||
|
private val viewModel by viewModels<UserViewModel>()
|
||||||
|
override fun getViewModel(): BaseViewModel {
|
||||||
|
return viewModel
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun inflateViewBinding(): ActivityCollectedDataBinding {
|
||||||
|
return ActivityCollectedDataBinding.inflate(layoutInflater)
|
||||||
|
}
|
||||||
|
|
||||||
|
private val list: MutableList<CollectedFoodBean> = mutableListOf()
|
||||||
|
private val adapter by lazy {
|
||||||
|
CollectedFoodAdapter(list).apply {
|
||||||
|
isStateViewEnable = true
|
||||||
|
addOnItemChildClickListener(R.id.ivDeleteFood) { _, _, position ->
|
||||||
|
deleteGoods(position)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun initialize() {
|
||||||
|
super.initialize()
|
||||||
|
binding.rvFoodList.let {
|
||||||
|
it.layoutManager = LinearLayoutManager(this)
|
||||||
|
it.adapter = adapter
|
||||||
|
}
|
||||||
|
binding.ivBack.setOnClickListener { finish() }
|
||||||
|
binding.ivGoodsSearch.setOnClickListener {
|
||||||
|
val searchName = binding.etInputGoods.text.toString().trim()
|
||||||
|
if (searchName.isBlank()) {
|
||||||
|
ToastUtils.showToast("请输入物品名称")
|
||||||
|
return@setOnClickListener
|
||||||
|
}
|
||||||
|
getCollectGoods(searchName)
|
||||||
|
}
|
||||||
|
binding.etInputGoods.let { v ->
|
||||||
|
v.addOnActionSearchListener {
|
||||||
|
val searchName = binding.etInputGoods.text.toString().trim()
|
||||||
|
if (searchName.isBlank()) {
|
||||||
|
ToastUtils.showToast("请输入物品名称")
|
||||||
|
return@addOnActionSearchListener
|
||||||
|
}
|
||||||
|
getCollectGoods(searchName)
|
||||||
|
}
|
||||||
|
v.addTextChangedListener {
|
||||||
|
if (it.isNullOrBlank()) {
|
||||||
|
getCollectGoods()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
getCollectGoods()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private var box: Box<Food>? = null
|
||||||
|
|
||||||
|
@SuppressLint("NotifyDataSetChanged")
|
||||||
|
private fun getCollectGoods(searchName:String?=null) {
|
||||||
|
if (box == null) {
|
||||||
|
box = ObjectBox.boxStore.boxFor(Food::class)
|
||||||
|
}
|
||||||
|
list.clear()
|
||||||
|
var queryList = box?.all?.distinctBy { it.name }
|
||||||
|
if (searchName.isNullOrBlank().not()) {
|
||||||
|
queryList = queryList?.filter { it.name?.contains(searchName) == true }
|
||||||
|
}
|
||||||
|
queryList?.forEach {
|
||||||
|
list.add(CollectedFoodBean(foodName = it.name))
|
||||||
|
}
|
||||||
|
adapter.notifyDataSetChanged()
|
||||||
|
if (list.isEmpty()) {
|
||||||
|
loadEmptyView()
|
||||||
|
}
|
||||||
|
binding.root.hideKeyboard()
|
||||||
|
}
|
||||||
|
|
||||||
|
private var emptyBinding: LayoutEmptySearchBinding? = null
|
||||||
|
private fun loadEmptyView() {
|
||||||
|
if (emptyBinding == null) {
|
||||||
|
emptyBinding = LayoutEmptySearchBinding.inflate(layoutInflater, binding.rvFoodList, false)
|
||||||
|
}
|
||||||
|
emptyBinding?.root?.let { layout ->
|
||||||
|
layout.setOnClickListener { layout.hideKeyboard() }
|
||||||
|
adapter.stateView = layout
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun deleteGoods(position: Int) {
|
||||||
|
WarnDialog(
|
||||||
|
context = context,
|
||||||
|
content = "请确认是否删除菜品:${list[position].foodName}?",
|
||||||
|
confirmBlock = {
|
||||||
|
Thread {
|
||||||
|
runOnUiThread {
|
||||||
|
showWaitingDialog("加载中……")
|
||||||
|
}
|
||||||
|
val name = list[position].foodName
|
||||||
|
val filterIdList = box?.all?.filter { it.name == name }?.map { it.id }
|
||||||
|
box?.removeByIds(filterIdList)
|
||||||
|
runOnUiThread {
|
||||||
|
hideWaitingDialog()
|
||||||
|
adapter.removeAt(position)
|
||||||
|
ToastUtils.showToast("删除成功")
|
||||||
|
if (list.isEmpty()) {
|
||||||
|
loadEmptyView()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.start()
|
||||||
|
}).show()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,370 @@
|
|||||||
|
package com.sw.dualscreen.activity
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.graphics.Bitmap
|
||||||
|
import android.graphics.Typeface
|
||||||
|
import android.net.Uri
|
||||||
|
import android.view.View
|
||||||
|
import android.view.inputmethod.EditorInfo
|
||||||
|
import android.view.inputmethod.InputMethodManager
|
||||||
|
import android.widget.Toast
|
||||||
|
import androidx.activity.viewModels
|
||||||
|
import androidx.camera.view.PreviewView
|
||||||
|
import androidx.core.net.toUri
|
||||||
|
import androidx.core.view.updateLayoutParams
|
||||||
|
import androidx.lifecycle.lifecycleScope
|
||||||
|
import androidx.recyclerview.widget.GridLayoutManager
|
||||||
|
import com.example.utils.FloatBase64Utils
|
||||||
|
import com.sw.dualscreen.R
|
||||||
|
import com.sw.dualscreen.adapter.FoodCollectionAdapter
|
||||||
|
import com.sw.dualscreen.adapter.GenericItemAdapter
|
||||||
|
import com.sw.dualscreen.adapter.GridSpacingItemDecoration
|
||||||
|
import com.sw.dualscreen.adapter.dpToPx
|
||||||
|
import com.sw.dualscreen.databinding.ActivityFoodCollectionBinding
|
||||||
|
import com.sw.dualscreen.databinding.ItemSearchFoodInfoBinding
|
||||||
|
import com.sw.dualscreen.databinding.LayoutCameraPreviewBinding
|
||||||
|
import com.sw.dualscreen.ext.clickWithDebounce
|
||||||
|
import com.sw.dualscreen.ext.dp
|
||||||
|
import com.sw.dualscreen.model.response.FoodInfo
|
||||||
|
import com.sw.dualscreen.objbox.Food
|
||||||
|
import com.sw.dualscreen.objbox.FoodCollectionBean
|
||||||
|
import com.sw.dualscreen.objbox.FoodModule
|
||||||
|
import com.sw.dualscreen.objbox.ObjectBox
|
||||||
|
import com.sw.dualscreen.utils.BitmapCropper
|
||||||
|
import com.sw.dualscreen.utils.BitmapSaver
|
||||||
|
import com.sw.dualscreen.utils.CameraHelper
|
||||||
|
import com.sw.dualscreen.utils.CameraUtils
|
||||||
|
import com.sw.dualscreen.utils.Debouncer
|
||||||
|
import com.sw.dualscreen.utils.ImageUtil
|
||||||
|
import com.sw.dualscreen.viewmodel.BaseViewModel
|
||||||
|
import com.sw.dualscreen.viewmodel.UserViewModel
|
||||||
|
import com.sw.plate.utils.ToastUtils
|
||||||
|
import io.objectbox.Box
|
||||||
|
import io.objectbox.kotlin.boxFor
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import org.pytorch.IValue
|
||||||
|
import org.pytorch.Module
|
||||||
|
import org.pytorch.torchvision.TensorImageUtils
|
||||||
|
import timber.log.Timber
|
||||||
|
import kotlin.math.max
|
||||||
|
|
||||||
|
class FoodCollectionActivity : BaseActivity<ActivityFoodCollectionBinding>() {
|
||||||
|
companion object {
|
||||||
|
val MAX_COUNT = 100
|
||||||
|
}
|
||||||
|
private val viewModel by viewModels<UserViewModel>()
|
||||||
|
private var selectedFoodId: String? = ""
|
||||||
|
private var selectedFoodName: String? = ""
|
||||||
|
|
||||||
|
private var box: Box<Food>? = null
|
||||||
|
private val foodCollectionList: MutableList<FoodCollectionBean> = mutableListOf()
|
||||||
|
|
||||||
|
// private lateinit var cameraHelper: CameraHelper
|
||||||
|
|
||||||
|
private lateinit var foodAdapter: GenericItemAdapter<FoodInfo, ItemSearchFoodInfoBinding>
|
||||||
|
private val foodList = mutableListOf<FoodInfo>() // 适配器内部维护的数据列表
|
||||||
|
private val debouncer = Debouncer(2000)
|
||||||
|
private lateinit var previewView: PreviewView
|
||||||
|
|
||||||
|
private val cameraUtils: CameraUtils by lazy {
|
||||||
|
CameraUtils(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
private val collectionAdapter: FoodCollectionAdapter by lazy {
|
||||||
|
FoodCollectionAdapter(foodCollectionList).apply {
|
||||||
|
// setOnItemClickListener { _, _, position ->
|
||||||
|
// if (list[position].isShowCamera) {
|
||||||
|
//// takePhoto()
|
||||||
|
// cameraHelper.openCamera()
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
addOnItemChildClickListener(R.id.ivDelete) { _, _, position ->
|
||||||
|
// list.removeAt(position)
|
||||||
|
// collectionAdapter.notifyItemRemoved(position)
|
||||||
|
// collectionAdapter.notifyItemRangeChanged(position, list.size)
|
||||||
|
foodCollectionList[position].let {
|
||||||
|
it.bitmap = null
|
||||||
|
it.isShowCamera = true
|
||||||
|
it.isFinish = false
|
||||||
|
}
|
||||||
|
notifyItemChanged(position)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getViewModel(): BaseViewModel {
|
||||||
|
return viewModel
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun inflateViewBinding(): ActivityFoodCollectionBinding {
|
||||||
|
return ActivityFoodCollectionBinding.inflate(layoutInflater)
|
||||||
|
}
|
||||||
|
|
||||||
|
private val cameraCallback: (Uri) -> Unit = { uri ->
|
||||||
|
val index = foodCollectionList.indexOfFirst { it.bitmap == null }
|
||||||
|
if (index == -1) {
|
||||||
|
ToastUtils.showToast("每次只允许保存${MAX_COUNT}条数据")
|
||||||
|
rerurn@ cameraCallback
|
||||||
|
}
|
||||||
|
ImageUtil.uriToBitmap(this, uri)?.let { bitmap ->
|
||||||
|
// val cropBitmap = BitmapCropper.cropCenter(
|
||||||
|
// original = bitmap,
|
||||||
|
// targetWidth = 1300, targetHeight = 900,
|
||||||
|
// //offsetX = 30, offsetY = 100
|
||||||
|
// )
|
||||||
|
val file = BitmapSaver.saveToAppFilesDir(
|
||||||
|
bitmap, this, "IMG_CROP_${System.currentTimeMillis()}.jpg"
|
||||||
|
)
|
||||||
|
Timber.d("${this.javaClass.simpleName}-cameraCallback-裁剪bitmap保存文件路径:${file?.absolutePath}")
|
||||||
|
|
||||||
|
foodCollectionList[index].let {
|
||||||
|
it.bitmap = bitmap
|
||||||
|
it.isShowCamera = false
|
||||||
|
it.imageUri = file?.toUri()
|
||||||
|
}
|
||||||
|
collectionAdapter.notifyItemChanged(index)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressLint("NotifyDataSetChanged")
|
||||||
|
private fun takePhoto() {
|
||||||
|
val count = foodCollectionList.count { it.bitmap != null }
|
||||||
|
if (count >= MAX_COUNT) {
|
||||||
|
ToastUtils.showToast("每次只允许保存${MAX_COUNT}条数据")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cameraUtils.takePhoto(cameraCallback)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun initialize() {
|
||||||
|
cameraUtils.initCamera()
|
||||||
|
val previewBinding =
|
||||||
|
LayoutCameraPreviewBinding.inflate(layoutInflater, binding.flCameraPreview)
|
||||||
|
previewView = previewBinding.previewView.also {
|
||||||
|
it.updateLayoutParams {
|
||||||
|
width = 180.dp
|
||||||
|
height = 180.dp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cameraUtils.setPreviewController(previewView)
|
||||||
|
// 初始化 CameraHelper
|
||||||
|
// cameraHelper = CameraHelper(
|
||||||
|
// context = this,
|
||||||
|
// caller = this,
|
||||||
|
// authority = "${packageName}.fileprovider"
|
||||||
|
// ) { uri, path ->
|
||||||
|
// //if (foodCollectionList.size < 6) {
|
||||||
|
// // val insertIndex = if (foodCollectionList.isEmpty()) 0 else foodCollectionList.size - 1
|
||||||
|
// // foodCollectionList.add(insertIndex, FoodCollectionBean(imageUri = uri))
|
||||||
|
// //} else {
|
||||||
|
// // foodCollectionList[foodCollectionList.size - 1] = FoodCollectionBean(imageUri = uri)
|
||||||
|
// //}
|
||||||
|
// //collectionAdapter.notifyDataSetChanged()
|
||||||
|
// cameraCallback(uri)
|
||||||
|
// }
|
||||||
|
|
||||||
|
binding.ivBack.setOnClickListener {
|
||||||
|
val intent = Intent(this, MainActivity::class.java)
|
||||||
|
startActivity(intent)
|
||||||
|
finish()
|
||||||
|
}
|
||||||
|
repeat(MAX_COUNT) {
|
||||||
|
foodCollectionList.add(FoodCollectionBean(isShowCamera = true))
|
||||||
|
}
|
||||||
|
binding.rvFoodCollection.let {
|
||||||
|
it.layoutManager = GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false)
|
||||||
|
it.adapter = collectionAdapter
|
||||||
|
}
|
||||||
|
|
||||||
|
binding.btnFoodSearch.setOnClickListener {
|
||||||
|
searchInfo()
|
||||||
|
}
|
||||||
|
|
||||||
|
binding.btnSave.setOnClickListener {
|
||||||
|
if (selectedFoodName.isNullOrBlank()) {
|
||||||
|
Toast.makeText(this, "请选择菜品名称", Toast.LENGTH_SHORT).show()
|
||||||
|
return@setOnClickListener
|
||||||
|
}
|
||||||
|
if (foodCollectionList.size <= 1) {
|
||||||
|
Toast.makeText(this, "请拍摄菜品照片", Toast.LENGTH_SHORT).show()
|
||||||
|
return@setOnClickListener
|
||||||
|
}
|
||||||
|
vectorThread()
|
||||||
|
}
|
||||||
|
|
||||||
|
binding.editFoodName.setOnEditorActionListener { v, actionId, event ->
|
||||||
|
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
|
||||||
|
searchInfo()
|
||||||
|
val imm =
|
||||||
|
v.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
|
||||||
|
imm.hideSoftInputFromWindow(v.windowToken, 0)
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
binding.btnCollectedGoods.setOnClickListener {
|
||||||
|
startActivity(Intent(this, CollectedDataActivity::class.java))
|
||||||
|
}
|
||||||
|
binding.btnTakePhoto.clickWithDebounce {
|
||||||
|
binding.btnTakePhoto.text = "拍照"
|
||||||
|
val count = foodCollectionList.count { it.bitmap != null }
|
||||||
|
if (count >= MAX_COUNT) {
|
||||||
|
ToastUtils.showToast("每次只允许保存${MAX_COUNT}条数据")
|
||||||
|
return@clickWithDebounce
|
||||||
|
}
|
||||||
|
// cameraHelper.openCamera()
|
||||||
|
takePhoto()
|
||||||
|
}
|
||||||
|
binding.btnClearData.setOnClickListener { clearData() }
|
||||||
|
foodAdapter = createAdapter()
|
||||||
|
binding.recyclerview.layoutManager = GridLayoutManager(context, 2)
|
||||||
|
// 添加间距装饰(12dp)
|
||||||
|
binding.recyclerview.addItemDecoration(
|
||||||
|
GridSpacingItemDecoration(
|
||||||
|
spanCount = 2,
|
||||||
|
spacing = dpToPx(30),
|
||||||
|
includeEdge = false // 包含边缘间距
|
||||||
|
)
|
||||||
|
)
|
||||||
|
binding.recyclerview.adapter = foodAdapter
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun vectorThread() {
|
||||||
|
//Loading.show(this)
|
||||||
|
showWaitingDialog("加载中……")
|
||||||
|
Thread {
|
||||||
|
foodCollectionList
|
||||||
|
.filter { it.bitmap != null }
|
||||||
|
.forEachIndexed { index, it ->
|
||||||
|
image2VectorTask(it, index)
|
||||||
|
}
|
||||||
|
runOnUiThread {
|
||||||
|
window.decorView.postDelayed({
|
||||||
|
//Loading.dismiss()
|
||||||
|
hideWaitingDialog()
|
||||||
|
}, 1000)
|
||||||
|
}
|
||||||
|
}.start()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun initBox() {
|
||||||
|
if (box == null) {
|
||||||
|
box = ObjectBox.boxStore.boxFor(Food::class)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun image2VectorTask(item: FoodCollectionBean, position: Int) {
|
||||||
|
initBox()
|
||||||
|
val imageVector = FoodModule.bitmap2FloatArray(item.bitmap!!)
|
||||||
|
|
||||||
|
val base64Str = FloatBase64Utils.floatArrayToBase64(imageVector)
|
||||||
|
Timber.tag("mzf1").e(base64Str)
|
||||||
|
// viewModel.postImageData(
|
||||||
|
// context,
|
||||||
|
// foodId = selectedFoodId.toString(),
|
||||||
|
// foodName = selectedFoodName.toString(),
|
||||||
|
// foodVector = base64Str,
|
||||||
|
// uri = item.imageUri!!
|
||||||
|
// )
|
||||||
|
box?.put(Food(name = checkedItem!!.foodName, foodIdx = 0, foodVector = imageVector))
|
||||||
|
foodCollectionList[position].let {
|
||||||
|
it.imageVector = imageVector
|
||||||
|
it.isFinish = true
|
||||||
|
}
|
||||||
|
|
||||||
|
runOnUiThread {
|
||||||
|
collectionAdapter.notifyItemChanged(position)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private fun searchInfo() {
|
||||||
|
debouncer.debounce {
|
||||||
|
viewModel.searchByFoodName(binding.editFoodName.text.toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun registerDataChange() {
|
||||||
|
super.registerDataChange()
|
||||||
|
lifecycleScope.launch {
|
||||||
|
viewModel.searchFoodInfoList.collect {
|
||||||
|
// foodList.clear()
|
||||||
|
// foodList.addAll(it)
|
||||||
|
foodAdapter.updateData(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private var checkedItem: FoodInfo? = null
|
||||||
|
private fun createAdapter(): GenericItemAdapter<FoodInfo, ItemSearchFoodInfoBinding> {
|
||||||
|
return GenericItemAdapter(
|
||||||
|
items = emptyList(),
|
||||||
|
bindingInflater = ItemSearchFoodInfoBinding::inflate,
|
||||||
|
bindCallback = { item, position ->
|
||||||
|
this.tvName.text = item.foodName
|
||||||
|
if (item.id != checkedItem?.id) {
|
||||||
|
this.tvName.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
|
||||||
|
this.tvName.setTextColor(resources.getColor(R.color.search_normal))
|
||||||
|
this.llRoot.setBackgroundResource(R.drawable.grid_search_item_normal)
|
||||||
|
} else {
|
||||||
|
this.tvName.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
|
||||||
|
this.tvName.setTextColor(resources.getColor(R.color.search_checked))
|
||||||
|
this.llRoot.setBackgroundResource(R.drawable.grid_search_item_checked)
|
||||||
|
}
|
||||||
|
|
||||||
|
this.llRoot.setOnClickListener {
|
||||||
|
Timber.d("itemClick ${item.foodName}, position = $position")
|
||||||
|
checkedItem = item
|
||||||
|
// viewModel.updateCurrentItem(item)
|
||||||
|
// itemClickCallback(item)
|
||||||
|
selectedFoodName = item.foodName
|
||||||
|
selectedFoodId = item.id
|
||||||
|
|
||||||
|
foodAdapter.notifyDataSetChanged()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private var clickIndex = -1
|
||||||
|
|
||||||
|
@SuppressLint("NotifyDataSetChanged")
|
||||||
|
private fun clearData() {
|
||||||
|
foodCollectionList.forEach {
|
||||||
|
it.bitmap = null
|
||||||
|
it.isShowCamera = true
|
||||||
|
it.isFinish = false
|
||||||
|
}
|
||||||
|
collectionAdapter.notifyDataSetChanged()
|
||||||
|
|
||||||
|
clickIndex = -1
|
||||||
|
binding.editFoodName.setText("")
|
||||||
|
// foodList.clear()
|
||||||
|
// foodAdapter.updateData(mutableListOf())
|
||||||
|
// foodAdapter.notifyDataSetChanged()
|
||||||
|
//loadEmptyView()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onResume() {
|
||||||
|
super.onResume()
|
||||||
|
cameraUtils.bind()
|
||||||
|
binding.llCameraFlag.run {
|
||||||
|
visibility = View.VISIBLE
|
||||||
|
postDelayed({
|
||||||
|
visibility = View.GONE
|
||||||
|
}, 3000)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onPause() {
|
||||||
|
super.onPause()
|
||||||
|
cameraUtils.unbind()
|
||||||
|
binding.llCameraFlag.visibility = View.VISIBLE
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,19 +1,18 @@
|
|||||||
package com.sw.dualscreen.activity
|
package com.sw.dualscreen.activity
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
import android.graphics.Outline
|
import android.graphics.Outline
|
||||||
import android.graphics.Typeface
|
import android.graphics.Typeface
|
||||||
import android.hardware.display.DisplayManager
|
import android.hardware.display.DisplayManager
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Bundle
|
|
||||||
import android.text.TextUtils
|
import android.text.TextUtils
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewOutlineProvider
|
import android.view.ViewOutlineProvider
|
||||||
import android.widget.AdapterView
|
import android.widget.AdapterView
|
||||||
import android.widget.ArrayAdapter
|
import android.widget.ArrayAdapter
|
||||||
import androidx.activity.enableEdgeToEdge
|
import android.widget.Toast
|
||||||
import androidx.activity.viewModels
|
import androidx.activity.viewModels
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
|
||||||
import androidx.camera.core.CameraSelector
|
import androidx.camera.core.CameraSelector
|
||||||
import androidx.camera.core.ImageAnalysis
|
import androidx.camera.core.ImageAnalysis
|
||||||
import androidx.camera.core.ImageCapture
|
import androidx.camera.core.ImageCapture
|
||||||
@@ -24,6 +23,8 @@ import androidx.core.content.ContextCompat
|
|||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import androidx.recyclerview.widget.GridLayoutManager
|
import androidx.recyclerview.widget.GridLayoutManager
|
||||||
import com.google.common.util.concurrent.ListenableFuture
|
import com.google.common.util.concurrent.ListenableFuture
|
||||||
|
import com.sw.dualscreen.GlobalData
|
||||||
|
import com.sw.dualscreen.GlobalKey
|
||||||
import com.sw.dualscreen.R
|
import com.sw.dualscreen.R
|
||||||
import com.sw.dualscreen.adapter.GenericItemAdapter
|
import com.sw.dualscreen.adapter.GenericItemAdapter
|
||||||
import com.sw.dualscreen.adapter.GridSpacingItemDecoration
|
import com.sw.dualscreen.adapter.GridSpacingItemDecoration
|
||||||
@@ -31,26 +32,40 @@ import com.sw.dualscreen.adapter.dpToPx
|
|||||||
import com.sw.dualscreen.databinding.ActivityMainBinding
|
import com.sw.dualscreen.databinding.ActivityMainBinding
|
||||||
import com.sw.dualscreen.databinding.ItemFoodInfoBinding
|
import com.sw.dualscreen.databinding.ItemFoodInfoBinding
|
||||||
import com.sw.dualscreen.ext.dp
|
import com.sw.dualscreen.ext.dp
|
||||||
|
import com.sw.dualscreen.ext.format2String
|
||||||
import com.sw.dualscreen.model.response.FoodInfo
|
import com.sw.dualscreen.model.response.FoodInfo
|
||||||
|
import com.sw.dualscreen.objbox.FoodModule
|
||||||
|
import com.sw.dualscreen.objbox.FoodModule.IdNameScore
|
||||||
import com.sw.dualscreen.presentation.SecondaryScreenPresentation
|
import com.sw.dualscreen.presentation.SecondaryScreenPresentation
|
||||||
import com.sw.dualscreen.sdk.SensorScaleUtils
|
import com.sw.dualscreen.sdk.SensorScaleUtils
|
||||||
|
import com.sw.dualscreen.utils.AssetsTool
|
||||||
|
import com.sw.dualscreen.utils.BitmapCropper
|
||||||
|
import com.sw.dualscreen.utils.BitmapSaver
|
||||||
|
import com.sw.dualscreen.utils.Debouncer
|
||||||
import com.sw.dualscreen.utils.GlideUtils
|
import com.sw.dualscreen.utils.GlideUtils
|
||||||
|
import com.sw.dualscreen.utils.GsonUtils
|
||||||
|
import com.sw.dualscreen.utils.ImageUtil
|
||||||
|
import com.sw.dualscreen.utils.SPUtil
|
||||||
import com.sw.dualscreen.view.CustomBottomSheetDialog
|
import com.sw.dualscreen.view.CustomBottomSheetDialog
|
||||||
import com.sw.dualscreen.view.CustomDialog
|
|
||||||
import com.sw.dualscreen.viewmodel.BaseViewModel
|
import com.sw.dualscreen.viewmodel.BaseViewModel
|
||||||
import com.sw.dualscreen.viewmodel.UserViewModel
|
import com.sw.dualscreen.viewmodel.UserViewModel
|
||||||
|
import com.sw.plate.utils.LightManager
|
||||||
import com.sw.plate.utils.ToastUtils
|
import com.sw.plate.utils.ToastUtils
|
||||||
import com.sw.plate.utils.arcface.viewmodel.RecognizeViewModel
|
import com.sw.plate.utils.arcface.viewmodel.RecognizeViewModel
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.util.concurrent.Executors
|
import java.util.concurrent.Executors
|
||||||
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 服务员显示界面
|
||||||
|
*/
|
||||||
class MainActivity : BaseActivity<ActivityMainBinding>() {
|
class MainActivity : BaseActivity<ActivityMainBinding>() {
|
||||||
private val viewModel by viewModels<UserViewModel>()
|
private val viewModel by viewModels<UserViewModel>()
|
||||||
private var imageCapture: ImageCapture? = null
|
private var imageCapture: ImageCapture? = null
|
||||||
private var isAnalyzing = true // 控制是否进行图像分析
|
private var isAnalyzing = true // 控制是否进行图像分析
|
||||||
private lateinit var presentation: SecondaryScreenPresentation
|
private var presentation: SecondaryScreenPresentation? = null
|
||||||
private val executor = Executors.newSingleThreadExecutor()
|
private val executor = Executors.newSingleThreadExecutor()
|
||||||
private lateinit var adapter: GenericItemAdapter<FoodInfo, ItemFoodInfoBinding>
|
private lateinit var adapter: GenericItemAdapter<FoodInfo, ItemFoodInfoBinding>
|
||||||
private var checkedItem: FoodInfo? = null
|
private var checkedItem: FoodInfo? = null
|
||||||
@@ -59,6 +74,9 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
|||||||
private var canIdentify: Boolean = false
|
private var canIdentify: Boolean = false
|
||||||
private var bottomSheetDialog: CustomBottomSheetDialog? = null
|
private var bottomSheetDialog: CustomBottomSheetDialog? = null
|
||||||
private var lastWeight: Double = 0.0
|
private var lastWeight: Double = 0.0
|
||||||
|
private var lastPhotoUri: Uri? = null // 最后拍照的图片
|
||||||
|
private var debouncer = Debouncer(2000)
|
||||||
|
private var isRecognitionFood = true
|
||||||
|
|
||||||
override fun getViewModel(): BaseViewModel {
|
override fun getViewModel(): BaseViewModel {
|
||||||
return viewModel
|
return viewModel
|
||||||
@@ -74,15 +92,21 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
|||||||
setupSecondaryDisplay()
|
setupSecondaryDisplay()
|
||||||
setupCamera()
|
setupCamera()
|
||||||
initData()
|
initData()
|
||||||
|
FoodModule.init(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun initData() {
|
private fun initData() {
|
||||||
viewModel.getEquipmentToken()
|
viewModel.getEquipmentToken()
|
||||||
presentation.step1()
|
presentation?.step1()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun initView() {
|
private fun initView() {
|
||||||
viewModel.activeEngine()
|
viewModel.activeEngine()
|
||||||
|
binding.foodDisplayLayout.setOnClickListener {
|
||||||
|
val intent = Intent(this, FoodCollectionActivity::class.java)
|
||||||
|
startActivity(intent)
|
||||||
|
finish()
|
||||||
|
}
|
||||||
binding.previewView.outlineProvider = object : ViewOutlineProvider() {
|
binding.previewView.outlineProvider = object : ViewOutlineProvider() {
|
||||||
override fun getOutline(view: View, outline: Outline) {
|
override fun getOutline(view: View, outline: Outline) {
|
||||||
outline.setRoundRect(0, 0, view.width, view.height, 12f.dp)
|
outline.setRoundRect(0, 0, view.width, view.height, 12f.dp)
|
||||||
@@ -91,6 +115,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
|||||||
binding.previewView.clipToOutline = true
|
binding.previewView.clipToOutline = true
|
||||||
binding.tvToSearch.setOnClickListener {
|
binding.tvToSearch.setOnClickListener {
|
||||||
bottomSheetDialog = CustomBottomSheetDialog.newInstance(viewModel) {
|
bottomSheetDialog = CustomBottomSheetDialog.newInstance(viewModel) {
|
||||||
|
it.photoUri = null
|
||||||
updateCurrentFood(it)
|
updateCurrentFood(it)
|
||||||
}
|
}
|
||||||
bottomSheetDialog!!.show(supportFragmentManager, "CustomBottomSheetDialog")
|
bottomSheetDialog!!.show(supportFragmentManager, "CustomBottomSheetDialog")
|
||||||
@@ -102,7 +127,9 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
|||||||
arrayOf("即放即取", "余量取餐")
|
arrayOf("即放即取", "余量取餐")
|
||||||
)
|
)
|
||||||
binding.spinner.adapter = spinnerAdapter
|
binding.spinner.adapter = spinnerAdapter
|
||||||
binding.spinner.setSelection(0)
|
val mode = SPUtil.getInstance().get(GlobalKey.KEY_PICKUP_MODE, 0)
|
||||||
|
binding.spinner.setSelection(mode ?: 0)
|
||||||
|
presentation?.updateMealPickupMode(mode ?: 0)
|
||||||
binding.spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
|
binding.spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
|
||||||
override fun onItemSelected(
|
override fun onItemSelected(
|
||||||
parent: AdapterView<*>?,
|
parent: AdapterView<*>?,
|
||||||
@@ -110,7 +137,12 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
|||||||
position: Int,
|
position: Int,
|
||||||
id: Long
|
id: Long
|
||||||
) {
|
) {
|
||||||
presentation.updateMealPickupMode(position)
|
SPUtil.getInstance().put(GlobalKey.KEY_PICKUP_MODE, position)
|
||||||
|
updateCurrentFood(null)
|
||||||
|
isRecognitionFood = true
|
||||||
|
presentation?.updateMealPickupMode(position)
|
||||||
|
|
||||||
|
SensorScaleUtils.zero()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onNothingSelected(parent: AdapterView<*>?) {}
|
override fun onNothingSelected(parent: AdapterView<*>?) {}
|
||||||
@@ -129,11 +161,14 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
|||||||
binding.tvFoodName.setOnClickListener {
|
binding.tvFoodName.setOnClickListener {
|
||||||
SensorScaleUtils.zero()
|
SensorScaleUtils.zero()
|
||||||
}
|
}
|
||||||
|
binding.tvRescan.setOnClickListener {
|
||||||
|
debouncer.debounce { recognizeFood() }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun updateCurrentFood(foodInfo: FoodInfo?) {
|
fun updateCurrentFood(foodInfo: FoodInfo?) {
|
||||||
if (foodInfo != null) {
|
if (foodInfo != null) {
|
||||||
presentation.updateFood(foodInfo)
|
presentation?.updateFood(foodInfo)
|
||||||
binding.tvFoodName.text = foodInfo.foodName
|
binding.tvFoodName.text = foodInfo.foodName
|
||||||
binding.previewView.visibility = View.GONE
|
binding.previewView.visibility = View.GONE
|
||||||
binding.ivImg.visibility = View.VISIBLE
|
binding.ivImg.visibility = View.VISIBLE
|
||||||
@@ -141,11 +176,15 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
|||||||
if (TextUtils.isEmpty(imgUrl)) {
|
if (TextUtils.isEmpty(imgUrl)) {
|
||||||
takePhoto { imgUri ->
|
takePhoto { imgUri ->
|
||||||
GlideUtils.loadRoundCornerImage(this, imgUri, binding.ivImg, 12)
|
GlideUtils.loadRoundCornerImage(this, imgUri, binding.ivImg, 12)
|
||||||
presentation.updateFood(foodInfo.copy(imgUrl = imgUri.toString()))
|
presentation?.updateFood(foodInfo.copy(imgUrl = imgUri.toString()))
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if (foodInfo.photoUri != null) {
|
||||||
|
GlideUtils.loadRoundCornerImage(this, foodInfo.photoUri, binding.ivImg, 12)
|
||||||
} else {
|
} else {
|
||||||
GlideUtils.loadRoundCornerImage(this, foodInfo.imgUrl, binding.ivImg, 12)
|
GlideUtils.loadRoundCornerImage(this, foodInfo.imgUrl, binding.ivImg, 12)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
pauseAnalysis()
|
pauseAnalysis()
|
||||||
} else {
|
} else {
|
||||||
binding.tvFoodName.text = "-"
|
binding.tvFoodName.text = "-"
|
||||||
@@ -158,22 +197,104 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
|||||||
|
|
||||||
override fun registerDataChange() {
|
override fun registerDataChange() {
|
||||||
super.registerDataChange()
|
super.registerDataChange()
|
||||||
|
// lifecycleScope.launch {
|
||||||
|
// viewModel.identifiedFoodInfoList2.collect { list ->
|
||||||
|
// updateFoodInfo(list)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch {
|
||||||
viewModel.identifiedFoodInfoList.collect { list ->
|
viewModel.identifiedFoodInfoList.collect { list ->
|
||||||
binding.recyclerview
|
|
||||||
adapter.updateData(list)
|
adapter.updateData(list)
|
||||||
// presentation.step2()
|
if (list.isNotEmpty()) {
|
||||||
|
checkedItem = list[0]
|
||||||
|
checkedItem!!.photoUri = lastPhotoUri
|
||||||
|
updateCurrentFood(checkedItem)
|
||||||
|
lastPhotoUri = null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SensorScaleUtils.startScale { weight ->
|
SensorScaleUtils.startScale { weight ->
|
||||||
Timber.d("registerDataChange weight = $weight")
|
Timber.d("registerDataChange weight = $weight")
|
||||||
presentation.updateWeight(weight)
|
presentation?.updateWeight(weight)
|
||||||
if (weight > 0.0) {
|
|
||||||
if (canIdentify) {
|
if (weight <= 0.005) {//余量取餐,检测到秤上没有东西,重新启动识别菜品 && presentation?.mealPickupMode == 1
|
||||||
canIdentify = false
|
isRecognitionFood = true
|
||||||
viewModel.getIdentifiedFoodList()
|
}
|
||||||
|
|
||||||
|
if (weight - lastWeight > 0.05 && isRecognitionFood) { // 大于50g
|
||||||
|
if (presentation?.mealPickupMode == 1) {
|
||||||
|
isRecognitionFood = false
|
||||||
|
}
|
||||||
|
debouncer.debounce {
|
||||||
|
recognizeFood()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
lastWeight = weight
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun recognizeFood() {
|
||||||
|
LightManager.openRedLight()
|
||||||
|
LightManager.openGreenLight()
|
||||||
|
takePhoto { photoUri ->
|
||||||
|
lastPhotoUri = photoUri
|
||||||
|
Timber.d("registerDataChange photoUri = ${photoUri.path}")
|
||||||
|
// viewModel.getIdentifiedFoodList()
|
||||||
|
|
||||||
|
ImageUtil.uriToBitmap(this, photoUri)?.let { bitmap ->
|
||||||
|
Timber.d("registerDataChange photoUri 拿到bitmap")
|
||||||
|
// val bmp = BitmapCropper.cropCenter(bitmap, 1300, 900)
|
||||||
|
Timber.d("registerDataChange photoUri bitmap裁剪完成")
|
||||||
|
val file = BitmapSaver.saveToAppFilesDir(
|
||||||
|
bitmap,
|
||||||
|
this,
|
||||||
|
"IMG_CROP_${System.currentTimeMillis()}.jpg"
|
||||||
|
)
|
||||||
|
Timber.d("registerDataChange photoUri bitmap保存文件路径:${file?.absolutePath}")
|
||||||
|
//val nameList = FoodModule.queryFood(bitmap)
|
||||||
|
val scoreList = FoodModule.getFoodScoreList(bitmap)
|
||||||
|
Timber.d(
|
||||||
|
"registerDataChange photoUri 拿到识别数据:${
|
||||||
|
GsonUtils.toJson(
|
||||||
|
scoreList
|
||||||
|
)
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
val nameList = scoreList.map { it.name }
|
||||||
|
val foodName = nameList.joinToString(separator = ",")
|
||||||
|
|
||||||
|
Timber.d("registerDataChange photoUri 识别数据名称:$foodName")
|
||||||
|
if (TextUtils.isEmpty(foodName)) {
|
||||||
|
LightManager.closeGreenLight()
|
||||||
|
LightManager.closeRedLight()
|
||||||
|
binding.layoutRescan.visibility = View.VISIBLE
|
||||||
|
} else {
|
||||||
|
binding.layoutRescan.visibility = View.GONE
|
||||||
|
viewModel.getFoodInfo(foodName) { list ->
|
||||||
|
runOnUiThread {
|
||||||
|
updateFoodInfo(list, scoreList)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun updateFoodInfo(list: List<FoodInfo>, scoreList: List<IdNameScore>) {
|
||||||
|
Timber.d("registerDataChange识别后查询接口数据:${GsonUtils.toJson(list)}")
|
||||||
|
list.forEach { foodInfo ->
|
||||||
|
val scoreItem = scoreList.firstOrNull { it.name == foodInfo.foodName }
|
||||||
|
val score = scoreItem?.score ?: 0.0
|
||||||
|
foodInfo.score = ((1 - score) * 10000).roundToInt()
|
||||||
|
}
|
||||||
|
adapter.updateData(list)
|
||||||
|
if (list.isNotEmpty()) {
|
||||||
|
checkedItem = list[0]
|
||||||
|
checkedItem!!.photoUri = lastPhotoUri
|
||||||
|
updateCurrentFood(checkedItem)
|
||||||
|
lastPhotoUri = null
|
||||||
|
LightManager.closeRedLight()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,7 +303,8 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
|||||||
items = emptyList(),
|
items = emptyList(),
|
||||||
bindingInflater = ItemFoodInfoBinding::inflate,
|
bindingInflater = ItemFoodInfoBinding::inflate,
|
||||||
bindCallback = { item, position ->
|
bindCallback = { item, position ->
|
||||||
this.tvName.text = item.foodName
|
val score = (item.score / 100.0).format2String(2)
|
||||||
|
this.tvName.text = "${item.foodName}\n${score}%"
|
||||||
if (item.id != checkedItem?.id) {
|
if (item.id != checkedItem?.id) {
|
||||||
this.tvName.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
|
this.tvName.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
|
||||||
this.tvName.setTextColor(resources.getColor(R.color.identify_normal))
|
this.tvName.setTextColor(resources.getColor(R.color.identify_normal))
|
||||||
@@ -195,6 +317,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
|||||||
//
|
//
|
||||||
this.llRoot.setOnClickListener {
|
this.llRoot.setOnClickListener {
|
||||||
Timber.d("itemClick ${item.foodName}, position = $position")
|
Timber.d("itemClick ${item.foodName}, position = $position")
|
||||||
|
item.photoUri = null
|
||||||
checkedItem = item
|
checkedItem = item
|
||||||
adapter.notifyDataSetChanged()
|
adapter.notifyDataSetChanged()
|
||||||
updateCurrentFood(item)
|
updateCurrentFood(item)
|
||||||
@@ -226,7 +349,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
|||||||
// 仅在步骤1时传递
|
// 仅在步骤1时传递
|
||||||
if (isAnalyzing) {
|
if (isAnalyzing) {
|
||||||
val bitmap = imageProxy.toBitmap()
|
val bitmap = imageProxy.toBitmap()
|
||||||
presentation.updateImage(bitmap) // 更新副屏
|
presentation?.updateImage(bitmap) // 更新副屏
|
||||||
}
|
}
|
||||||
imageProxy.close() // 必须关闭以释放资源
|
imageProxy.close() // 必须关闭以释放资源
|
||||||
}
|
}
|
||||||
@@ -266,9 +389,9 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
|||||||
if (isAnalyzing) return
|
if (isAnalyzing) return
|
||||||
isAnalyzing = true
|
isAnalyzing = true
|
||||||
imageAnalysis?.setAnalyzer(executor) { imageProxy ->
|
imageAnalysis?.setAnalyzer(executor) { imageProxy ->
|
||||||
if (presentation.currentStep == 1) {
|
if (presentation?.currentStep == 1) {
|
||||||
val bitmap = imageProxy.toBitmap()
|
val bitmap = imageProxy.toBitmap()
|
||||||
presentation.updateImage(bitmap)
|
presentation?.updateImage(bitmap)
|
||||||
}
|
}
|
||||||
imageProxy.close()
|
imageProxy.close()
|
||||||
}
|
}
|
||||||
@@ -323,7 +446,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
|||||||
runOnUiThread {
|
runOnUiThread {
|
||||||
val savedUri = outputFileResults.savedUri
|
val savedUri = outputFileResults.savedUri
|
||||||
Timber.d("takePhoto savedUri = $savedUri")
|
Timber.d("takePhoto savedUri = $savedUri")
|
||||||
ToastUtils.showToast("照片保存成功: $savedUri")
|
// ToastUtils.showToast("照片保存成功: $savedUri")
|
||||||
if (savedUri != null) {
|
if (savedUri != null) {
|
||||||
callback(savedUri)
|
callback(savedUri)
|
||||||
}
|
}
|
||||||
@@ -360,19 +483,28 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
|||||||
viewModel = viewModel,
|
viewModel = viewModel,
|
||||||
recognizeViewModel = viewModels<RecognizeViewModel>().value
|
recognizeViewModel = viewModels<RecognizeViewModel>().value
|
||||||
)
|
)
|
||||||
presentation.setStepChangeCallback { step ->
|
presentation!!.setStepChangeCallback { step ->
|
||||||
if (step == 1) {
|
if (step == 1) {
|
||||||
|
Timber.d("setStepChangeCallback${step}")
|
||||||
updateCurrentFood(null)
|
updateCurrentFood(null)
|
||||||
bottomSheetDialog?.dismiss()
|
bottomSheetDialog?.dismiss()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
presentation.show()
|
presentation!!.show()
|
||||||
|
} else {
|
||||||
|
ToastUtils.showToast("获取副屏失败")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun clearFoodList() {
|
||||||
|
adapter.updateData(listOf())
|
||||||
|
adapter.notifyDataSetChanged()
|
||||||
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
SensorScaleUtils.closeScale()
|
SensorScaleUtils.closeScale()
|
||||||
presentation.dismiss()
|
presentation?.dismiss()
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
|
shutdownCamera()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package com.sw.dualscreen.adapter
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import android.widget.ImageView
|
||||||
|
import com.chad.library.adapter4.BaseQuickAdapter
|
||||||
|
import com.chad.library.adapter4.viewholder.QuickViewHolder
|
||||||
|
import com.sw.dualscreen.R
|
||||||
|
import com.sw.dualscreen.databinding.ListItemCollectedDataBinding
|
||||||
|
import com.sw.dualscreen.databinding.ListItemFoodCollectionBinding
|
||||||
|
import com.sw.dualscreen.objbox.CollectedFoodBean
|
||||||
|
import com.sw.dualscreen.objbox.FoodCollectionBean
|
||||||
|
|
||||||
|
class CollectedFoodAdapter (var list: MutableList<CollectedFoodBean>) :
|
||||||
|
BaseQuickAdapter<CollectedFoodBean, CollectedFoodAdapter.VH>(list) {
|
||||||
|
|
||||||
|
inner class VH(var binding: ListItemCollectedDataBinding) : QuickViewHolder(binding.root)
|
||||||
|
|
||||||
|
override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH {
|
||||||
|
val inflater = LayoutInflater.from(context)
|
||||||
|
val binding = ListItemCollectedDataBinding.inflate(inflater, parent, false)
|
||||||
|
return VH(binding)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onBindViewHolder(holder: VH, position: Int, item: CollectedFoodBean?) {
|
||||||
|
holder.binding.tvFoodName.text = item?.foodName
|
||||||
|
holder.binding.divider.run {
|
||||||
|
visibility = if (position == list.size - 1) View.GONE else View.VISIBLE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package com.sw.dualscreen.adapter
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import android.widget.ImageView
|
||||||
|
import com.chad.library.adapter4.BaseQuickAdapter
|
||||||
|
import com.chad.library.adapter4.viewholder.QuickViewHolder
|
||||||
|
import com.sw.dualscreen.R
|
||||||
|
import com.sw.dualscreen.databinding.ListItemFoodCollectionBinding
|
||||||
|
import com.sw.dualscreen.objbox.FoodCollectionBean
|
||||||
|
|
||||||
|
class FoodCollectionAdapter (var list: MutableList<FoodCollectionBean>) :
|
||||||
|
BaseQuickAdapter<FoodCollectionBean, FoodCollectionAdapter.VH>(list) {
|
||||||
|
|
||||||
|
inner class VH(var binding: ListItemFoodCollectionBinding) : QuickViewHolder(binding.root)
|
||||||
|
|
||||||
|
override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH {
|
||||||
|
val inflater = LayoutInflater.from(context)
|
||||||
|
val binding = ListItemFoodCollectionBinding.inflate(inflater, parent, false)
|
||||||
|
return VH(binding)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onBindViewHolder(holder: VH, position: Int, item: FoodCollectionBean?) {
|
||||||
|
val binding = holder.binding
|
||||||
|
item?.let {
|
||||||
|
binding.ivFinish.visibility = if (it.isFinish) View.VISIBLE else View.GONE
|
||||||
|
binding.ivDelete.visibility = if (it.isShowCamera) View.GONE else View.VISIBLE
|
||||||
|
binding.imageView.run {
|
||||||
|
if (it.isShowCamera) {
|
||||||
|
scaleType = ImageView.ScaleType.CENTER
|
||||||
|
setImageResource(R.drawable.ic_camera256)
|
||||||
|
} else {
|
||||||
|
scaleType = ImageView.ScaleType.FIT_CENTER
|
||||||
|
//setImageURI(it.imageUri)
|
||||||
|
setImageBitmap(it.bitmap)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package com.sw.dualscreen.dialog
|
||||||
|
|
||||||
|
import android.app.Dialog
|
||||||
|
import android.content.Context
|
||||||
|
import android.graphics.drawable.ColorDrawable
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.Gravity
|
||||||
|
import android.view.View
|
||||||
|
import com.sw.dualscreen.R
|
||||||
|
import com.sw.dualscreen.ext.dp
|
||||||
|
import com.sw.dualscreen.ext.hideKeyboard
|
||||||
|
|
||||||
|
abstract class BaseDialog(
|
||||||
|
context: Context,
|
||||||
|
var defWidth: Int = 400.dp,
|
||||||
|
var defHeight: Int = 260.dp
|
||||||
|
) : Dialog(context, R.style.DialogTheme) {
|
||||||
|
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
val rootView = getRootView()
|
||||||
|
setContentView(rootView)
|
||||||
|
setCancelable(false)
|
||||||
|
setCanceledOnTouchOutside(false)
|
||||||
|
window?.apply {
|
||||||
|
setLayout(defWidth, defHeight)
|
||||||
|
setBackgroundDrawable(ColorDrawable())
|
||||||
|
setGravity(Gravity.CENTER)
|
||||||
|
}
|
||||||
|
rootView.setOnClickListener { it.hideKeyboard() }
|
||||||
|
initView()
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract fun getRootView(): View
|
||||||
|
|
||||||
|
abstract fun initView()
|
||||||
|
|
||||||
|
// fun getReceiptActivity(): GoodsListActivity? {
|
||||||
|
// var ctx = context
|
||||||
|
// while (ctx is ContextWrapper) {
|
||||||
|
// ctx = ctx.baseContext
|
||||||
|
// if (ctx is GoodsListActivity) {
|
||||||
|
// return ctx
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// return null
|
||||||
|
// }
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package com.sw.dualscreen.dialog
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import com.sw.dualscreen.databinding.DialogWarnBinding
|
||||||
|
|
||||||
|
class WarnDialog(
|
||||||
|
context: Context,
|
||||||
|
var content:String,
|
||||||
|
var cancelBlock: () -> Unit = {},
|
||||||
|
var confirmBlock: () -> Unit = {},
|
||||||
|
) : BaseDialog(context = context) {
|
||||||
|
|
||||||
|
private lateinit var binding: DialogWarnBinding
|
||||||
|
override fun getRootView(): View {
|
||||||
|
val inflater = LayoutInflater.from(context)
|
||||||
|
binding = DialogWarnBinding.inflate(inflater)
|
||||||
|
return binding.root
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun initView() {
|
||||||
|
binding.tvWarnContent.text = content
|
||||||
|
binding.btnCancel.setOnClickListener {
|
||||||
|
cancelBlock()
|
||||||
|
dismiss()
|
||||||
|
}
|
||||||
|
binding.btnConfirm.setOnClickListener {
|
||||||
|
confirmBlock()
|
||||||
|
dismiss()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.sw.dualscreen.ext
|
package com.sw.dualscreen.ext
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
import android.content.res.Resources
|
import android.content.res.Resources
|
||||||
import android.graphics.Bitmap
|
import android.graphics.Bitmap
|
||||||
import android.graphics.BitmapFactory
|
import android.graphics.BitmapFactory
|
||||||
@@ -7,8 +8,17 @@ import android.graphics.ImageFormat
|
|||||||
import android.graphics.Rect
|
import android.graphics.Rect
|
||||||
import android.graphics.YuvImage
|
import android.graphics.YuvImage
|
||||||
import android.util.TypedValue
|
import android.util.TypedValue
|
||||||
|
import android.view.View
|
||||||
|
import android.view.inputmethod.EditorInfo
|
||||||
|
import android.view.inputmethod.InputMethodManager
|
||||||
|
import android.widget.EditText
|
||||||
import androidx.annotation.Dimension
|
import androidx.annotation.Dimension
|
||||||
import androidx.camera.core.ImageProxy
|
import androidx.camera.core.ImageProxy
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.Job
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import java.io.ByteArrayOutputStream
|
import java.io.ByteArrayOutputStream
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -56,6 +66,7 @@ val Float.sp: Float
|
|||||||
)
|
)
|
||||||
|
|
||||||
fun Double.format2String(): String = "%.1f".format(this)
|
fun Double.format2String(): String = "%.1f".format(this)
|
||||||
|
fun Double.format2String(num:Int): String = "%.${num}f".format(this)
|
||||||
|
|
||||||
// 添加扩展函数
|
// 添加扩展函数
|
||||||
fun ImageProxy.toSafeBitmap(): Bitmap {
|
fun ImageProxy.toSafeBitmap(): Bitmap {
|
||||||
@@ -82,3 +93,30 @@ fun ImageProxy.toSafeBitmap(): Bitmap {
|
|||||||
val imageBytes = out.toByteArray()
|
val imageBytes = out.toByteArray()
|
||||||
return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
|
return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun View.hideKeyboard() {
|
||||||
|
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
|
||||||
|
imm.hideSoftInputFromWindow(this.windowToken, 0)
|
||||||
|
this.clearFocus() // 清除焦点避免键盘再次弹出
|
||||||
|
}
|
||||||
|
|
||||||
|
fun EditText.addOnActionSearchListener(searchCallback: () -> Unit) {
|
||||||
|
setOnEditorActionListener { v, actionId, event ->
|
||||||
|
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
|
||||||
|
// 处理搜索逻辑
|
||||||
|
searchCallback()
|
||||||
|
return@setOnEditorActionListener true // 阻止事件继续传递
|
||||||
|
}
|
||||||
|
return@setOnEditorActionListener false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fun View.clickWithDebounce(delay: Long = 500, action: () -> Unit) {
|
||||||
|
var job: Job? = null
|
||||||
|
setOnClickListener {
|
||||||
|
job?.cancel()
|
||||||
|
job = CoroutineScope(Dispatchers.Main).launch {
|
||||||
|
delay(delay)
|
||||||
|
action()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package com.sw.dualscreen.model;
|
||||||
|
|
||||||
|
import androidx.activity.result.ActivityResult;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public interface IActivityResult {
|
||||||
|
|
||||||
|
interface LaunchActivityCallback {
|
||||||
|
void callback(ActivityResult result);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface LaunchTackPictureCallback {
|
||||||
|
void callback(boolean result);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface RequestPermissionCallback {
|
||||||
|
void callback(boolean result);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface RequestMultiplePermissionsCallback {
|
||||||
|
void callback(Map<String,Boolean> result);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.sw.dualscreen.model.response
|
package com.sw.dualscreen.model.response
|
||||||
|
|
||||||
|
|
||||||
|
import android.net.Uri
|
||||||
import android.os.Parcelable
|
import android.os.Parcelable
|
||||||
import android.text.TextUtils
|
import android.text.TextUtils
|
||||||
import com.google.gson.annotations.SerializedName
|
import com.google.gson.annotations.SerializedName
|
||||||
@@ -23,7 +24,11 @@ data class FoodInfo(
|
|||||||
@SerializedName("id")
|
@SerializedName("id")
|
||||||
val id: String? = "",
|
val id: String? = "",
|
||||||
@SerializedName("imgUrl")
|
@SerializedName("imgUrl")
|
||||||
val imgUrl: String? = "",
|
var imgUrl: String? = "",
|
||||||
|
/**
|
||||||
|
* 拍照结果
|
||||||
|
*/
|
||||||
|
var photoUri: Uri? = null,
|
||||||
@SerializedName("stFoodInfoMaterial")
|
@SerializedName("stFoodInfoMaterial")
|
||||||
val stFoodInfoMaterial: StFoodInfoMaterial? = StFoodInfoMaterial(),
|
val stFoodInfoMaterial: StFoodInfoMaterial? = StFoodInfoMaterial(),
|
||||||
@SerializedName("stFoodInfoPagoda")
|
@SerializedName("stFoodInfoPagoda")
|
||||||
@@ -33,7 +38,8 @@ data class FoodInfo(
|
|||||||
@SerializedName("stFoodInfoSetting")
|
@SerializedName("stFoodInfoSetting")
|
||||||
val stFoodInfoSetting: StFoodInfoSetting? = StFoodInfoSetting(),
|
val stFoodInfoSetting: StFoodInfoSetting? = StFoodInfoSetting(),
|
||||||
@SerializedName("stFoodInfoSpecificationList")
|
@SerializedName("stFoodInfoSpecificationList")
|
||||||
val stFoodInfoSpecificationList: List<StFoodInfoSpecification?>? = listOf()
|
val stFoodInfoSpecificationList: List<StFoodInfoSpecification?>? = listOf(),
|
||||||
|
var score: Int = 0
|
||||||
) : Parcelable {
|
) : Parcelable {
|
||||||
@Parcelize
|
@Parcelize
|
||||||
data class FoodTypeAndRealIntakeVo(
|
data class FoodTypeAndRealIntakeVo(
|
||||||
|
|||||||
@@ -8,10 +8,15 @@ import com.sw.dualscreen.model.response.EquipmentInfo
|
|||||||
import com.sw.dualscreen.model.response.FoodInfo
|
import com.sw.dualscreen.model.response.FoodInfo
|
||||||
import com.sw.dualscreen.model.response.UserFaceInfo
|
import com.sw.dualscreen.model.response.UserFaceInfo
|
||||||
import com.sw.dualscreen.model.response.UserNutritionData
|
import com.sw.dualscreen.model.response.UserNutritionData
|
||||||
|
import okhttp3.MultipartBody
|
||||||
|
import okhttp3.RequestBody
|
||||||
import retrofit2.http.Body
|
import retrofit2.http.Body
|
||||||
import retrofit2.http.GET
|
import retrofit2.http.GET
|
||||||
import retrofit2.http.Header
|
import retrofit2.http.Header
|
||||||
|
import retrofit2.http.Multipart
|
||||||
import retrofit2.http.POST
|
import retrofit2.http.POST
|
||||||
|
import retrofit2.http.Part
|
||||||
|
import retrofit2.http.PartMap
|
||||||
import retrofit2.http.Query
|
import retrofit2.http.Query
|
||||||
import retrofit2.http.Url
|
import retrofit2.http.Url
|
||||||
|
|
||||||
@@ -99,4 +104,27 @@ interface ApiService {
|
|||||||
@Url url: String = "${GlobalData.appBaseUrl}/zhstapi/zhst/userEatFood/doubleBalance/face/stall",
|
@Url url: String = "${GlobalData.appBaseUrl}/zhstapi/zhst/userEatFood/doubleBalance/face/stall",
|
||||||
@Body param: List<UserNutritionParam>
|
@Body param: List<UserNutritionParam>
|
||||||
): ApiResponse<String>
|
): ApiResponse<String>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取菜品信息
|
||||||
|
*/
|
||||||
|
@GET
|
||||||
|
suspend fun getFoodInfo(
|
||||||
|
@Url url: String = "${GlobalData.appBaseUrl}/zhstapi/zhst/getRestInfoFoodsByType/stall/v2",
|
||||||
|
@Query("restId") restId: String,
|
||||||
|
@Query("foodNames") foodName: String,
|
||||||
|
): ApiResponse<List<FoodInfo>>
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 提交采集图片数据
|
||||||
|
*/
|
||||||
|
@Multipart
|
||||||
|
@POST
|
||||||
|
suspend fun postImageData(
|
||||||
|
@Url url: String = "http://192.168.1.201:14801/terminal/neglect/dishCollectionVectorData/add",
|
||||||
|
@PartMap params: Map<String, @JvmSuppressWildcards RequestBody>,
|
||||||
|
@Part image: MultipartBody.Part?
|
||||||
|
): ApiResponse<String>
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,7 @@ package com.sw.dualscreen.network.interceptor
|
|||||||
|
|
||||||
import android.text.TextUtils
|
import android.text.TextUtils
|
||||||
import com.sw.dualscreen.GlobalKey
|
import com.sw.dualscreen.GlobalKey
|
||||||
import com.sw.inbound.utils.SPUtil
|
import com.sw.dualscreen.utils.SPUtil
|
||||||
import com.sw.plate.App
|
import com.sw.plate.App
|
||||||
import okhttp3.Interceptor
|
import okhttp3.Interceptor
|
||||||
import okhttp3.Request
|
import okhttp3.Request
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package com.sw.dualscreen.objbox
|
||||||
|
|
||||||
|
import android.graphics.Bitmap
|
||||||
|
import android.net.Uri
|
||||||
|
|
||||||
|
data class FoodClassInfo(
|
||||||
|
var class_names: List<String>,
|
||||||
|
var class_to_idx: Map<String, Int>,
|
||||||
|
var idx_to_class: Map<String, String>
|
||||||
|
)
|
||||||
|
|
||||||
|
//data class FoodQueryResult(
|
||||||
|
// var id: Long,
|
||||||
|
// var name: String?,
|
||||||
|
// var foodIdx: Int,
|
||||||
|
// var score: Double
|
||||||
|
//)
|
||||||
|
|
||||||
|
data class FoodCollectionBean(
|
||||||
|
var imageUri: Uri? = null,
|
||||||
|
var bitmap: Bitmap? = null,
|
||||||
|
var imageVector: FloatArray? = null,
|
||||||
|
var isShowCamera: Boolean = false,
|
||||||
|
var isFinish:Boolean = false
|
||||||
|
)
|
||||||
|
|
||||||
|
data class CollectedFoodBean(
|
||||||
|
var foodName: String?,
|
||||||
|
)
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package com.sw.dualscreen.objbox
|
||||||
|
|
||||||
|
import io.objectbox.annotation.Entity
|
||||||
|
import io.objectbox.annotation.HnswIndex
|
||||||
|
import io.objectbox.annotation.Id
|
||||||
|
import io.objectbox.annotation.VectorDistanceType
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
data class Food(
|
||||||
|
@Id var id: Long = 0,
|
||||||
|
var name: String? = null,
|
||||||
|
var foodIdx: Int = 0,
|
||||||
|
@HnswIndex(dimensions = 512, distanceType = VectorDistanceType.DOT_PRODUCT)
|
||||||
|
var foodVector: FloatArray? = null
|
||||||
|
) {
|
||||||
|
override fun equals(other: Any?): Boolean {
|
||||||
|
if (this === other) return true
|
||||||
|
if (javaClass != other?.javaClass) return false
|
||||||
|
|
||||||
|
other as Food
|
||||||
|
|
||||||
|
if (id != other.id) return false
|
||||||
|
if (foodIdx != other.foodIdx) return false
|
||||||
|
if (name != other.name) return false
|
||||||
|
if (!foodVector.contentEquals(other.foodVector)) return false
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun hashCode(): Int {
|
||||||
|
var result = id.hashCode()
|
||||||
|
result = 31 * result + foodIdx
|
||||||
|
result = 31 * result + (name?.hashCode() ?: 0)
|
||||||
|
result = 31 * result + (foodVector?.contentHashCode() ?: 0)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,241 @@
|
|||||||
|
package com.sw.dualscreen.objbox
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.graphics.Bitmap
|
||||||
|
import android.net.Uri
|
||||||
|
import android.util.SparseLongArray
|
||||||
|
import com.google.gson.Gson
|
||||||
|
import com.google.gson.reflect.TypeToken
|
||||||
|
import com.sw.dualscreen.MyApp
|
||||||
|
import com.sw.dualscreen.utils.AssetsTool
|
||||||
|
import com.sw.dualscreen.utils.GsonUtils
|
||||||
|
import com.sw.dualscreen.utils.ImageUtil
|
||||||
|
import io.objectbox.Box
|
||||||
|
import io.objectbox.kotlin.boxFor
|
||||||
|
import io.objectbox.query.Query
|
||||||
|
import org.pytorch.IValue
|
||||||
|
import org.pytorch.Module
|
||||||
|
import org.pytorch.torchvision.TensorImageUtils
|
||||||
|
import timber.log.Timber
|
||||||
|
import java.io.File
|
||||||
|
import java.io.FileOutputStream
|
||||||
|
import java.io.IOException
|
||||||
|
import java.io.InputStream
|
||||||
|
|
||||||
|
object FoodModule {
|
||||||
|
|
||||||
|
private const val THRESHOLD = 0.8
|
||||||
|
private lateinit var module_mobile: Module
|
||||||
|
private lateinit var box: Box<Food>
|
||||||
|
private lateinit var embeddingsList: List<List<Float>>
|
||||||
|
private lateinit var labelsList: IntArray
|
||||||
|
private lateinit var classInfo: FoodClassInfo
|
||||||
|
val NO_MEAN_RGB = floatArrayOf(0.0f, 0.0f, 0.0f)
|
||||||
|
val NO_STD_RGB = floatArrayOf(1.0f, 1.0f, 1.0f)
|
||||||
|
val DEFAULT_FOOD_INDEX = -1
|
||||||
|
|
||||||
|
fun init(context: Context) {
|
||||||
|
Thread {
|
||||||
|
module_mobile = Module.load(copyAssetToCache(context, "best_embedding_model_mobile.pt"))
|
||||||
|
box = ObjectBox.boxStore.boxFor(Food::class)
|
||||||
|
//if (box.all.isNotEmpty()) {
|
||||||
|
// box.removeAll()
|
||||||
|
//}
|
||||||
|
//if (box.all.isEmpty()) {
|
||||||
|
// initDefFoodData(context)
|
||||||
|
//}
|
||||||
|
}.start()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun uri2FloatArray(uri: Uri): FloatArray? {
|
||||||
|
return MyApp.instance?.let { context ->
|
||||||
|
ImageUtil.uriToBitmap(context, uri)?.let {
|
||||||
|
bitmap2FloatArray(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun bitmap2FloatArray(bitmap: Bitmap): FloatArray {
|
||||||
|
val inputTensor = TensorImageUtils.bitmapToFloat32Tensor(
|
||||||
|
bitmap,
|
||||||
|
NO_MEAN_RGB, // [0.485, 0.456, 0.406] TORCHVISION_NORM_MEAN_RGB
|
||||||
|
NO_STD_RGB // [0.229, 0.224, 0.225] TORCHVISION_NORM_STD_RGB
|
||||||
|
)
|
||||||
|
val outputTensor = module_mobile.forward(IValue.from(inputTensor)).toTensor()
|
||||||
|
return outputTensor.dataAsFloatArray
|
||||||
|
}
|
||||||
|
|
||||||
|
fun queryFood(uri: Uri, queryCount: Int = 15): List<String>? {
|
||||||
|
return uri2FloatArray(uri)?.let {
|
||||||
|
queryFood(it, queryCount)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回识别物品名称列表
|
||||||
|
*/
|
||||||
|
fun queryFood(bitmap: Bitmap, queryCount: Int = 15): List<String> {
|
||||||
|
val floatArray = bitmap2FloatArray(bitmap)
|
||||||
|
return queryFood(floatArray, queryCount)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回识别物品IdNameScore对象列表
|
||||||
|
*/
|
||||||
|
fun queryFoodNameScore(bitmap: Bitmap, queryCount: Int = 15): List<IdNameScore> {
|
||||||
|
val floatArray = bitmap2FloatArray(bitmap)
|
||||||
|
return queryFoodNameScore(floatArray, queryCount)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun queryFoodNameScore(floatArray: FloatArray, queryCount: Int = 15): List<IdNameScore> {
|
||||||
|
val query: Query<Food> =
|
||||||
|
box.query(Food_.foodVector.nearestNeighbors(floatArray, queryCount)).build()
|
||||||
|
//查询比较分数
|
||||||
|
// val tempList = query.findWithScores().sortedBy { it.score }.map { "${it.get().name}|${it.get().foodIdx}|${it.score}" }
|
||||||
|
val idScoreList = query.findIdsWithScores()
|
||||||
|
val nameScoreList = mutableListOf<IdNameScore>()
|
||||||
|
idScoreList.forEach {
|
||||||
|
nameScoreList.add(IdNameScore(id = it.id, name = box.get(it.id).name?:"", score = it.score))
|
||||||
|
}
|
||||||
|
Timber.tag("FoodModule").d("registerDataChange,queryFood数据:${GsonUtils.toJson(nameScoreList)}")
|
||||||
|
return nameScoreList
|
||||||
|
}
|
||||||
|
fun getFoodScoreList(bitmap: Bitmap, queryCount: Int = 15): List<IdNameScore> {
|
||||||
|
val floatArray = bitmap2FloatArray(bitmap)
|
||||||
|
val nameScoreList = queryFoodNameScore(floatArray, queryCount)
|
||||||
|
val maxScoreList = nameScoreList
|
||||||
|
.filter { it.score < 1 - THRESHOLD }
|
||||||
|
.groupBy { it.name }
|
||||||
|
.map { (_, value) -> value.minByOrNull { it.score }!! }
|
||||||
|
.toMutableList()
|
||||||
|
val map = mutableMapOf<String, Int>()
|
||||||
|
nameScoreList.forEach {
|
||||||
|
val key = it.name
|
||||||
|
val count = map[key] ?: 0
|
||||||
|
map[key] = count + 1
|
||||||
|
}
|
||||||
|
val orderList = map.entries.sortedByDescending { it.value }.map { it.key }.toMutableList()
|
||||||
|
val firstFood = nameScoreList[0].name
|
||||||
|
orderList.remove(firstFood)
|
||||||
|
orderList.add(0, firstFood)
|
||||||
|
|
||||||
|
val sortedScoreList = maxScoreList.sortedWith(compareBy {
|
||||||
|
orderList.indexOf(it.name)
|
||||||
|
})
|
||||||
|
//Timber.tag("FoodModule").d("getFoodScoreList数据:${sortedScoreList.toJsonString()}")
|
||||||
|
return sortedScoreList
|
||||||
|
}
|
||||||
|
fun queryFood(floatArray: FloatArray, queryCount: Int = 15): List<String> {
|
||||||
|
val query: Query<Food> =
|
||||||
|
box.query(Food_.foodVector.nearestNeighbors(floatArray, queryCount)).build()
|
||||||
|
//查询比较分数
|
||||||
|
// val tempList = query.findWithScores().sortedBy { it.score }.map { "${it.get().name}|${it.get().foodIdx}|${it.score}" }
|
||||||
|
val map = mutableMapOf<String, Int>()
|
||||||
|
query.findIdsWithScores().forEach {
|
||||||
|
val food = box.get(it.id)
|
||||||
|
Timber.d("${food.name}|${food.foodIdx}|${it.score}")
|
||||||
|
if (1 - it.score >= THRESHOLD) {
|
||||||
|
//FoodQueryResult(id = it.id, name = food.name, foodIdx = food.foodIdx, score = it.score)
|
||||||
|
food.name?.let { key ->
|
||||||
|
val count = map[key] ?: 0
|
||||||
|
map.put(key, count + 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val list = map.entries.sortedByDescending { it.value }.map { it.key }
|
||||||
|
return list
|
||||||
|
|
||||||
|
// val map = mutableMapOf<String, Int>()
|
||||||
|
// val nameScoreList = queryFoodNameScore(floatArray, queryCount)
|
||||||
|
// nameScoreList.filter { it.score < 0.05 }.forEach {
|
||||||
|
// val count = map[it.name] ?: 0
|
||||||
|
// map[it.name] = count + 1
|
||||||
|
// }
|
||||||
|
// val list = map.entries.sortedByDescending { it.value }.map { it.key }
|
||||||
|
// return list
|
||||||
|
}
|
||||||
|
|
||||||
|
data class IdNameScore(
|
||||||
|
val id:Long,
|
||||||
|
var name:String,
|
||||||
|
val score: Double
|
||||||
|
)
|
||||||
|
|
||||||
|
fun initDefFoodData(context: Context, action:()-> Unit={}) {
|
||||||
|
val count = box.all.count { it.foodIdx == DEFAULT_FOOD_INDEX }
|
||||||
|
if (count > 0) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
val embeddingsJson = AssetsTool.readAssetsFile(context, "data/embeddings.json")
|
||||||
|
val labelsJson = AssetsTool.readAssetsFile(context, "data/labels.json")
|
||||||
|
val classInfoJson = AssetsTool.readAssetsFile(context, "data/class_info.json")
|
||||||
|
|
||||||
|
embeddingsList =
|
||||||
|
Gson().fromJson(embeddingsJson, object : TypeToken<List<List<Float>>>() {}.type)
|
||||||
|
labelsList = Gson().fromJson(labelsJson, IntArray::class.java)
|
||||||
|
classInfo =
|
||||||
|
Gson().fromJson(classInfoJson, FoodClassInfo::class.java)
|
||||||
|
|
||||||
|
val foodMap = classInfo.idx_to_class
|
||||||
|
embeddingsList.forEachIndexed { index, floatList ->
|
||||||
|
val classIdx = labelsList[index]
|
||||||
|
val foodName = foodMap["$classIdx"]
|
||||||
|
val array = floatList.toFloatArray()
|
||||||
|
box.put(Food(name = foodName, foodVector = array, foodIdx = DEFAULT_FOOD_INDEX))
|
||||||
|
}
|
||||||
|
action()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ,此方法的主要目的是:从assets 拷贝到 app的cache目录
|
||||||
|
* @param context
|
||||||
|
* @param fileName
|
||||||
|
* @return 例如是这样:/data/user/0/com.frizzle.pluginhookandroid9/cache/plugin-debug.apk
|
||||||
|
*
|
||||||
|
* 不可能反正SD
|
||||||
|
*/
|
||||||
|
fun copyAssetToCache(context: Context, fileName: String): String? {
|
||||||
|
// 此app的缓存目录 --> 会默认在 cache目录...,可以自己去看看哦
|
||||||
|
val cacheDir = context.getCacheDir()
|
||||||
|
if (!cacheDir.exists()) {
|
||||||
|
cacheDir.mkdirs() // TODO 如果没有缓存目录,就创建
|
||||||
|
}
|
||||||
|
val outPath = File(cacheDir, fileName) // TODO 创建输出的文件位置
|
||||||
|
if (outPath.exists()) {
|
||||||
|
outPath.delete() // TODO 如果该文件已经存在,就删掉
|
||||||
|
}
|
||||||
|
var `is`: InputStream? = null // 读取
|
||||||
|
var fos: FileOutputStream? = null // 写入
|
||||||
|
try {
|
||||||
|
// 创建文件,如果创建成功,就返回true
|
||||||
|
val res = outPath.createNewFile()
|
||||||
|
if (res) {
|
||||||
|
`is` = context.getAssets().open(fileName) // 拿到main/assets目录的输入流,用于读取字节
|
||||||
|
fos = FileOutputStream(outPath) // 读取出来的字节最终写到outPath
|
||||||
|
val buf = ByteArray(`is`.available()) // 缓存区
|
||||||
|
var byteCount: Int
|
||||||
|
|
||||||
|
// 开始循环读取
|
||||||
|
while ((`is`.read(buf).also { byteCount = it }) != -1) {
|
||||||
|
fos.write(buf, 0, byteCount)
|
||||||
|
}
|
||||||
|
return outPath.getAbsolutePath()
|
||||||
|
}
|
||||||
|
} catch (e: IOException) {
|
||||||
|
e.printStackTrace()
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
// TODO 一定要记得关闭资源,为了不去性能的磨损
|
||||||
|
fos!!.flush()
|
||||||
|
`is`!!.close()
|
||||||
|
fos.close()
|
||||||
|
} catch (e: IOException) {
|
||||||
|
e.printStackTrace()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2024 ObjectBox Ltd. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.sw.dualscreen.objbox
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.util.Log
|
||||||
|
import io.objectbox.BoxStore
|
||||||
|
import io.objectbox.BoxStoreBuilder
|
||||||
|
import io.objectbox.android.Admin
|
||||||
|
import io.objectbox.android.ObjectBoxLiveData
|
||||||
|
import io.objectbox.exception.DbException
|
||||||
|
import io.objectbox.exception.FileCorruptException
|
||||||
|
import io.objectbox.sync.Sync
|
||||||
|
import java.io.File
|
||||||
|
import java.util.Date
|
||||||
|
import java.util.zip.GZIPOutputStream
|
||||||
|
import kotlin.also
|
||||||
|
import kotlin.io.copyTo
|
||||||
|
import kotlin.io.inputStream
|
||||||
|
import kotlin.io.outputStream
|
||||||
|
import kotlin.io.use
|
||||||
|
import kotlin.jvm.java
|
||||||
|
import kotlin.jvm.javaClass
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Singleton to keep BoxStore reference and provide current list of Notes Objects.
|
||||||
|
* Inserts demo data if no Objects are stored.
|
||||||
|
*/
|
||||||
|
object ObjectBox {
|
||||||
|
|
||||||
|
// 确保所有设备/版本使用相同的objectbox-models/default.json
|
||||||
|
// 避免直接修改自动生成的MyObjectBox类
|
||||||
|
// 跨版本升级时使用boxStore.runInTx执行数据迁移
|
||||||
|
|
||||||
|
|
||||||
|
private const val TAG = "ObjectBox"
|
||||||
|
|
||||||
|
lateinit var boxStore: BoxStore
|
||||||
|
private set
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If building the [boxStore] failed, contains the thrown error message.
|
||||||
|
*/
|
||||||
|
var dbExceptionMessage: String? = null
|
||||||
|
private set
|
||||||
|
|
||||||
|
fun init(context: Context) {
|
||||||
|
// On Android make sure to pass a Context when building the Store.
|
||||||
|
boxStore = try {
|
||||||
|
MyObjectBox.builder()
|
||||||
|
.androidContext(context.applicationContext)
|
||||||
|
.build()
|
||||||
|
} catch (e: DbException) {
|
||||||
|
if (e.javaClass.equals(DbException::class.java) || e is FileCorruptException) {
|
||||||
|
// Failed to build BoxStore due to database file issue, store message;
|
||||||
|
// checked in NoteListActivity to notify user.
|
||||||
|
dbExceptionMessage = e.toString()
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
// Failed to build BoxStore due to developer error.
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if (BuildConfig.DEBUG) {
|
||||||
|
var syncAvailable = if (Sync.isAvailable()) "available" else "unavailable"
|
||||||
|
Log.d(TAG,"Using ObjectBox ${BoxStore.getVersion()} (${BoxStore.getVersionNative()}, sync $syncAvailable)")
|
||||||
|
// Enable ObjectBox Admin on debug builds.
|
||||||
|
// https://docs.objectbox.io/data-browser
|
||||||
|
Admin(boxStore).start(context.applicationContext)
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If the database file is not in use, compresses (GZIP) and copies it to the given [target].
|
||||||
|
*/
|
||||||
|
fun copyAndGzipDatabaseFileTo(target: File, context: Context): Boolean {
|
||||||
|
if (BoxStore.isDatabaseOpen(context, null)) {
|
||||||
|
// Do not copy if database file is still in use.
|
||||||
|
// If it would be open, the copy will likely get corrupted
|
||||||
|
// as BoxStore may currently write data to the file.
|
||||||
|
Log.e(TAG, "Database file is still in use, can not copy.")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// If a name was given when building BoxStore use that instead of the default below.
|
||||||
|
val dbName = BoxStoreBuilder.DEFAULT_NAME
|
||||||
|
File(context.filesDir, "objectbox/$dbName/data.mdb").inputStream().use { input ->
|
||||||
|
target.parentFile?.mkdirs()
|
||||||
|
GZIPOutputStream(target.outputStream()).use { output ->
|
||||||
|
input.copyTo(output, DEFAULT_BUFFER_SIZE)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package com.sw.dualscreen.presentation
|
package com.sw.dualscreen.presentation
|
||||||
|
|
||||||
import android.Manifest
|
import android.Manifest
|
||||||
import android.R.attr.visibility
|
|
||||||
import android.app.Presentation
|
import android.app.Presentation
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.pm.PackageManager
|
import android.content.pm.PackageManager
|
||||||
@@ -36,6 +35,7 @@ import com.sw.dualscreen.model.response.UserNutritionData
|
|||||||
import com.sw.dualscreen.utils.Debouncer
|
import com.sw.dualscreen.utils.Debouncer
|
||||||
import com.sw.dualscreen.utils.GlideUtils
|
import com.sw.dualscreen.utils.GlideUtils
|
||||||
import com.sw.dualscreen.viewmodel.UserViewModel
|
import com.sw.dualscreen.viewmodel.UserViewModel
|
||||||
|
import com.sw.plate.utils.LightManager
|
||||||
import com.sw.plate.utils.ToastUtils
|
import com.sw.plate.utils.ToastUtils
|
||||||
import com.sw.plate.utils.arcface.ConfigUtil
|
import com.sw.plate.utils.arcface.ConfigUtil
|
||||||
import com.sw.plate.utils.arcface.ErrorCodeUtil
|
import com.sw.plate.utils.arcface.ErrorCodeUtil
|
||||||
@@ -74,17 +74,20 @@ class SecondaryScreenPresentation(
|
|||||||
private lateinit var itemUserNutritionBinding: ItemUserNutritionBinding
|
private lateinit var itemUserNutritionBinding: ItemUserNutritionBinding
|
||||||
private lateinit var stepChangeCallback: (Int) -> Unit
|
private lateinit var stepChangeCallback: (Int) -> Unit
|
||||||
|
|
||||||
// 上一次的计算热量结果
|
|
||||||
private var lastWeight = 0.0
|
private var detectWeight = 0.0 //识别菜品时的重量
|
||||||
|
private var lastWeight = 0.0 // 上一次的计算热量结果
|
||||||
private val debouncer = Debouncer(500)
|
private val debouncer = Debouncer(500)
|
||||||
private var recognitionTime: Long = 0L // 人脸识别时的时间
|
private var recognitionTime: Long = 0L // 人脸识别时的时间
|
||||||
private var recognitionWeight: Double = 0.0 // 人脸识别时的重量
|
private var recognitionWeight: Double = 0.0 // 人脸识别时的重量
|
||||||
private var lastFaceTrackId: Int = 0 // 上一次的人脸信息
|
private var lastFaceTrackId: Int = -1 // 上一次的人脸信息
|
||||||
private var mealPickupMode: Int = 0 // 取餐模式 0 即放即取 1 余量取餐
|
var mealPickupMode: Int = 0 // 取餐模式 0 即放即取 1 余量取餐
|
||||||
|
|
||||||
// 当前步骤
|
// 当前步骤
|
||||||
var currentStep: Int = 0
|
var currentStep: Int = 0
|
||||||
|
|
||||||
|
private var isGoStep1 = false;//防止持续调用
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
@@ -93,7 +96,6 @@ class SecondaryScreenPresentation(
|
|||||||
window?.setBackgroundDrawableResource(android.R.color.transparent)
|
window?.setBackgroundDrawableResource(android.R.color.transparent)
|
||||||
initView()
|
initView()
|
||||||
setupArcCamera()
|
setupArcCamera()
|
||||||
// binding.includeUserHeat.ivHeat.showText("1070千卡")
|
|
||||||
registerDataChange()
|
registerDataChange()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,6 +164,8 @@ class SecondaryScreenPresentation(
|
|||||||
fun step1() {
|
fun step1() {
|
||||||
Timber.d("step1")
|
Timber.d("step1")
|
||||||
currentStep = 1
|
currentStep = 1
|
||||||
|
LightManager.closeGreenLight()
|
||||||
|
LightManager.closeRedLight()
|
||||||
//回复上一次的设置
|
//回复上一次的设置
|
||||||
// lastWeight = 0.0
|
// lastWeight = 0.0
|
||||||
dinnerTypeInfo = null
|
dinnerTypeInfo = null
|
||||||
@@ -175,18 +179,23 @@ class SecondaryScreenPresentation(
|
|||||||
binding.flPreview.visibility = View.VISIBLE
|
binding.flPreview.visibility = View.VISIBLE
|
||||||
binding.ivImg.visibility = View.VISIBLE
|
binding.ivImg.visibility = View.VISIBLE
|
||||||
binding.flFace.visibility = View.INVISIBLE
|
binding.flFace.visibility = View.INVISIBLE
|
||||||
|
binding.tvFaceTip.visibility = View.GONE
|
||||||
pauseCamera()
|
pauseCamera()
|
||||||
binding.tvBottomTip.visibility = View.VISIBLE
|
binding.tvBottomTip.visibility = View.VISIBLE
|
||||||
itemUserNutritionBinding.clNutritionData.visibility = View.GONE
|
itemUserNutritionBinding.clNutritionData.visibility = View.GONE
|
||||||
|
|
||||||
|
activity.clearFoodList()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 显示人脸识别
|
* 显示人脸识别
|
||||||
*/
|
*/
|
||||||
fun step2(foodInfo: FoodInfo) {
|
fun step2(foodInfo: FoodInfo) {
|
||||||
|
isGoStep1 = true
|
||||||
Timber.d("step2")
|
Timber.d("step2")
|
||||||
currentStep = 2
|
currentStep = 2
|
||||||
|
LightManager.closeRedLight()
|
||||||
|
// LightManager.openGreenLight()
|
||||||
dinnerTypeInfo = null
|
dinnerTypeInfo = null
|
||||||
userNutritionData = null
|
userNutritionData = null
|
||||||
recognitionWeight = 0.0
|
recognitionWeight = 0.0
|
||||||
@@ -217,13 +226,7 @@ class SecondaryScreenPresentation(
|
|||||||
if (currentStep == 1) {
|
if (currentStep == 1) {
|
||||||
step2(foodInfo)
|
step2(foodInfo)
|
||||||
} else {
|
} else {
|
||||||
binding.tvFoodName.text = foodInfo.foodName
|
updateFoodInfo(foodInfo)
|
||||||
GlideUtils.loadRoundCornerImage(
|
|
||||||
context,
|
|
||||||
url = currentFood!!.imgUrl,
|
|
||||||
imageView = binding.ivImg,
|
|
||||||
radius = 12
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -233,19 +236,15 @@ class SecondaryScreenPresentation(
|
|||||||
*/
|
*/
|
||||||
fun step3() {
|
fun step3() {
|
||||||
Timber.d("step3")
|
Timber.d("step3")
|
||||||
|
LightManager.closeRedLight()
|
||||||
|
LightManager.openGreenLight()
|
||||||
currentStep = 3
|
currentStep = 3
|
||||||
stepChangeCallback(currentStep)
|
stepChangeCallback(currentStep)
|
||||||
if (currentFood == null) {
|
if (currentFood == null) {
|
||||||
ToastUtils.showToast("选中物品为空3")
|
ToastUtils.showToast("选中物品为空3")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
binding.tvFoodName.text = currentFood!!.foodName
|
updateFoodInfo(currentFood!!)
|
||||||
GlideUtils.loadRoundCornerImage(
|
|
||||||
context,
|
|
||||||
url = currentFood!!.imgUrl,
|
|
||||||
imageView = binding.ivImg,
|
|
||||||
radius = 12
|
|
||||||
)
|
|
||||||
binding.flPreview.visibility = View.VISIBLE
|
binding.flPreview.visibility = View.VISIBLE
|
||||||
binding.ivImg.visibility = View.VISIBLE
|
binding.ivImg.visibility = View.VISIBLE
|
||||||
binding.flFace.visibility = View.INVISIBLE
|
binding.flFace.visibility = View.INVISIBLE
|
||||||
@@ -256,13 +255,46 @@ class SecondaryScreenPresentation(
|
|||||||
itemUserNutritionBinding.clNutritionData.visibility = View.VISIBLE
|
itemUserNutritionBinding.clNutritionData.visibility = View.VISIBLE
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun updateFoodInfo(foodInfo: FoodInfo) {
|
||||||
|
binding.tvFoodName.text = foodInfo.foodName
|
||||||
|
if (foodInfo.photoUri != null) {
|
||||||
|
GlideUtils.loadRoundCornerImage(
|
||||||
|
context,
|
||||||
|
url = foodInfo.photoUri,
|
||||||
|
imageView = binding.ivImg,
|
||||||
|
radius = 12
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
GlideUtils.loadRoundCornerImage(
|
||||||
|
context,
|
||||||
|
url = foodInfo.imgUrl,
|
||||||
|
imageView = binding.ivImg,
|
||||||
|
radius = 12
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun updateWeight(weight: Double) {
|
fun updateWeight(weight: Double) {
|
||||||
lastWeight = weight * 1000 // 将千克转成克
|
lastWeight = weight * 1000 // 将千克转成克
|
||||||
Timber.d("updateWeight lastWeight = $weight")
|
Timber.d("updateWeight lastWeight = $weight")
|
||||||
|
if (currentStep == 2 &&
|
||||||
|
lastWeight <= 5 &&
|
||||||
|
isGoStep1
|
||||||
|
) {//人脸识别过程中,秤上物品拿走
|
||||||
|
isGoStep1 = false
|
||||||
|
activity.runOnUiThread {
|
||||||
|
Timber.e("切回step1")
|
||||||
|
step1()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if (currentFood == null) {
|
if (currentFood == null) {
|
||||||
Timber.e("updateWeight currentFood is null")
|
Timber.e("updateWeight currentFood is null")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (userNutritionData == null) {
|
if (userNutritionData == null) {
|
||||||
Timber.e("updateWeight userNutritionData is null")
|
Timber.e("updateWeight userNutritionData is null")
|
||||||
return
|
return
|
||||||
@@ -274,15 +306,22 @@ class SecondaryScreenPresentation(
|
|||||||
calculateNutrition(recognitionWeight - lastWeight)
|
calculateNutrition(recognitionWeight - lastWeight)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun updateDetectWeight(weight: Double) {
|
||||||
|
detectWeight = weight * 1000 // 将千克转成克
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 余量取餐
|
* 余量取餐
|
||||||
*/
|
*/
|
||||||
fun updateMealPickupMode(mode: Int) {
|
fun updateMealPickupMode(mode: Int) {
|
||||||
Timber.d("updateMealPickupMode mode = $mode")
|
Timber.d("updateMealPickupMode mode = $mode")
|
||||||
mealPickupMode = mode
|
mealPickupMode = mode
|
||||||
|
step1()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun calculateNutrition(weight: Double) {
|
private fun calculateNutrition(weight1: Double) {
|
||||||
|
var weight = weight1
|
||||||
|
if (weight < 0) weight = 0.0
|
||||||
debouncer.debounce {
|
debouncer.debounce {
|
||||||
Timber.d("calculateNutrition weight = $weight, recognitionWeight = $recognitionWeight")
|
Timber.d("calculateNutrition weight = $weight, recognitionWeight = $recognitionWeight")
|
||||||
val dinnerType = dinnerTypeInfo!!.dinnerType!!.dinnerType!!
|
val dinnerType = dinnerTypeInfo!!.dinnerType!!.dinnerType!!
|
||||||
@@ -308,43 +347,76 @@ class SecondaryScreenPresentation(
|
|||||||
)
|
)
|
||||||
var maxKcal = totalKcal.max
|
var maxKcal = totalKcal.max
|
||||||
Timber.d("maxKcal = $maxKcal")
|
Timber.d("maxKcal = $maxKcal")
|
||||||
|
// 避免0作为被除数
|
||||||
if (maxKcal <= 0.0) {
|
if (maxKcal <= 0.0) {
|
||||||
maxKcal = 100.0
|
maxKcal = 1.0
|
||||||
}
|
}
|
||||||
Timber.d("maxKcal 1 = $maxKcal")
|
Timber.d("maxKcal 1 = $maxKcal")
|
||||||
itemUserNutritionBinding.totalKcalTv.text = totalKcal.current.format2String()
|
itemUserNutritionBinding.totalKcalTv.text = totalKcal.current.format2String()
|
||||||
|
var totalKcalHeight = (totalKcal.current / maxKcal).toFloat()
|
||||||
|
if (totalKcalHeight == 0F) {
|
||||||
|
itemUserNutritionBinding.divKcalView.visibility = View.VISIBLE
|
||||||
|
} else {
|
||||||
|
itemUserNutritionBinding.divKcalView.visibility = View.GONE
|
||||||
|
totalKcalHeight += 0.1F
|
||||||
|
}
|
||||||
itemUserNutritionBinding.customKcalColumn.setCustomHeightPercent(
|
itemUserNutritionBinding.customKcalColumn.setCustomHeightPercent(
|
||||||
(totalKcal.current / maxKcal).toFloat(),
|
totalKcalHeight,
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
|
|
||||||
// 设置主食
|
// 设置主食
|
||||||
itemUserNutritionBinding.customFoodColumn.setImageDrawable(
|
itemUserNutritionBinding.customFoodColumn.setImageDrawable(
|
||||||
false,
|
false,
|
||||||
grain.max < grain.current
|
grain.max < grain.current
|
||||||
)
|
)
|
||||||
itemUserNutritionBinding.totalFoodTv.text = grain.current.format2String()
|
itemUserNutritionBinding.totalFoodTv.text = grain.current.format2String()
|
||||||
|
var grainHeight = (grain.current / columnMax).toFloat()
|
||||||
|
if (grainHeight == 0F) {
|
||||||
|
itemUserNutritionBinding.divTotalFoodView.visibility = View.VISIBLE
|
||||||
|
} else {
|
||||||
|
itemUserNutritionBinding.divTotalFoodView.visibility = View.GONE
|
||||||
|
grainHeight += 0.1F
|
||||||
|
}
|
||||||
itemUserNutritionBinding.customFoodColumn.setCustomHeightPercent(
|
itemUserNutritionBinding.customFoodColumn.setCustomHeightPercent(
|
||||||
(grain.current / columnMax).toFloat(),
|
grainHeight,
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
|
|
||||||
// 设置果蔬
|
// 设置果蔬
|
||||||
itemUserNutritionBinding.customVegetableColumn.setImageDrawable(
|
itemUserNutritionBinding.customVegetableColumn.setImageDrawable(
|
||||||
false,
|
false,
|
||||||
fruits.max < fruits.current
|
fruits.max < fruits.current
|
||||||
)
|
)
|
||||||
itemUserNutritionBinding.totalVegetableTv.text = fruits.current.format2String()
|
itemUserNutritionBinding.totalVegetableTv.text = fruits.current.format2String()
|
||||||
|
var fruitsHeight = (fruits.current / columnMax).toFloat()
|
||||||
|
|
||||||
|
if (fruitsHeight == 0F) {
|
||||||
|
itemUserNutritionBinding.divVegetableView.visibility = View.VISIBLE
|
||||||
|
} else {
|
||||||
|
itemUserNutritionBinding.divVegetableView.visibility = View.GONE
|
||||||
|
fruitsHeight += 0.1F
|
||||||
|
}
|
||||||
itemUserNutritionBinding.customVegetableColumn.setCustomHeightPercent(
|
itemUserNutritionBinding.customVegetableColumn.setCustomHeightPercent(
|
||||||
(fruits.current / columnMax).toFloat(),
|
fruitsHeight,
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
|
|
||||||
// 设置肉蛋
|
// 设置肉蛋
|
||||||
itemUserNutritionBinding.customMeatColumn.setImageDrawable(
|
itemUserNutritionBinding.customMeatColumn.setImageDrawable(
|
||||||
false,
|
false,
|
||||||
meat.max < meat.current
|
meat.max < meat.current
|
||||||
)
|
)
|
||||||
itemUserNutritionBinding.totalMeatTv.text = meat.current.format2String()
|
itemUserNutritionBinding.totalMeatTv.text = meat.current.format2String()
|
||||||
|
var meatHeight = (meat.current / columnMax).toFloat()
|
||||||
|
if (meatHeight == 0F) {
|
||||||
|
itemUserNutritionBinding.divMeatView.visibility = View.VISIBLE
|
||||||
|
} else {
|
||||||
|
itemUserNutritionBinding.divMeatView.visibility = View.GONE
|
||||||
|
meatHeight += 0.1F
|
||||||
|
}
|
||||||
itemUserNutritionBinding.customMeatColumn.setCustomHeightPercent(
|
itemUserNutritionBinding.customMeatColumn.setCustomHeightPercent(
|
||||||
(meat.current / columnMax).toFloat(),
|
meatHeight,
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -464,11 +536,11 @@ class SecondaryScreenPresentation(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 调整View的宽高,使预览显示正常且采集框固定为380x380
|
* 调整View的宽高,使预览显示正常且采集框固定为
|
||||||
*
|
*
|
||||||
* @param rgbPreview RGB预览View
|
* @param rgbPreview RGB预览View
|
||||||
* @param previewView 显示预览数据的view
|
* @param previewView 显示预览数据的view
|
||||||
* @param faceRectView 画框的view (380x380)
|
* @param faceRectView 画框的view
|
||||||
* @param previewSize 预览大小
|
* @param previewSize 预览大小
|
||||||
* @param displayOrientation 相机旋转角度
|
* @param displayOrientation 相机旋转角度
|
||||||
* @param scale 缩放比例
|
* @param scale 缩放比例
|
||||||
@@ -532,26 +604,38 @@ class SecondaryScreenPresentation(
|
|||||||
displayOrientation: Int,
|
displayOrientation: Int,
|
||||||
isMirror: Boolean
|
isMirror: Boolean
|
||||||
) {
|
) {
|
||||||
Timber.d("Rgb onCameraOpened: cameraId = $cameraId, displayOrientation = $displayOrientation")
|
Timber.d("initRgbCamera Rgb onCameraOpened: cameraId = $cameraId, displayOrientation = $displayOrientation")
|
||||||
activity.runOnUiThread({
|
activity.runOnUiThread({
|
||||||
val previewSizeRgb = camera.getParameters().getPreviewSize()
|
val previewSizeRgb = camera.getParameters().getPreviewSize()
|
||||||
val layoutParams = adjustPreviewViewSize(
|
val layoutParams = adjustPreviewViewSize(
|
||||||
binding.dualCameraTexturePreviewRgb,
|
binding.dualCameraTexturePreviewRgb,
|
||||||
binding.dualCameraTexturePreviewRgb, binding.dualCameraFaceRectView,
|
binding.dualCameraTexturePreviewRgb, binding.dualCameraFaceRectView,
|
||||||
previewSizeRgb, displayOrientation, 1f
|
previewSizeRgb, displayOrientation, 0.6F
|
||||||
)
|
)
|
||||||
Timber.d("initRgbCamera previewSizeRgb = ${previewSizeRgb.width}-${previewSizeRgb.height}")
|
Timber.d("initRgbCamera previewSizeRgb = ${previewSizeRgb.width}-${previewSizeRgb.height}")
|
||||||
Timber.d("initRgbCamera layoutParams = ${layoutParams.width}-${layoutParams.height}")
|
Timber.d("initRgbCamera layoutParams = ${layoutParams.width}-${layoutParams.height}")
|
||||||
|
Timber.d(
|
||||||
|
"initRgbCamera isMirror = ${isMirror}, isDrawRgbRectHorizontalMirror = ${
|
||||||
|
ConfigUtil.isDrawRgbRectHorizontalMirror(
|
||||||
|
context
|
||||||
|
)
|
||||||
|
}, isDrawRgbRectVerticalMirror = ${
|
||||||
|
ConfigUtil.isDrawRgbRectVerticalMirror(
|
||||||
|
context
|
||||||
|
)
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
// 调整识别窗口位置
|
||||||
rgbFaceRectTransformer = FaceRectTransformer(
|
rgbFaceRectTransformer = FaceRectTransformer(
|
||||||
previewSizeRgb.width,
|
previewSizeRgb.width,
|
||||||
previewSizeRgb.height,
|
previewSizeRgb.height,
|
||||||
layoutParams.width,
|
layoutParams.width,
|
||||||
layoutParams.height,
|
layoutParams.height,
|
||||||
displayOrientation,
|
90,
|
||||||
cameraId,
|
cameraId,
|
||||||
isMirror,
|
isMirror,
|
||||||
ConfigUtil.isDrawRgbRectHorizontalMirror(context),
|
true,
|
||||||
ConfigUtil.isDrawRgbRectVerticalMirror(context)
|
true
|
||||||
)
|
)
|
||||||
|
|
||||||
recognizeViewModel.onRgbCameraOpened(camera)
|
recognizeViewModel.onRgbCameraOpened(camera)
|
||||||
@@ -574,20 +658,20 @@ class SecondaryScreenPresentation(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onCameraClosed() {
|
override fun onCameraClosed() {
|
||||||
Timber.i("onCameraClosed: ")
|
Timber.i("initRgbCamera onCameraClosed: ")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCameraError(e: java.lang.Exception) {
|
override fun onCameraError(e: java.lang.Exception) {
|
||||||
Timber.i("onCameraError: %s", e.message)
|
Timber.i("initRgbCamera onCameraError: %s", e.message)
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCameraConfigurationChanged(cameraID: Int, displayOrientation: Int) {
|
override fun onCameraConfigurationChanged(cameraID: Int, displayOrientation: Int) {
|
||||||
Timber.i("onCameraConfigurationChanged: threadName = ${Thread.currentThread().name}")
|
Timber.i("initRgbCamera onCameraConfigurationChanged: threadName = ${Thread.currentThread().name}")
|
||||||
if (rgbFaceRectTransformer != null) {
|
if (rgbFaceRectTransformer != null) {
|
||||||
rgbFaceRectTransformer!!.cameraDisplayOrientation = displayOrientation
|
rgbFaceRectTransformer!!.cameraDisplayOrientation = displayOrientation
|
||||||
}
|
}
|
||||||
Timber.i("onCameraConfigurationChanged: $cameraID $displayOrientation")
|
Timber.i("initRgbCamera onCameraConfigurationChanged: $cameraID $displayOrientation")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -626,7 +710,7 @@ class SecondaryScreenPresentation(
|
|||||||
displayOrientation: Int,
|
displayOrientation: Int,
|
||||||
isMirror: Boolean
|
isMirror: Boolean
|
||||||
) {
|
) {
|
||||||
Timber.d("IR onCameraOpened: cameraId = $cameraId, displayOrientation = $displayOrientation")
|
Timber.d("initIrCamera IR onCameraOpened: cameraId = $cameraId, displayOrientation = $displayOrientation")
|
||||||
val previewSizeIr = camera.getParameters().getPreviewSize()
|
val previewSizeIr = camera.getParameters().getPreviewSize()
|
||||||
val layoutParams = adjustPreviewViewSize(
|
val layoutParams = adjustPreviewViewSize(
|
||||||
binding.dualCameraTexturePreviewRgb,
|
binding.dualCameraTexturePreviewRgb,
|
||||||
@@ -651,11 +735,11 @@ class SecondaryScreenPresentation(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onCameraClosed() {
|
override fun onCameraClosed() {
|
||||||
Timber.i("onCameraClosed: ")
|
Timber.i("initIrCamera onCameraClosed: ")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCameraError(e: java.lang.Exception) {
|
override fun onCameraError(e: java.lang.Exception) {
|
||||||
Timber.i("onCameraError: ${e.message}")
|
Timber.i("initIrCamera onCameraError: ${e.message}")
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -666,7 +750,7 @@ class SecondaryScreenPresentation(
|
|||||||
if (irFaceRectTransformer != null) {
|
if (irFaceRectTransformer != null) {
|
||||||
irFaceRectTransformer!!.cameraDisplayOrientation = displayOrientation
|
irFaceRectTransformer!!.cameraDisplayOrientation = displayOrientation
|
||||||
}
|
}
|
||||||
Timber.i("onCameraConfigurationChanged: cameraID = $cameraID, displayOrientation = $displayOrientation")
|
Timber.i("initIrCamera onCameraConfigurationChanged: cameraID = $cameraID, displayOrientation = $displayOrientation")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -712,9 +796,9 @@ class SecondaryScreenPresentation(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (facePreviewInfoList.isEmpty() || (lastFaceTrackId != facePreviewInfoList[0]!!.trackId)) {
|
if (facePreviewInfoList.isEmpty() || (lastFaceTrackId != facePreviewInfoList[0]!!.trackId)) {
|
||||||
if (lastFaceTrackId != 0) {
|
if (lastFaceTrackId != -1) {
|
||||||
Timber.i("$lastFaceTrackId 用户离开")
|
Timber.i("$lastFaceTrackId 用户离开")
|
||||||
lastFaceTrackId = 0
|
lastFaceTrackId = -1
|
||||||
postUserData()
|
postUserData()
|
||||||
if (mealPickupMode == 0) {
|
if (mealPickupMode == 0) {
|
||||||
step1()
|
step1()
|
||||||
@@ -731,12 +815,18 @@ class SecondaryScreenPresentation(
|
|||||||
Timber.d("postUserData userNutritionData = ${userNutritionData == null}, currentFood = ${currentFood == null}")
|
Timber.d("postUserData userNutritionData = ${userNutritionData == null}, currentFood = ${currentFood == null}")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
var eatWeight = 0.0
|
||||||
|
if (mealPickupMode == 0) {//即放即取
|
||||||
|
eatWeight = recognitionWeight - 0
|
||||||
|
} else {
|
||||||
|
eatWeight = recognitionWeight - lastWeight
|
||||||
|
}
|
||||||
val userNutritionParam = UserNutritionParam(
|
val userNutritionParam = UserNutritionParam(
|
||||||
userId = userNutritionData?.userId!!,
|
userId = userNutritionData?.userId!!,
|
||||||
foodId = currentFood?.id!!,
|
foodId = currentFood?.id!!,
|
||||||
faceTime = recognitionTime,
|
faceTime = recognitionTime,
|
||||||
faceEndTime = System.currentTimeMillis(),
|
faceEndTime = System.currentTimeMillis(),
|
||||||
eatWeight = recognitionWeight - lastWeight,
|
eatWeight = eatWeight,//lastWeight
|
||||||
foodWeight = lastWeight
|
foodWeight = lastWeight
|
||||||
)
|
)
|
||||||
viewModel.postUserNutritionData(listOf<UserNutritionParam>(userNutritionParam))
|
viewModel.postUserNutritionData(listOf<UserNutritionParam>(userNutritionParam))
|
||||||
|
|||||||
@@ -7,8 +7,10 @@ import com.sw.dualscreen.model.response.FoodInfo
|
|||||||
import com.sw.dualscreen.model.response.UserNutritionData
|
import com.sw.dualscreen.model.response.UserNutritionData
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import kotlin.math.max
|
import kotlin.math.max
|
||||||
import kotlin.math.min
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户营养信息计算工具
|
||||||
|
*/
|
||||||
object UserNutritionUtils {
|
object UserNutritionUtils {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.sw.dualscreen.receiver
|
||||||
|
|
||||||
|
import android.content.BroadcastReceiver
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import com.sw.dualscreen.activity.InitActivity
|
||||||
|
import timber.log.Timber
|
||||||
|
|
||||||
|
class BootReceiver : BroadcastReceiver() {
|
||||||
|
|
||||||
|
override fun onReceive(context: Context, intent: Intent) {
|
||||||
|
if (Intent.ACTION_BOOT_COMPLETED == intent.action) {
|
||||||
|
Timber.d("设备启动完成,开始执行自启动逻辑")
|
||||||
|
val intent = Intent(context, InitActivity::class.java)
|
||||||
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||||
|
context.startActivity(intent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.sw.dualscreen.repository
|
package com.sw.dualscreen.repository
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.net.Uri
|
||||||
import com.sw.dualscreen.GlobalData
|
import com.sw.dualscreen.GlobalData
|
||||||
import com.sw.dualscreen.model.request.UserNutritionParam
|
import com.sw.dualscreen.model.request.UserNutritionParam
|
||||||
import com.sw.dualscreen.model.response.ApiResponse
|
import com.sw.dualscreen.model.response.ApiResponse
|
||||||
@@ -9,6 +11,9 @@ import com.sw.dualscreen.model.response.FoodInfo
|
|||||||
import com.sw.dualscreen.model.response.UserFaceInfo
|
import com.sw.dualscreen.model.response.UserFaceInfo
|
||||||
import com.sw.dualscreen.model.response.UserNutritionData
|
import com.sw.dualscreen.model.response.UserNutritionData
|
||||||
import com.sw.dualscreen.network.api.ApiService
|
import com.sw.dualscreen.network.api.ApiService
|
||||||
|
import com.sw.dualscreen.utils.ImageUtil
|
||||||
|
import okhttp3.RequestBody
|
||||||
|
import okhttp3.RequestBody.Companion.toRequestBody
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 远程数据处理
|
* 远程数据处理
|
||||||
@@ -103,4 +108,39 @@ class RemoteRepository constructor(
|
|||||||
): ApiResponse<String> {
|
): ApiResponse<String> {
|
||||||
return safeApiCall { apiService.postUserNutritionData(param = param) }
|
return safeApiCall { apiService.postUserNutritionData(param = param) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取菜品信息
|
||||||
|
* @param restId 从device服务获取的canteenId字段
|
||||||
|
* @param foodName 菜品名称,多个使用逗号拼接
|
||||||
|
*/
|
||||||
|
suspend fun getFoodInfo(
|
||||||
|
restId: String = GlobalData.restId,
|
||||||
|
foodName: String,
|
||||||
|
): ApiResponse<List<FoodInfo>> {
|
||||||
|
return safeApiCall { apiService.getFoodInfo(restId = restId, foodName = foodName) }
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 获取菜品信息
|
||||||
|
* @param restId 从device服务获取的canteenId字段
|
||||||
|
* @param foodName 菜品名称,多个使用逗号拼接
|
||||||
|
*/
|
||||||
|
suspend fun postImageData(
|
||||||
|
context: Context,
|
||||||
|
restId: String = GlobalData.restId,
|
||||||
|
foodId: String,
|
||||||
|
foodName: String,
|
||||||
|
foodVector: String,
|
||||||
|
uri: Uri,
|
||||||
|
): ApiResponse<String> {
|
||||||
|
|
||||||
|
val params = HashMap<String, RequestBody>()
|
||||||
|
params["placeId"] = restId.toRequestBody()
|
||||||
|
params["foodId"] = foodId.toRequestBody()
|
||||||
|
params["foodName"] = foodName.toRequestBody()
|
||||||
|
params["foodVector"] = foodVector.toRequestBody()
|
||||||
|
val imagePart = ImageUtil.uriToMultipart(context, uri, "foodPic")
|
||||||
|
|
||||||
|
return safeApiCall { apiService.postImageData(params = params, image = imagePart) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -19,7 +19,7 @@ object SensorScaleUtils {
|
|||||||
private var mSensorScale: SensorScale? = null
|
private var mSensorScale: SensorScale? = null
|
||||||
private var isOpened: Boolean = false
|
private var isOpened: Boolean = false
|
||||||
private var isRead: Boolean = false
|
private var isRead: Boolean = false
|
||||||
private var isZero = false
|
var isZero = false
|
||||||
private var callback: Callback? = {}
|
private var callback: Callback? = {}
|
||||||
private var lastWeight: Double? = null
|
private var lastWeight: Double? = null
|
||||||
|
|
||||||
@@ -74,12 +74,14 @@ object SensorScaleUtils {
|
|||||||
if (open) {
|
if (open) {
|
||||||
// zero()
|
// zero()
|
||||||
if (autoScale) {
|
if (autoScale) {
|
||||||
Thread({
|
Thread {
|
||||||
Thread.sleep(1000)
|
Thread.sleep(1000)
|
||||||
// 打开后需要等待后才能调用,否则会 1001 SDK未初始化
|
// 打开后需要等待后才能调用,否则会 1001 SDK未初始化
|
||||||
mSensorScale?.startContinuousRead()
|
mSensorScale?.startContinuousRead()
|
||||||
|
// Thread.sleep(1000)
|
||||||
|
zero()
|
||||||
|
|
||||||
}).start()
|
}.start()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -114,11 +116,11 @@ object SensorScaleUtils {
|
|||||||
* 零位标定
|
* 零位标定
|
||||||
*/
|
*/
|
||||||
fun zero() {
|
fun zero() {
|
||||||
Timber.d("zero isZero = $isZero")
|
Timber.d("zero isOpened = $isOpened, mSensorScale = $mSensorScale")
|
||||||
if (isZero) return
|
if (!isOpened) return
|
||||||
isZero = true
|
|
||||||
mSensorScale?.zero {
|
mSensorScale?.zero {
|
||||||
Timber.d("零位标定操作成功")
|
isZero = true
|
||||||
|
Timber.d("zero 零位标定操作成功")
|
||||||
ToastUtils.showToast("零位标定操作成功")
|
ToastUtils.showToast("零位标定操作成功")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,76 @@
|
|||||||
|
package com.sw.dualscreen.utils
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.graphics.Bitmap
|
||||||
|
import android.graphics.BitmapFactory
|
||||||
|
import java.io.BufferedReader
|
||||||
|
import java.io.IOException
|
||||||
|
import java.io.InputStreamReader
|
||||||
|
|
||||||
|
|
||||||
|
object AssetsTool {
|
||||||
|
|
||||||
|
// fun readJson(context: Context, fileName: String): List<String> {
|
||||||
|
// val json = readAssetsFile(context, fileName)
|
||||||
|
// val json2 = json.replace("[[", "[").replaceAfterLast("]]", "]")
|
||||||
|
// val list = json2.split("],")
|
||||||
|
// val resultList = mutableListOf<String>()
|
||||||
|
// var count = 0
|
||||||
|
// val tempList = mutableListOf<String>()
|
||||||
|
// list.forEachIndexed { index, text ->
|
||||||
|
// if (count >= 500) {
|
||||||
|
// val tempJson = "[${tempList.joinToString (",")}]"
|
||||||
|
// resultList.add(tempJson)
|
||||||
|
// tempList.clear()
|
||||||
|
// count = 0
|
||||||
|
// }
|
||||||
|
// val newText = if (index == list.size - 1) text else "${text}]"
|
||||||
|
// tempList.add(newText)
|
||||||
|
// count++
|
||||||
|
// }
|
||||||
|
// if (count < 500) {
|
||||||
|
// val tempJson = "[${tempList.joinToString(",")}]"
|
||||||
|
// resultList.add(tempJson)
|
||||||
|
// }
|
||||||
|
// return resultList
|
||||||
|
// }
|
||||||
|
|
||||||
|
fun readAssetsFile(context: Context, fileName: String): String {
|
||||||
|
val stringBuilder = StringBuilder()
|
||||||
|
try {
|
||||||
|
val bf = BufferedReader(InputStreamReader(context.assets.open(fileName)))
|
||||||
|
bf.useLines { lines -> lines.forEach { stringBuilder.append(it) } }
|
||||||
|
} catch (e: IOException) {
|
||||||
|
e.printStackTrace()
|
||||||
|
}
|
||||||
|
return stringBuilder.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun loadImagesFromAssets(context: Context, subPath: String): MutableList<Bitmap> {
|
||||||
|
val bitmaps: MutableList<Bitmap> = mutableListOf()
|
||||||
|
val assetManager = context.assets
|
||||||
|
|
||||||
|
try {
|
||||||
|
val files = assetManager.list(subPath)
|
||||||
|
files?.forEach { file ->
|
||||||
|
assetManager.open("$subPath/$file").use { `is` ->
|
||||||
|
val bitmap = BitmapFactory.decodeStream(`is`)
|
||||||
|
if (bitmap != null) {
|
||||||
|
bitmaps.add(bitmap)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: IOException) {
|
||||||
|
e.printStackTrace()
|
||||||
|
}
|
||||||
|
return bitmaps
|
||||||
|
}
|
||||||
|
|
||||||
|
fun loadImageBitmapFromAssets(context: Context, imagePath:String, action:(bmp: Bitmap)-> Unit) {
|
||||||
|
context.assets.open(imagePath).use { `is` ->
|
||||||
|
val bitmap = BitmapFactory.decodeStream(`is`)
|
||||||
|
action(bitmap)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package com.sw.dualscreen.utils
|
||||||
|
import android.graphics.Bitmap
|
||||||
|
|
||||||
|
object BitmapCropper {
|
||||||
|
/**
|
||||||
|
* 裁剪Bitmap中心区域为指定尺寸
|
||||||
|
* @param original 原始Bitmap
|
||||||
|
* @param targetWidth 目标宽度
|
||||||
|
* @param targetHeight 目标高度
|
||||||
|
* @return 裁剪后的Bitmap
|
||||||
|
*/
|
||||||
|
fun cropCenter(original: Bitmap, targetWidth: Int, targetHeight: Int): Bitmap {
|
||||||
|
val originalWidth = original.width
|
||||||
|
val originalHeight = original.height
|
||||||
|
|
||||||
|
// 计算中心点坐标
|
||||||
|
var startX = (originalWidth - targetWidth) / 2
|
||||||
|
var startY = (originalHeight - targetHeight) / 2
|
||||||
|
|
||||||
|
// 边界检查
|
||||||
|
startX = startX.coerceAtLeast(0)
|
||||||
|
startY = startY.coerceAtLeast(0)
|
||||||
|
val actualWidth = minOf(targetWidth, originalWidth - startX)
|
||||||
|
val actualHeight = minOf(targetHeight, originalHeight - startY)
|
||||||
|
|
||||||
|
return Bitmap.createBitmap(original, startX, startY, actualWidth, actualHeight)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用示例
|
||||||
|
fun main() {
|
||||||
|
// 假设这是从资源加载的Bitmap
|
||||||
|
val originalBitmap = Bitmap.createBitmap(1000, 1000, Bitmap.Config.ARGB_8888)
|
||||||
|
|
||||||
|
// 裁剪中心500x500区域
|
||||||
|
val croppedBitmap = BitmapCropper.cropCenter(originalBitmap, 500, 500)
|
||||||
|
println("裁剪后尺寸:${croppedBitmap.width}x${croppedBitmap.height}")
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package com.sw.dualscreen.utils
|
||||||
|
import android.content.Context
|
||||||
|
import android.graphics.Bitmap
|
||||||
|
import android.os.Environment
|
||||||
|
import java.io.File
|
||||||
|
import java.io.FileOutputStream
|
||||||
|
import java.io.IOException
|
||||||
|
|
||||||
|
object BitmapSaver {
|
||||||
|
// 保存到公共目录(需WRITE_EXTERNAL_STORAGE权限)
|
||||||
|
fun saveToPublicDirectory(
|
||||||
|
bitmap: Bitmap,
|
||||||
|
folderName: String = Environment.DIRECTORY_PICTURES,
|
||||||
|
fileName: String,
|
||||||
|
format: Bitmap.CompressFormat = Bitmap.CompressFormat.PNG,
|
||||||
|
quality: Int = 100
|
||||||
|
): File? {
|
||||||
|
val dir = Environment.getExternalStoragePublicDirectory(folderName)
|
||||||
|
if (!dir.exists()) dir.mkdirs()
|
||||||
|
|
||||||
|
return saveBitmap(bitmap, File(dir, fileName), format, quality)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 保存到应用私有目录(无需权限)
|
||||||
|
fun saveToAppFilesDir(
|
||||||
|
bitmap: Bitmap,
|
||||||
|
context: Context,
|
||||||
|
fileName: String,
|
||||||
|
format: Bitmap.CompressFormat = Bitmap.CompressFormat.JPEG,
|
||||||
|
quality: Int = 100
|
||||||
|
): File? {
|
||||||
|
//val dir = context.getExternalFilesDir(null)
|
||||||
|
val dir = File(context.cacheDir, "crop")
|
||||||
|
if (dir.exists().not()) {
|
||||||
|
dir.mkdirs()
|
||||||
|
}
|
||||||
|
return saveBitmap(bitmap, File(dir, fileName), format, quality)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun saveBitmap(
|
||||||
|
bitmap: Bitmap,
|
||||||
|
outputFile: File,
|
||||||
|
format: Bitmap.CompressFormat,
|
||||||
|
quality: Int
|
||||||
|
): File? {
|
||||||
|
return try {
|
||||||
|
FileOutputStream(outputFile).use { fos ->
|
||||||
|
bitmap.compress(format, quality, fos)
|
||||||
|
fos.flush()
|
||||||
|
}
|
||||||
|
outputFile
|
||||||
|
} catch (e: IOException) {
|
||||||
|
e.printStackTrace()
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
package com.sw.dualscreen.utils
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.net.Uri
|
||||||
|
import android.provider.OpenableColumns
|
||||||
|
import android.widget.Toast
|
||||||
|
import androidx.activity.result.ActivityResultCaller
|
||||||
|
import androidx.activity.result.ActivityResultLauncher
|
||||||
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
|
import androidx.core.content.FileProvider
|
||||||
|
import java.io.File
|
||||||
|
import java.io.FileOutputStream
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 简洁相机工具类
|
||||||
|
* 调用系统相机拍照并返回照片路径
|
||||||
|
*/
|
||||||
|
class CameraHelper(
|
||||||
|
private val context: Context,
|
||||||
|
caller: ActivityResultCaller,
|
||||||
|
private val authority: String,
|
||||||
|
private val onResult: (uri: Uri, filePath: String?) -> Unit
|
||||||
|
) {
|
||||||
|
|
||||||
|
private lateinit var imageUri: Uri
|
||||||
|
private val takePictureLauncher: ActivityResultLauncher<Uri>
|
||||||
|
|
||||||
|
init {
|
||||||
|
takePictureLauncher =
|
||||||
|
caller.registerForActivityResult(ActivityResultContracts.TakePicture()) { success ->
|
||||||
|
if (success) {
|
||||||
|
val path = getRealPathFromUri(context, imageUri)
|
||||||
|
onResult(imageUri, path)
|
||||||
|
} else {
|
||||||
|
Toast.makeText(context, "拍照取消", Toast.LENGTH_SHORT).show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 打开系统相机 */
|
||||||
|
fun openCamera() {
|
||||||
|
// 1. 创建存储图片的文件
|
||||||
|
val photoFile = File(context.externalCacheDir, "IMG_${System.currentTimeMillis()}.jpg")
|
||||||
|
photoFile.parentFile?.mkdirs() // 确保目录存在
|
||||||
|
|
||||||
|
// 2. 生成 content:// Uri
|
||||||
|
imageUri = FileProvider.getUriForFile(context, authority, photoFile)
|
||||||
|
|
||||||
|
// 3. 启动相机
|
||||||
|
takePictureLauncher.launch(imageUri)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 可选:将 content:// 转为实际路径 */
|
||||||
|
private fun getRealPathFromUri(context: Context, uri: Uri): String? {
|
||||||
|
var filePath: String? = null
|
||||||
|
context.contentResolver.query(uri, null, null, null, null)?.use { cursor ->
|
||||||
|
val nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
|
||||||
|
cursor.moveToFirst()
|
||||||
|
val fileName = cursor.getString(nameIndex)
|
||||||
|
val file = File(context.cacheDir, fileName)
|
||||||
|
context.contentResolver.openInputStream(uri)?.use { input ->
|
||||||
|
FileOutputStream(file).use { output -> input.copyTo(output) }
|
||||||
|
}
|
||||||
|
filePath = file.absolutePath
|
||||||
|
}
|
||||||
|
return filePath
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
package com.sw.dualscreen.utils
|
||||||
|
|
||||||
|
import android.net.Uri
|
||||||
|
import androidx.activity.ComponentActivity
|
||||||
|
import androidx.camera.core.CameraSelector
|
||||||
|
import androidx.camera.view.CameraController
|
||||||
|
import androidx.camera.view.LifecycleCameraController
|
||||||
|
import androidx.camera.view.PreviewView
|
||||||
|
|
||||||
|
class CameraUtils(private var activity: ComponentActivity) {
|
||||||
|
|
||||||
|
private var cameraController: LifecycleCameraController? = null
|
||||||
|
private var photoCaptureHelper: PhotoCaptureHelper? = null
|
||||||
|
|
||||||
|
// private var isCameraReady = false
|
||||||
|
fun takePhoto(callback: (Uri) -> Unit) {
|
||||||
|
cameraController?.let {
|
||||||
|
if (photoCaptureHelper == null) {
|
||||||
|
initCaptureHelper()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
photoCaptureHelper?.let {
|
||||||
|
it.addSuccessCallback(callback)
|
||||||
|
it.bindCameraCallback {
|
||||||
|
bind()
|
||||||
|
}
|
||||||
|
it.takePhoto()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun initCaptureHelper() {
|
||||||
|
photoCaptureHelper = PhotoCaptureHelper(
|
||||||
|
context = activity,
|
||||||
|
cameraController = cameraController!!,
|
||||||
|
onSuccess = {},
|
||||||
|
onError = { msg ->
|
||||||
|
//toast(msg)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setPreviewController(previewView: PreviewView?) {
|
||||||
|
if (previewView?.controller == null) {
|
||||||
|
previewView?.controller = cameraController
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun initCamera() {
|
||||||
|
if (cameraController == null) {
|
||||||
|
cameraController = LifecycleCameraController(activity).apply {
|
||||||
|
// 必须设置有效的用例
|
||||||
|
setEnabledUseCases(
|
||||||
|
CameraController.IMAGE_CAPTURE
|
||||||
|
// or CameraController.VIDEO_CAPTURE
|
||||||
|
)
|
||||||
|
cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
|
||||||
|
}
|
||||||
|
bind()
|
||||||
|
}
|
||||||
|
|
||||||
|
// if (isCameraReady.not()) {
|
||||||
|
// try {
|
||||||
|
// cameraController!!.initializationFuture.addListener({
|
||||||
|
// isCameraReady = true
|
||||||
|
// Timber.d("Camera initialized successfully")
|
||||||
|
// }, ContextCompat.getMainExecutor(this))
|
||||||
|
// } catch (e: Exception) {
|
||||||
|
// Timber.d("Camera initialized error = ${e.message}")
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun bind() {
|
||||||
|
cameraController?.bindToLifecycle(activity)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun unbind() {
|
||||||
|
cameraController?.unbind()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,246 @@
|
|||||||
|
package com.sw.dualscreen.utils
|
||||||
|
|
||||||
|
import android.app.ActivityManager
|
||||||
|
import android.app.AlarmManager
|
||||||
|
import android.app.PendingIntent
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.os.Build
|
||||||
|
import android.os.Handler
|
||||||
|
import android.os.Looper
|
||||||
|
import android.os.Process
|
||||||
|
import com.sw.dualscreen.activity.MainActivity
|
||||||
|
import timber.log.Timber
|
||||||
|
import java.io.File
|
||||||
|
import java.io.FileOutputStream
|
||||||
|
import java.io.IOException
|
||||||
|
import java.io.PrintWriter
|
||||||
|
import java.io.StringWriter
|
||||||
|
import java.text.SimpleDateFormat
|
||||||
|
import java.util.Date
|
||||||
|
import java.util.Locale
|
||||||
|
import kotlin.apply
|
||||||
|
import kotlin.collections.forEach
|
||||||
|
import kotlin.io.bufferedReader
|
||||||
|
import kotlin.io.readLines
|
||||||
|
import kotlin.io.use
|
||||||
|
import kotlin.jvm.java
|
||||||
|
import kotlin.run
|
||||||
|
import kotlin.system.exitProcess
|
||||||
|
import kotlin.text.endsWith
|
||||||
|
import kotlin.text.toByteArray
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 崩溃处理
|
||||||
|
*/
|
||||||
|
class CrashHandler private constructor(private val context: Context) :
|
||||||
|
Thread.UncaughtExceptionHandler {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val TAG = "CrashHandler"
|
||||||
|
private const val CRASH_REPORTS_DIR = "crash_reports"
|
||||||
|
private const val LOG_LINES = 500 // 收集最近500行日志
|
||||||
|
|
||||||
|
@Volatile
|
||||||
|
private var instance: CrashHandler? = null
|
||||||
|
|
||||||
|
fun init(context: Context) {
|
||||||
|
if (instance == null) {
|
||||||
|
synchronized(CrashHandler::class.java) {
|
||||||
|
if (instance == null) {
|
||||||
|
instance = CrashHandler(context.applicationContext)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getCrashReportFiles(context: Context): Array<File> {
|
||||||
|
val crashDir = File(context.getExternalFilesDir(null), CRASH_REPORTS_DIR)
|
||||||
|
return if (crashDir.exists() && crashDir.isDirectory) {
|
||||||
|
crashDir.listFiles { _, name -> name.endsWith(".log") } ?: emptyArray()
|
||||||
|
} else {
|
||||||
|
emptyArray()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun clearCrashReports(context: Context) {
|
||||||
|
getCrashReportFiles(context).forEach { it.delete() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private val defaultHandler = Thread.getDefaultUncaughtExceptionHandler()
|
||||||
|
|
||||||
|
init {
|
||||||
|
Thread.setDefaultUncaughtExceptionHandler(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun uncaughtException(thread: Thread, ex: Throwable) {
|
||||||
|
handleException(thread, ex)
|
||||||
|
|
||||||
|
// 如果系统提供了默认的异常处理器,则交给系统去结束程序
|
||||||
|
// 否则自己结束程序
|
||||||
|
defaultHandler?.uncaughtException(thread, ex) ?: run {
|
||||||
|
Process.killProcess(Process.myPid())
|
||||||
|
exitProcess(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自动重启app
|
||||||
|
*/
|
||||||
|
private fun restartApp() {
|
||||||
|
// 延迟1秒后重启应用
|
||||||
|
Handler(Looper.getMainLooper()).postDelayed({
|
||||||
|
val intent = Intent(context, MainActivity::class.java).apply {
|
||||||
|
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||||
|
}
|
||||||
|
|
||||||
|
val pendingIntent = PendingIntent.getActivity(
|
||||||
|
context, 0, intent,
|
||||||
|
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
|
||||||
|
)
|
||||||
|
|
||||||
|
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
||||||
|
alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + 100, pendingIntent)
|
||||||
|
|
||||||
|
Process.killProcess(Process.myPid())
|
||||||
|
exitProcess(1)
|
||||||
|
}, 1000)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun handleException(thread: Thread, ex: Throwable) {
|
||||||
|
// 收集设备信息和异常信息
|
||||||
|
val crashInfo = collectCrashInfo(thread, ex)
|
||||||
|
|
||||||
|
// 保存日志文件
|
||||||
|
saveCrashInfoToFile(crashInfo)
|
||||||
|
|
||||||
|
// 这里可以添加其他处理逻辑,比如上传到服务器等
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun collectCrashInfo(thread: Thread, ex: Throwable): String {
|
||||||
|
return buildString {
|
||||||
|
// 收集设备信息
|
||||||
|
collectDeviceInfo(this)
|
||||||
|
|
||||||
|
// 收集应用日志
|
||||||
|
append("\n\n").append(collectLogs())
|
||||||
|
|
||||||
|
// 收集线程和异常信息
|
||||||
|
append("\n\n========== Thread & Exception Info ==========\n")
|
||||||
|
append("Thread: ${thread.name}\n")
|
||||||
|
append("Stack Trace:\n")
|
||||||
|
|
||||||
|
val sw = StringWriter()
|
||||||
|
val pw = PrintWriter(sw)
|
||||||
|
ex.printStackTrace(pw)
|
||||||
|
var cause: Throwable? = ex.cause
|
||||||
|
while (cause != null) {
|
||||||
|
cause.printStackTrace(pw)
|
||||||
|
cause = cause.cause
|
||||||
|
}
|
||||||
|
pw.close()
|
||||||
|
append(sw.toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun collectDeviceInfo(sb: StringBuilder) {
|
||||||
|
sb.append("========== Device Info ==========\n")
|
||||||
|
try {
|
||||||
|
// 应用信息
|
||||||
|
val pm = context.packageManager
|
||||||
|
val pi = pm.getPackageInfo(context.packageName, 0)
|
||||||
|
|
||||||
|
sb.append("App Version: ${pi.versionName}_${pi.versionCode}\n")
|
||||||
|
|
||||||
|
// Android 版本信息
|
||||||
|
sb.append("OS Version: ${Build.VERSION.RELEASE}_${Build.VERSION.SDK_INT}\n")
|
||||||
|
|
||||||
|
// 设备信息
|
||||||
|
sb.append("Vendor: ${Build.MANUFACTURER}\n")
|
||||||
|
sb.append("Model: ${Build.MODEL}\n")
|
||||||
|
sb.append("CPU ABI: ${Build.SUPPORTED_ABIS[0]}\n")
|
||||||
|
|
||||||
|
// 其他信息
|
||||||
|
sb.append("Locale: ${Locale.getDefault()}\n")
|
||||||
|
sb.append(
|
||||||
|
"Current Time: ${
|
||||||
|
SimpleDateFormat(
|
||||||
|
"yyyy-MM-dd HH:mm:ss",
|
||||||
|
Locale.getDefault()
|
||||||
|
).format(Date())
|
||||||
|
}\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 内存信息
|
||||||
|
val memoryInfo = ActivityManager.MemoryInfo()
|
||||||
|
val activityManager =
|
||||||
|
context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
|
||||||
|
activityManager.getMemoryInfo(memoryInfo)
|
||||||
|
sb.append("Available Memory: ${memoryInfo.availMem / (1024 * 1024)}MB\n")
|
||||||
|
sb.append("Total Memory: ${memoryInfo.totalMem / (1024 * 1024)}MB\n")
|
||||||
|
sb.append("Low Memory: ${memoryInfo.lowMemory}\n")
|
||||||
|
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Timber.e(e, "Error while collecting device info")
|
||||||
|
sb.append("Error while collecting device info: ${e.message}\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun collectLogs(): String {
|
||||||
|
return buildString {
|
||||||
|
append("========== Application Logs ==========\n")
|
||||||
|
try {
|
||||||
|
val process = Runtime.getRuntime().exec("logcat -d -v threadtime")
|
||||||
|
val reader = process.inputStream.bufferedReader()
|
||||||
|
|
||||||
|
val logLines = reader.readLines()
|
||||||
|
val start = kotlin.comparisons.maxOf(0, logLines.size - LOG_LINES)
|
||||||
|
|
||||||
|
logLines.subList(start, logLines.size).forEach {
|
||||||
|
append(it).append("\n")
|
||||||
|
}
|
||||||
|
} catch (e: IOException) {
|
||||||
|
Timber.e(e, "Error collecting logs")
|
||||||
|
append("Error collecting logs: ${e.message}\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun saveCrashInfoToFile(crashInfo: String) {
|
||||||
|
try {
|
||||||
|
val time = SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.getDefault()).format(Date())
|
||||||
|
val fileName = "crash_$time.log"
|
||||||
|
|
||||||
|
val crashDir = File(context.getExternalFilesDir(null), CRASH_REPORTS_DIR)
|
||||||
|
if (!crashDir.exists() && !crashDir.mkdirs()) {
|
||||||
|
Timber.tag(TAG).e("Failed to create crash report directory")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val crashFile = File(crashDir, fileName)
|
||||||
|
FileOutputStream(crashFile).use { it.write(crashInfo.toByteArray()) }
|
||||||
|
|
||||||
|
Timber.tag(TAG).d("Crash info saved to: ${crashFile.absolutePath}")
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Timber.tag(TAG).e(e, "Error saving crash info to file")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清理旧的崩溃日志
|
||||||
|
*/
|
||||||
|
fun cleanupOldCrashReports(maxAgeDays: Int = 7) {
|
||||||
|
val crashDir = File(context.getExternalFilesDir(null), CRASH_REPORTS_DIR)
|
||||||
|
if (!crashDir.exists() || !crashDir.isDirectory) return
|
||||||
|
|
||||||
|
val now = System.currentTimeMillis()
|
||||||
|
val maxAgeMillis = maxAgeDays * 24 * 60 * 60 * 1000L
|
||||||
|
|
||||||
|
crashDir.listFiles()?.forEach { file ->
|
||||||
|
if (file.lastModified() < now - maxAgeMillis) {
|
||||||
|
file.delete()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,9 @@
|
|||||||
package com.sw.dualscreen.utils
|
package com.sw.dualscreen.utils
|
||||||
|
|
||||||
class Debouncer(private val delayMillis: Long) {
|
/**
|
||||||
|
* 防抖工具
|
||||||
|
*/
|
||||||
|
class Debouncer(private val delayMillis: Long = 2000) {
|
||||||
private var lastActionTime = 0L
|
private var lastActionTime = 0L
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,156 +0,0 @@
|
|||||||
package com.sw.dualscreen.utils
|
|
||||||
|
|
||||||
import android.content.ContentUris
|
|
||||||
import android.content.Context
|
|
||||||
import android.net.Uri
|
|
||||||
import android.os.Build
|
|
||||||
import android.provider.MediaStore
|
|
||||||
import androidx.annotation.RequiresApi
|
|
||||||
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
|
||||||
import okhttp3.MultipartBody
|
|
||||||
import okhttp3.RequestBody.Companion.asRequestBody
|
|
||||||
import timber.log.Timber
|
|
||||||
import java.io.File
|
|
||||||
|
|
||||||
object FileUtils {
|
|
||||||
/**
|
|
||||||
* 从Uri获取File
|
|
||||||
* example: file:///data/user/0/com.sw.inbound/cache/IMG_17515262353556856678814444882273.jpg
|
|
||||||
*/
|
|
||||||
private fun getFileFromUri(context: Context, uri: Uri): File? {
|
|
||||||
Timber.d("getFileFromUri uri = ${uri.scheme}")
|
|
||||||
return when (uri.scheme) {
|
|
||||||
"file" -> File(uri.path ?: return null)
|
|
||||||
"content" -> {
|
|
||||||
try {
|
|
||||||
val inputStream = context.contentResolver.openInputStream(uri) ?: return null
|
|
||||||
val cacheDir = context.cacheDir
|
|
||||||
val file = File.createTempFile(
|
|
||||||
"upload_${System.currentTimeMillis()}",
|
|
||||||
".jpg",
|
|
||||||
cacheDir
|
|
||||||
)
|
|
||||||
file.outputStream().use { output ->
|
|
||||||
inputStream.copyTo(output)
|
|
||||||
}
|
|
||||||
file
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Timber.e(e)
|
|
||||||
null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
else -> null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 通过uri生成http请求体
|
|
||||||
*/
|
|
||||||
fun genRequestPart(context: Context, imageUri: Uri): MultipartBody.Part? {
|
|
||||||
Timber.d("genRequestPart imageUri = $imageUri")
|
|
||||||
// 1. 从Uri获取文件
|
|
||||||
val file = getFileFromUri(context, imageUri)
|
|
||||||
if (file == null) {
|
|
||||||
Timber.e("getFileFromUri file is null")
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
// 2. 创建请求体
|
|
||||||
val requestFile = file
|
|
||||||
.asRequestBody("application/octet-stream".toMediaTypeOrNull())
|
|
||||||
val imagePart = MultipartBody.Part.createFormData(
|
|
||||||
"file",
|
|
||||||
file.name,
|
|
||||||
requestFile
|
|
||||||
)
|
|
||||||
return imagePart
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 通过Uri删除文件
|
|
||||||
* @param context 上下文
|
|
||||||
* @param uri 文件Uri
|
|
||||||
* @return Boolean 是否删除成功
|
|
||||||
*/
|
|
||||||
fun deleteFileWithUri(context: Context, uri: Uri): Boolean {
|
|
||||||
Timber.d("deleteFileWithUri uri = ${uri.scheme}")
|
|
||||||
return when {
|
|
||||||
// 1. 处理 content:// 类型的Uri (MediaStore)
|
|
||||||
uri.scheme.equals("content", ignoreCase = true) -> {
|
|
||||||
deleteContentUriFile(context, uri)
|
|
||||||
}
|
|
||||||
// 2. 处理 file:// 类型的Uri
|
|
||||||
uri.scheme.equals("file", ignoreCase = true) -> {
|
|
||||||
deleteFileUriFile(uri)
|
|
||||||
}
|
|
||||||
// 3. 其他情况尝试直接解析路径
|
|
||||||
else -> {
|
|
||||||
deleteFileFromPath(uri.path ?: return false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 删除Content Uri文件
|
|
||||||
private fun deleteContentUriFile(context: Context, uri: Uri): Boolean {
|
|
||||||
Timber.d("deleteContentUriFile uri = ${uri.scheme}")
|
|
||||||
return try {
|
|
||||||
context.contentResolver.delete(uri, null, null) > 0
|
|
||||||
} catch (e: SecurityException) {
|
|
||||||
// Android 10+需要特殊处理
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
||||||
deleteMediaStoreFile(context, uri)
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Timber.e(e)
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Android 10+删除MediaStore文件
|
|
||||||
@RequiresApi(Build.VERSION_CODES.Q)
|
|
||||||
private fun deleteMediaStoreFile(context: Context, uri: Uri): Boolean {
|
|
||||||
Timber.d("deleteMediaStoreFile uri = ${uri.scheme}")
|
|
||||||
val contentResolver = context.contentResolver
|
|
||||||
val projection = arrayOf(MediaStore.MediaColumns._ID)
|
|
||||||
|
|
||||||
return try {
|
|
||||||
contentResolver.query(uri, projection, null, null, null)?.use { cursor ->
|
|
||||||
if (cursor.moveToFirst()) {
|
|
||||||
val id =
|
|
||||||
cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns._ID))
|
|
||||||
val contentUri = ContentUris.withAppendedId(uri, id)
|
|
||||||
contentResolver.delete(contentUri, null, null) > 0
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
} ?: false
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Timber.e(e)
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 删除File Uri文件
|
|
||||||
private fun deleteFileUriFile(uri: Uri): Boolean {
|
|
||||||
Timber.d("deleteFileUriFile uri = $uri")
|
|
||||||
return try {
|
|
||||||
File(uri.path ?: return false).delete()
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Timber.e(e)
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 直接通过路径删除文件
|
|
||||||
private fun deleteFileFromPath(path: String): Boolean {
|
|
||||||
Timber.d("deleteFileFromPath path = $path")
|
|
||||||
return try {
|
|
||||||
File(path).delete()
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Timber.e(e)
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package com.example.utils
|
||||||
|
|
||||||
|
import android.util.Base64
|
||||||
|
import java.nio.ByteBuffer
|
||||||
|
import java.nio.ByteOrder
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 工具类:float[] 与 Base64 字符串互转
|
||||||
|
* 无需外部依赖,适用于 Android 原生项目。
|
||||||
|
*/
|
||||||
|
object FloatBase64Utils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* float数组 → Base64字符串
|
||||||
|
* @param values FloatArray
|
||||||
|
* @return Base64字符串(无换行)
|
||||||
|
*/
|
||||||
|
fun floatArrayToBase64(values: FloatArray): String {
|
||||||
|
if (values.isEmpty()) return ""
|
||||||
|
|
||||||
|
val buffer = ByteBuffer.allocate(values.size * 4)
|
||||||
|
buffer.order(ByteOrder.BIG_ENDIAN) // 默认大端序,可改 LITTLE_ENDIAN
|
||||||
|
values.forEach { buffer.putFloat(it) }
|
||||||
|
|
||||||
|
val bytes = buffer.array()
|
||||||
|
return Base64.encodeToString(bytes, Base64.NO_WRAP)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base64字符串 → float数组
|
||||||
|
* @param base64 Base64字符串
|
||||||
|
* @return FloatArray
|
||||||
|
*/
|
||||||
|
fun base64ToFloatArray(base64: String?): FloatArray {
|
||||||
|
if (base64.isNullOrEmpty()) return FloatArray(0)
|
||||||
|
|
||||||
|
val bytes = Base64.decode(base64, Base64.NO_WRAP)
|
||||||
|
val buffer = ByteBuffer.wrap(bytes)
|
||||||
|
buffer.order(ByteOrder.BIG_ENDIAN)
|
||||||
|
|
||||||
|
val result = FloatArray(bytes.size / 4)
|
||||||
|
for (i in result.indices) {
|
||||||
|
result[i] = buffer.getFloat()
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package com.sw.dualscreen.utils
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.graphics.Bitmap
|
||||||
|
import android.graphics.BitmapFactory
|
||||||
|
import android.net.Uri
|
||||||
|
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
||||||
|
import okhttp3.MultipartBody
|
||||||
|
import okhttp3.RequestBody
|
||||||
|
import java.io.File
|
||||||
|
import java.io.FileOutputStream
|
||||||
|
import java.io.InputStream
|
||||||
|
|
||||||
|
object ImageUtil {
|
||||||
|
|
||||||
|
fun uriToBitmap(context: Context, uri: Uri): Bitmap? {
|
||||||
|
return try {
|
||||||
|
context.contentResolver.openInputStream(uri)?.use { stream ->
|
||||||
|
BitmapFactory.decodeStream(stream)
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun uriToMultipart(context: Context, uri: Uri, partName: String): MultipartBody.Part? {
|
||||||
|
try {
|
||||||
|
val contentResolver = context.contentResolver
|
||||||
|
val inputStream: InputStream = contentResolver.openInputStream(uri) ?: return null
|
||||||
|
|
||||||
|
// 临时文件(避免直接访问SAF文件)
|
||||||
|
val tempFile = File.createTempFile("upload_", ".jpg", context.cacheDir)
|
||||||
|
|
||||||
|
val outputStream = FileOutputStream(tempFile)
|
||||||
|
inputStream.copyTo(outputStream)
|
||||||
|
inputStream.close()
|
||||||
|
outputStream.close()
|
||||||
|
|
||||||
|
val requestFile = RequestBody.create("image/*".toMediaTypeOrNull(), tempFile)
|
||||||
|
return MultipartBody.Part.createFormData(partName, tempFile.name, requestFile)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package com.sw.dualscreen.utils
|
||||||
|
|
||||||
|
import android.app.Activity
|
||||||
|
import android.content.Context
|
||||||
|
import android.view.View
|
||||||
|
import android.view.inputmethod.EditorInfo
|
||||||
|
import android.view.inputmethod.InputMethodManager
|
||||||
|
import android.widget.EditText
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 键盘工具类
|
||||||
|
*/
|
||||||
|
object KeyboardUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 显示键盘
|
||||||
|
*/
|
||||||
|
fun showKeyboard(editText: EditText) {
|
||||||
|
editText.requestFocus()
|
||||||
|
val imm = editText.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
|
||||||
|
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 隐藏键盘
|
||||||
|
*/
|
||||||
|
fun hideKeyboard(activity: Activity) {
|
||||||
|
val view = activity.currentFocus ?: activity.window.decorView
|
||||||
|
hideKeyboard(view)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 隐藏键盘
|
||||||
|
*/
|
||||||
|
fun hideKeyboard(view: View) {
|
||||||
|
val imm = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
|
||||||
|
imm.hideSoftInputFromWindow(view.windowToken, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为EditText设置EditorAction监听并隐藏键盘
|
||||||
|
*/
|
||||||
|
fun setupEditorAction(editText: EditText, onAction: (() -> Unit)? = null) {
|
||||||
|
editText.setOnEditorActionListener { _, actionId, _ ->
|
||||||
|
if (actionId == EditorInfo.IME_ACTION_DONE ||
|
||||||
|
actionId == EditorInfo.IME_ACTION_SEARCH ||
|
||||||
|
actionId == EditorInfo.IME_ACTION_GO ||
|
||||||
|
actionId == EditorInfo.IME_ACTION_SEND ||
|
||||||
|
actionId == EditorInfo.IME_ACTION_NEXT) {
|
||||||
|
|
||||||
|
hideKeyboard(editText)
|
||||||
|
onAction?.invoke()
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
package com.sw.dualscreen.utils
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.net.Uri
|
||||||
|
import androidx.camera.core.ImageCapture
|
||||||
|
import androidx.camera.core.ImageCaptureException
|
||||||
|
import androidx.camera.view.CameraController
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
|
import timber.log.Timber
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 拍照工具类
|
||||||
|
* @param context Context 上下文
|
||||||
|
* @param cameraController CameraController 相机控制器
|
||||||
|
* @param onSuccess (Uri) -> Unit 拍照成功回调
|
||||||
|
* @param onError (String) -> Unit 拍照失败回调
|
||||||
|
*/
|
||||||
|
class PhotoCaptureHelper(
|
||||||
|
private val context: Context,
|
||||||
|
private val cameraController: CameraController,
|
||||||
|
private val onSuccess: (Uri) -> Unit = {},
|
||||||
|
private val onError: (String) -> Unit = {}
|
||||||
|
) {
|
||||||
|
private val callbackList: MutableList<(Uri) -> Unit> = mutableListOf()
|
||||||
|
fun addSuccessCallback(callback:(Uri) -> Unit) {
|
||||||
|
if (callbackList.contains(callback).not()) {
|
||||||
|
callbackList.add(callback)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private var bindCamera:(()->Unit)?=null
|
||||||
|
fun bindCameraCallback(callback:()->Unit) {
|
||||||
|
this.bindCamera = callback
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 拍照方法
|
||||||
|
* @param fileNamePrefix 文件名前缀,默认为"IMG_"
|
||||||
|
* @param fileExtension 文件扩展名,默认为".jpg"
|
||||||
|
*/
|
||||||
|
fun takePhoto(
|
||||||
|
fileNamePrefix: String = "IMG_",
|
||||||
|
fileExtension: String = ".jpg"
|
||||||
|
) {
|
||||||
|
Timber.d("开始拍照采集")
|
||||||
|
|
||||||
|
try {
|
||||||
|
val executor = ContextCompat.getMainExecutor(context)
|
||||||
|
val cacheDir = context.cacheDir
|
||||||
|
val photoFile = File.createTempFile(
|
||||||
|
"${fileNamePrefix}${System.currentTimeMillis()}",
|
||||||
|
fileExtension,
|
||||||
|
cacheDir
|
||||||
|
)
|
||||||
|
|
||||||
|
val outputOptions = ImageCapture.OutputFileOptions.Builder(photoFile).build()
|
||||||
|
|
||||||
|
cameraController.takePicture(
|
||||||
|
outputOptions,
|
||||||
|
executor,
|
||||||
|
object : ImageCapture.OnImageSavedCallback {
|
||||||
|
override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) {
|
||||||
|
val photoUri = outputFileResults.savedUri ?: Uri.fromFile(photoFile)
|
||||||
|
Timber.d("照片保存成功: $photoUri")
|
||||||
|
onSuccess(photoUri)
|
||||||
|
callbackList.forEach {
|
||||||
|
it(photoUri)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onError(exception: ImageCaptureException) {
|
||||||
|
val errorMsg = "拍照失败: ${exception.message}"
|
||||||
|
if (errorMsg.contains("Not bound to a valid Camera")) {
|
||||||
|
if (bindCamera != null) {
|
||||||
|
bindCamera?.invoke()
|
||||||
|
//takePhoto()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
Timber.e(exception, errorMsg)
|
||||||
|
onError(errorMsg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
val errorMsg = "创建临时文件失败: ${e.message}"
|
||||||
|
Timber.e(e, errorMsg)
|
||||||
|
onError(errorMsg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.sw.inbound.utils
|
package com.sw.dualscreen.utils
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import androidx.core.content.edit
|
import androidx.core.content.edit
|
||||||
@@ -9,6 +9,9 @@ import kotlinx.coroutines.flow.map
|
|||||||
import kotlin.properties.ReadWriteProperty
|
import kotlin.properties.ReadWriteProperty
|
||||||
import kotlin.reflect.KProperty
|
import kotlin.reflect.KProperty
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 持久化工具
|
||||||
|
*/
|
||||||
class SPUtil private constructor(context: Context, private val spName: String) {
|
class SPUtil private constructor(context: Context, private val spName: String) {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package com.sw.dualscreen.utils
|
||||||
|
|
||||||
|
import android.Manifest
|
||||||
|
import com.sw.dualscreen.activity.BaseActivity
|
||||||
|
|
||||||
|
object StorageUtils {
|
||||||
|
|
||||||
|
fun requestPermission(activity: BaseActivity<*>, callback: () -> Unit) {
|
||||||
|
activity.requestSinglePermissionResult(Manifest.permission.WRITE_EXTERNAL_STORAGE) {
|
||||||
|
callback()
|
||||||
|
}
|
||||||
|
// activity.requestSinglePermissionResult(Manifest.permission.CAMERA) {
|
||||||
|
//// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||||
|
//// if (Environment.isExternalStorageManager()) {
|
||||||
|
//// callback()
|
||||||
|
//// } else {
|
||||||
|
//// val intent = Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION)
|
||||||
|
////// activity.startActivityForResult(intent, MANAGE_REQUEST_CODE)
|
||||||
|
//// activity.launchActivityResult(intent) {
|
||||||
|
//// callback()
|
||||||
|
//// }
|
||||||
|
//// }
|
||||||
|
//// } else {
|
||||||
|
//
|
||||||
|
//// }
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun requestCameraPermission(activity: BaseActivity<*>, callback: () -> Unit) {
|
||||||
|
activity.requestSinglePermissionResult(Manifest.permission.CAMERA) {
|
||||||
|
callback()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,7 +8,6 @@ import android.os.Bundle
|
|||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import android.view.inputmethod.EditorInfo
|
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import androidx.recyclerview.widget.GridLayoutManager
|
import androidx.recyclerview.widget.GridLayoutManager
|
||||||
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
|
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
|
||||||
@@ -20,10 +19,14 @@ import com.sw.dualscreen.databinding.BottomSheetDialogBinding
|
|||||||
import com.sw.dualscreen.databinding.ItemSearchFoodInfoBinding
|
import com.sw.dualscreen.databinding.ItemSearchFoodInfoBinding
|
||||||
import com.sw.dualscreen.model.response.FoodInfo
|
import com.sw.dualscreen.model.response.FoodInfo
|
||||||
import com.sw.dualscreen.utils.Debouncer
|
import com.sw.dualscreen.utils.Debouncer
|
||||||
|
import com.sw.dualscreen.utils.KeyboardUtils
|
||||||
import com.sw.dualscreen.viewmodel.UserViewModel
|
import com.sw.dualscreen.viewmodel.UserViewModel
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 底部搜索结果弹窗
|
||||||
|
*/
|
||||||
class CustomBottomSheetDialog(
|
class CustomBottomSheetDialog(
|
||||||
val viewModel: UserViewModel,
|
val viewModel: UserViewModel,
|
||||||
val itemClickCallback: (FoodInfo) -> Unit
|
val itemClickCallback: (FoodInfo) -> Unit
|
||||||
@@ -95,13 +98,8 @@ class CustomBottomSheetDialog(
|
|||||||
binding.ivSearch.setOnClickListener {
|
binding.ivSearch.setOnClickListener {
|
||||||
searchInfo()
|
searchInfo()
|
||||||
}
|
}
|
||||||
binding.etSearch.setOnEditorActionListener { _, actionId, _ ->
|
KeyboardUtils.setupEditorAction(binding.etSearch) {
|
||||||
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
|
|
||||||
searchInfo()
|
searchInfo()
|
||||||
true
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
registerDataChange()
|
registerDataChange()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,9 @@ import android.view.View;
|
|||||||
import android.view.Window;
|
import android.view.Window;
|
||||||
import android.view.WindowManager;
|
import android.view.WindowManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加载进度条弹窗
|
||||||
|
*/
|
||||||
public class CustomDialog extends Dialog {
|
public class CustomDialog extends Dialog {
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import android.graphics.Paint;
|
|||||||
import android.graphics.Rect;
|
import android.graphics.Rect;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.view.animation.AlphaAnimation;
|
import android.view.animation.AlphaAnimation;
|
||||||
|
|
||||||
@@ -17,6 +18,9 @@ import com.sw.plate.utils.AppUtil;
|
|||||||
|
|
||||||
import timber.log.Timber;
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义柱状图
|
||||||
|
*/
|
||||||
public class CustomImageView extends androidx.appcompat.widget.AppCompatImageView {
|
public class CustomImageView extends androidx.appcompat.widget.AppCompatImageView {
|
||||||
private AlphaAnimation alphaAnimation;
|
private AlphaAnimation alphaAnimation;
|
||||||
private String textToDraw;
|
private String textToDraw;
|
||||||
@@ -107,9 +111,8 @@ public class CustomImageView extends androidx.appcompat.widget.AppCompatImageVie
|
|||||||
// 用于设置自定义高度的方法
|
// 用于设置自定义高度的方法
|
||||||
public void setCustomHeight(int height, boolean isAnimation) {
|
public void setCustomHeight(int height, boolean isAnimation) {
|
||||||
Timber.d("setCustomHeight height = %s", height);
|
Timber.d("setCustomHeight height = %s", height);
|
||||||
if (height > 0) {
|
|
||||||
height += 30;
|
setVisibility(height == 0 ? View.GONE : VISIBLE);
|
||||||
}
|
|
||||||
if (height > MAX_HEIGHT){
|
if (height > MAX_HEIGHT){
|
||||||
height = MAX_HEIGHT;
|
height = MAX_HEIGHT;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,9 @@ import androidx.appcompat.widget.AppCompatSpinner
|
|||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import com.sw.dualscreen.R
|
import com.sw.dualscreen.R
|
||||||
|
|
||||||
|
/**
|
||||||
|
* spinner控件
|
||||||
|
*/
|
||||||
class CustomSpinner @JvmOverloads constructor(
|
class CustomSpinner @JvmOverloads constructor(
|
||||||
context: Context,
|
context: Context,
|
||||||
attrs: AttributeSet? = null,
|
attrs: AttributeSet? = null,
|
||||||
|
|||||||
@@ -1,56 +0,0 @@
|
|||||||
package com.sw.dualscreen.view
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import android.graphics.Canvas
|
|
||||||
import android.graphics.Color
|
|
||||||
import android.graphics.Paint
|
|
||||||
import android.util.AttributeSet
|
|
||||||
import android.view.View
|
|
||||||
|
|
||||||
class CylinderDrawView @JvmOverloads constructor(
|
|
||||||
context: Context,
|
|
||||||
attrs: AttributeSet? = null,
|
|
||||||
defStyleAttr: Int = 0
|
|
||||||
) : View(context, attrs, defStyleAttr) {
|
|
||||||
|
|
||||||
// 圆柱属性(默认红色)
|
|
||||||
var cylinderColor: Int = Color.RED
|
|
||||||
set(value) {
|
|
||||||
field = value
|
|
||||||
invalidate()
|
|
||||||
}
|
|
||||||
var cylinderWidth: Int = 120 // 默认宽度(px)
|
|
||||||
set(value) {
|
|
||||||
field = value
|
|
||||||
requestLayout()
|
|
||||||
}
|
|
||||||
var cylinderHeight: Int = 360 // 默认高度(px)
|
|
||||||
set(value) {
|
|
||||||
field = value
|
|
||||||
requestLayout()
|
|
||||||
}
|
|
||||||
|
|
||||||
private val paint = Paint().apply {
|
|
||||||
isAntiAlias = true // 抗锯齿
|
|
||||||
style = Paint.Style.FILL // 填充模式
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
|
|
||||||
val measuredWidth = resolveSize(cylinderWidth, widthMeasureSpec)
|
|
||||||
val measuredHeight = resolveSize(cylinderHeight, heightMeasureSpec)
|
|
||||||
setMeasuredDimension(measuredWidth, measuredHeight)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onDraw(canvas: Canvas) {
|
|
||||||
super.onDraw(canvas)
|
|
||||||
paint.color = cylinderColor
|
|
||||||
// 绘制圆角矩形(半径=宽度的一半,形成圆柱效果)
|
|
||||||
val radius = cylinderWidth / 2f
|
|
||||||
canvas.drawRoundRect(
|
|
||||||
0f, 0f,
|
|
||||||
cylinderWidth.toFloat(), cylinderHeight.toFloat(),
|
|
||||||
radius, radius,
|
|
||||||
paint
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,104 +0,0 @@
|
|||||||
package com.sw.dualscreen.view
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import android.graphics.Color
|
|
||||||
import android.util.AttributeSet
|
|
||||||
import android.view.LayoutInflater
|
|
||||||
import android.widget.FrameLayout
|
|
||||||
import android.widget.ImageView
|
|
||||||
import android.widget.TextView
|
|
||||||
import com.sw.dualscreen.R
|
|
||||||
|
|
||||||
class CylinderView @JvmOverloads constructor(
|
|
||||||
context: Context,
|
|
||||||
attrs: AttributeSet? = null,
|
|
||||||
defStyleAttr: Int = 0
|
|
||||||
) : FrameLayout(context, attrs, defStyleAttr) {
|
|
||||||
|
|
||||||
private val valueText: TextView
|
|
||||||
private val unitText: TextView
|
|
||||||
private val cylinderDrawView: CylinderDrawView
|
|
||||||
private val topImage: ImageView
|
|
||||||
private val bottomImage: ImageView
|
|
||||||
private val bottomText: TextView
|
|
||||||
|
|
||||||
init {
|
|
||||||
// 加载内部布局
|
|
||||||
LayoutInflater.from(context).inflate(R.layout.view_cylinder, this, true)
|
|
||||||
|
|
||||||
// 绑定控件
|
|
||||||
valueText = findViewById(R.id.valueText)
|
|
||||||
unitText = findViewById(R.id.unitText)
|
|
||||||
cylinderDrawView = findViewById(R.id.cylinderDrawView)
|
|
||||||
topImage = findViewById(R.id.topImage)
|
|
||||||
bottomImage = findViewById(R.id.bottomImage)
|
|
||||||
bottomText = findViewById(R.id.bottomText)
|
|
||||||
|
|
||||||
// 解析自定义属性
|
|
||||||
context.obtainStyledAttributes(attrs, R.styleable.CylinderView).apply {
|
|
||||||
// 顶部文字:数值+单位合并
|
|
||||||
val value = getString(R.styleable.CylinderView_valueText) ?: ""
|
|
||||||
val unit = getString(R.styleable.CylinderView_unitText) ?: ""
|
|
||||||
valueText.text = value
|
|
||||||
valueText.textSize = getDimension(R.styleable.CylinderView_valueTextSize, 20f)
|
|
||||||
valueText.setTextColor(
|
|
||||||
getColor(
|
|
||||||
R.styleable.CylinderView_valueTextColor,
|
|
||||||
0xFFFFFFFF.toInt()
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
unitText.text = unit
|
|
||||||
unitText.textSize = getDimension(R.styleable.CylinderView_valueTextSize, 10f)
|
|
||||||
unitText.setTextColor(
|
|
||||||
getColor(
|
|
||||||
R.styleable.CylinderView_valueTextColor,
|
|
||||||
0xFFFFFFFF.toInt()
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
// 圆柱属性
|
|
||||||
cylinderDrawView.cylinderColor =
|
|
||||||
getColor(R.styleable.CylinderView_cylinderColor, Color.RED)
|
|
||||||
cylinderDrawView.cylinderWidth =
|
|
||||||
getDimensionPixelSize(R.styleable.CylinderView_cylinderWidth, 60)
|
|
||||||
cylinderDrawView.cylinderHeight =
|
|
||||||
getDimensionPixelSize(R.styleable.CylinderView_cylinderHeight, 180)
|
|
||||||
|
|
||||||
getResourceId(R.styleable.CylinderView_topImageSrc, -1).takeIf { it != -1 }?.let {
|
|
||||||
topImage.setImageResource(it)
|
|
||||||
// val layoutParams = cylinderDrawView.layoutParams
|
|
||||||
// layoutParams.height = (layoutParams.width / 2.45).toInt()
|
|
||||||
// topImage.layoutParams = layoutParams
|
|
||||||
}
|
|
||||||
|
|
||||||
// 底部图片
|
|
||||||
getResourceId(R.styleable.CylinderView_bottomImageSrc, -1).takeIf { it != -1 }?.let {
|
|
||||||
bottomImage.setImageResource(it)
|
|
||||||
// val layoutParams = cylinderDrawView.layoutParams
|
|
||||||
// layoutParams.height = 44
|
|
||||||
// bottomImage.layoutParams = layoutParams
|
|
||||||
}
|
|
||||||
|
|
||||||
// 底部文字
|
|
||||||
bottomText.text = getString(R.styleable.CylinderView_bottomText) ?: "热量"
|
|
||||||
bottomText.textSize = getDimension(R.styleable.CylinderView_bottomTextSize, 16f)
|
|
||||||
bottomText.setTextColor(
|
|
||||||
getColor(
|
|
||||||
R.styleable.CylinderView_bottomTextColor,
|
|
||||||
0xFFFFFFFF.toInt()
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
recycle() // 回收属性
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun setToImageSize() {
|
|
||||||
// topImage.layoutParams = LayoutParams.
|
|
||||||
}
|
|
||||||
|
|
||||||
fun setBottomSize() {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -52,7 +52,10 @@ abstract class BaseViewModel() : ViewModel() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected open fun parseResponse(response: ApiResponse<*>): Boolean {
|
protected open fun parseResponse(response: ApiResponse<*>): Boolean {
|
||||||
if (response.isSuccess()) {
|
if (response.isSuccess()
|
||||||
|
|| response.code == 200
|
||||||
|
|| response.code == 0
|
||||||
|
) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
Timber.d("msg = ${response.message}, code = ${response.code}")
|
Timber.d("msg = ${response.message}, code = ${response.code}")
|
||||||
@@ -62,6 +65,7 @@ abstract class BaseViewModel() : ViewModel() {
|
|||||||
|
|
||||||
fun parseEquipmentInfo(equipmentInfo: EquipmentInfo) {
|
fun parseEquipmentInfo(equipmentInfo: EquipmentInfo) {
|
||||||
GlobalData.appBaseUrl = equipmentInfo.appPackageUrl!!
|
GlobalData.appBaseUrl = equipmentInfo.appPackageUrl!!
|
||||||
|
GlobalData.appBaseUrl="http://192.168.1.210/gateway/local"
|
||||||
GlobalData.sdkKey = equipmentInfo.arcsoftSdkKey!!
|
GlobalData.sdkKey = equipmentInfo.arcsoftSdkKey!!
|
||||||
GlobalData.appId = equipmentInfo.arcsoftAppId!!
|
GlobalData.appId = equipmentInfo.arcsoftAppId!!
|
||||||
// GlobalData.activeKey = equipmentInfo.arcsoftActiveKey!!
|
// GlobalData.activeKey = equipmentInfo.arcsoftActiveKey!!
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import com.sw.dualscreen.GlobalData
|
|||||||
import com.sw.dualscreen.GlobalKey
|
import com.sw.dualscreen.GlobalKey
|
||||||
import com.sw.dualscreen.model.response.EquipmentInfo
|
import com.sw.dualscreen.model.response.EquipmentInfo
|
||||||
import com.sw.dualscreen.utils.GsonUtils
|
import com.sw.dualscreen.utils.GsonUtils
|
||||||
import com.sw.inbound.utils.SPUtil
|
import com.sw.dualscreen.utils.SPUtil
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.sw.dualscreen.viewmodel
|
package com.sw.dualscreen.viewmodel
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.net.Uri
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import com.arcsoft.face.ErrorInfo
|
import com.arcsoft.face.ErrorInfo
|
||||||
import com.sw.dualscreen.GlobalData
|
import com.sw.dualscreen.GlobalData
|
||||||
@@ -9,19 +11,19 @@ import com.sw.dualscreen.model.response.DinnerTypeInfo
|
|||||||
import com.sw.dualscreen.model.response.FoodInfo
|
import com.sw.dualscreen.model.response.FoodInfo
|
||||||
import com.sw.dualscreen.model.response.UserFaceModel
|
import com.sw.dualscreen.model.response.UserFaceModel
|
||||||
import com.sw.dualscreen.model.response.UserNutritionData
|
import com.sw.dualscreen.model.response.UserNutritionData
|
||||||
import com.sw.inbound.utils.SPUtil
|
import com.sw.dualscreen.utils.SPUtil
|
||||||
import com.sw.plate.App
|
import com.sw.plate.App
|
||||||
import com.sw.plate.utils.Base64
|
import com.sw.plate.utils.Base64
|
||||||
import com.sw.plate.utils.ToastUtils
|
import com.sw.plate.utils.ToastUtils
|
||||||
import com.sw.plate.utils.arcface.FaceApi
|
import com.sw.plate.utils.arcface.FaceApi
|
||||||
import com.sw.plate.utils.arcface.facedb.entity.FaceEntity
|
import com.sw.plate.utils.arcface.facedb.entity.FaceEntity
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.delay
|
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
import kotlin.math.atan
|
||||||
|
|
||||||
class UserViewModel : BaseViewModel() {
|
class UserViewModel : BaseViewModel() {
|
||||||
|
|
||||||
@@ -50,7 +52,8 @@ class UserViewModel : BaseViewModel() {
|
|||||||
*/
|
*/
|
||||||
private val _loadFaceResult = MutableStateFlow<Boolean>(false)
|
private val _loadFaceResult = MutableStateFlow<Boolean>(false)
|
||||||
val loadFaceResult: StateFlow<Boolean> = _loadFaceResult
|
val loadFaceResult: StateFlow<Boolean> = _loadFaceResult
|
||||||
|
private val _identifiedFoodInfoList2 = MutableStateFlow<List<FoodInfo>>(emptyList())
|
||||||
|
val identifiedFoodInfoList2: StateFlow<List<FoodInfo>> = _identifiedFoodInfoList2
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取token
|
* 获取token
|
||||||
@@ -140,15 +143,10 @@ class UserViewModel : BaseViewModel() {
|
|||||||
fun getIdentifiedFoodList() {
|
fun getIdentifiedFoodList() {
|
||||||
Timber.d("identifiedFoodList")
|
Timber.d("identifiedFoodList")
|
||||||
launchWithLoading {
|
launchWithLoading {
|
||||||
delay(5000)
|
val response = repository.getRestInfoFoodsByType(foodName = "")
|
||||||
_identifiedFoodInfoList.value = List(6) {
|
if (parseResponse(response)) {
|
||||||
FoodInfo(
|
_identifiedFoodInfoList.value = response.result ?: emptyList()
|
||||||
id = "185225109358109491${it}",
|
|
||||||
foodName = "豆芽炒粉条$it",
|
|
||||||
imgUrl = "http://101.201.149.156:9002/temp/20230912/f54b2d76-45e1-4c6b-a7a0-5fd1eace99d1_1694484601879.png"
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
// _currentFoodInfo.value = _identifiedFoodInfoList.value[0]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,4 +210,35 @@ class UserViewModel : BaseViewModel() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getFoodInfo(foodName: String, action:(List<FoodInfo>)-> Unit = {}) {
|
||||||
|
Timber.d("getFoodInfo")
|
||||||
|
launchWithLoading {
|
||||||
|
val response = repository.getFoodInfo(foodName = foodName)
|
||||||
|
if (parseResponse(response)) {
|
||||||
|
val list = response.result ?: emptyList()
|
||||||
|
_identifiedFoodInfoList2.value = list
|
||||||
|
action(list)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun postImageData(
|
||||||
|
context: Context,
|
||||||
|
foodId: String,
|
||||||
|
foodName: String,
|
||||||
|
foodVector: String,
|
||||||
|
uri: Uri
|
||||||
|
) {
|
||||||
|
Timber.d("postImageData")
|
||||||
|
launchWithLoading {
|
||||||
|
val response = repository.postImageData(
|
||||||
|
context, foodId = foodId, foodName = foodName, foodVector = foodVector,
|
||||||
|
uri = uri
|
||||||
|
)
|
||||||
|
if (parseResponse(response)) {
|
||||||
|
Timber.tag("mzf").d("code=${response.code}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<item android:color="#FF3232" android:state_checked="true"/>
|
||||||
|
<item android:color="#5E7585" android:state_checked="false"/>
|
||||||
|
</selector>
|
||||||
|
After Width: | Height: | Size: 2.6 KiB |
|
After Width: | Height: | Size: 3.0 KiB |
|
After Width: | Height: | Size: 3.7 KiB |
|
After Width: | Height: | Size: 8.5 KiB |
|
After Width: | Height: | Size: 1.8 KiB |
|
After Width: | Height: | Size: 6.5 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 21 KiB |
|
After Width: | Height: | Size: 5.4 KiB |
|
After Width: | Height: | Size: 4.9 KiB |
|
After Width: | Height: | Size: 15 KiB |
|
After Width: | Height: | Size: 4.8 KiB |
|
After Width: | Height: | Size: 5.1 KiB |
|
After Width: | Height: | Size: 8.5 KiB |
|
After Width: | Height: | Size: 9.6 KiB |
|
After Width: | Height: | Size: 6.1 KiB |
|
After Width: | Height: | Size: 6.7 KiB |
|
After Width: | Height: | Size: 3.7 KiB |
|
After Width: | Height: | Size: 3.9 KiB |
@@ -0,0 +1,10 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:color="#30000000">
|
||||||
|
<item>
|
||||||
|
<shape android:shape="rectangle">
|
||||||
|
<solid android:color="#FF0032C8" />
|
||||||
|
<corners android:radius="10dp" />
|
||||||
|
</shape>
|
||||||
|
</item>
|
||||||
|
</ripple>
|
||||||
|
Before Width: | Height: | Size: 1.0 MiB After Width: | Height: | Size: 172 KiB |
@@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<shape xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:shape="rectangle">
|
||||||
|
<solid android:color="#ffffff"/>
|
||||||
|
<stroke android:color="#cdcdcd" android:width="1dp"/>
|
||||||
|
<corners android:radius="5dp"/>
|
||||||
|
</shape>
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
|
||||||
|
<item android:state_checked="false">
|
||||||
|
<shape android:shape="rectangle">
|
||||||
|
<corners android:radius="8dp"/>
|
||||||
|
<solid android:color="#F4FAFF"/>
|
||||||
|
<stroke android:color="@color/white" android:width="2dp"/>
|
||||||
|
</shape>
|
||||||
|
</item>
|
||||||
|
<item android:state_checked="true">
|
||||||
|
<layer-list>
|
||||||
|
<item>
|
||||||
|
<shape android:shape="rectangle">
|
||||||
|
<corners android:radius="8dp"/>
|
||||||
|
<solid android:color="#FFF0F0"/>
|
||||||
|
<stroke android:color="#FF3232" android:width="2dp"/>
|
||||||
|
</shape>
|
||||||
|
</item>
|
||||||
|
<item android:width="32dp" android:height="32dp" android:gravity="bottom|end">
|
||||||
|
<bitmap android:src="@drawable/check_red"/>
|
||||||
|
</item>
|
||||||
|
</layer-list>
|
||||||
|
</item>
|
||||||
|
|
||||||
|
</selector>
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<shape xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:shape="rectangle">
|
||||||
|
<corners
|
||||||
|
android:bottomLeftRadius="12dp"
|
||||||
|
android:bottomRightRadius="12dp"
|
||||||
|
android:topLeftRadius="12dp"
|
||||||
|
android:topRightRadius="12dp" />
|
||||||
|
<stroke
|
||||||
|
android:color="#ffff9900"
|
||||||
|
android:width="2dp" />
|
||||||
|
</shape>
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<shape xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:shape="rectangle">
|
||||||
|
<solid android:color="@color/white"/>
|
||||||
|
<corners android:radius="12dp"/>
|
||||||
|
</shape>
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:color="#30000000">
|
||||||
|
<item>
|
||||||
|
<shape android:shape="rectangle">
|
||||||
|
<solid android:color="@color/white"/>
|
||||||
|
<stroke android:color="#FF0032C8" android:width="1dp"/>
|
||||||
|
<corners android:radius="10dp"/>
|
||||||
|
</shape>
|
||||||
|
</item>
|
||||||
|
</ripple>
|
||||||
@@ -2,6 +2,6 @@
|
|||||||
<shape xmlns:android="http://schemas.android.com/apk/res/android">
|
<shape xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
<solid android:color="@android:color/white" />
|
<solid android:color="@android:color/white" />
|
||||||
<corners
|
<corners
|
||||||
android:topLeftRadius="16dp"
|
android:topLeftRadius="60dp"
|
||||||
android:topRightRadius="16dp" />
|
android:topRightRadius="60dp" />
|
||||||
</shape>
|
</shape>
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<item android:state_checked="false" >
|
||||||
|
<layer-list>
|
||||||
|
<item android:width="60dp" android:height="60dp">
|
||||||
|
<bitmap android:src="@drawable/pay_cash_normal" />
|
||||||
|
</item>
|
||||||
|
</layer-list>
|
||||||
|
</item>
|
||||||
|
<item android:state_checked="true">
|
||||||
|
<layer-list>
|
||||||
|
<item android:width="60dp" android:height="60dp">
|
||||||
|
<bitmap android:src="@drawable/pay_cash_checked" />
|
||||||
|
</item>
|
||||||
|
</layer-list>
|
||||||
|
</item>
|
||||||
|
</selector>
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<item android:state_checked="false" >
|
||||||
|
<layer-list>
|
||||||
|
<item android:width="60dp" android:height="60dp">
|
||||||
|
<bitmap android:src="@drawable/pay_face_normal" />
|
||||||
|
</item>
|
||||||
|
</layer-list>
|
||||||
|
</item>
|
||||||
|
<item android:state_checked="true">
|
||||||
|
<layer-list>
|
||||||
|
<item android:width="60dp" android:height="60dp">
|
||||||
|
<bitmap android:src="@drawable/pay_face_checked" />
|
||||||
|
</item>
|
||||||
|
</layer-list>
|
||||||
|
</item>
|
||||||
|
</selector>
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<item android:state_checked="false" >
|
||||||
|
<layer-list>
|
||||||
|
<item android:width="60dp" android:height="60dp">
|
||||||
|
<bitmap android:src="@drawable/pay_number_normal" />
|
||||||
|
</item>
|
||||||
|
</layer-list>
|
||||||
|
</item>
|
||||||
|
<item android:state_checked="true">
|
||||||
|
<layer-list>
|
||||||
|
<item android:width="60dp" android:height="60dp">
|
||||||
|
<bitmap android:src="@drawable/pay_number_checked" />
|
||||||
|
</item>
|
||||||
|
</layer-list>
|
||||||
|
</item>
|
||||||
|
</selector>
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<item android:state_checked="false" >
|
||||||
|
<layer-list>
|
||||||
|
<item android:width="60dp" android:height="60dp">
|
||||||
|
<bitmap android:src="@drawable/pay_qrcode_normal" />
|
||||||
|
</item>
|
||||||
|
</layer-list>
|
||||||
|
</item>
|
||||||
|
<item android:state_checked="true">
|
||||||
|
<layer-list>
|
||||||
|
<item android:width="60dp" android:height="60dp">
|
||||||
|
<bitmap android:src="@drawable/pay_qrcode_checked" />
|
||||||
|
</item>
|
||||||
|
</layer-list>
|
||||||
|
</item>
|
||||||
|
</selector>
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
android:orientation="vertical"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent">
|
||||||
|
<FrameLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="88dp"
|
||||||
|
android:layout_marginTop="24dp">
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/ivBack"
|
||||||
|
android:layout_width="68dp"
|
||||||
|
android:layout_height="68dp"
|
||||||
|
android:padding="10dp"
|
||||||
|
android:src="@drawable/ic_back" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="center"
|
||||||
|
android:text="已采集菜品"
|
||||||
|
android:textColor="@color/black"
|
||||||
|
android:textSize="26sp" />
|
||||||
|
</FrameLayout>
|
||||||
|
|
||||||
|
<FrameLayout
|
||||||
|
android:id="@+id/flSearchBlock"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="80dp"
|
||||||
|
android:layout_marginHorizontal="20dp"
|
||||||
|
android:background="@drawable/bg_gray">
|
||||||
|
|
||||||
|
<EditText
|
||||||
|
android:id="@+id/etInputGoods"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:background="#00000000"
|
||||||
|
android:gravity="center"
|
||||||
|
android:hint="输入菜品名称"
|
||||||
|
android:maxLines="1"
|
||||||
|
android:paddingStart="10dp"
|
||||||
|
android:paddingEnd="80dp"
|
||||||
|
android:textColor="#FF666666"
|
||||||
|
android:textColorHint="#FFC7C8DC"
|
||||||
|
android:textSize="30sp"
|
||||||
|
android:inputType="text"
|
||||||
|
android:imeOptions="actionSearch" />
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/ivGoodsSearch"
|
||||||
|
android:layout_width="60dp"
|
||||||
|
android:layout_height="60dp"
|
||||||
|
android:layout_gravity="end|center_vertical"
|
||||||
|
android:layout_marginEnd="16dp"
|
||||||
|
android:padding="14dp"
|
||||||
|
android:src="@drawable/ic_goods_search" />
|
||||||
|
|
||||||
|
</FrameLayout>
|
||||||
|
|
||||||
|
<!-- <com.scwang.smart.refresh.layout.SmartRefreshLayout-->
|
||||||
|
<!-- android:id="@+id/refreshLayout"-->
|
||||||
|
<!-- android:layout_width="match_parent"-->
|
||||||
|
<!-- android:layout_height="0dp"-->
|
||||||
|
<!-- android:layout_marginHorizontal="130dp"-->
|
||||||
|
<!-- android:layout_marginTop="40dp"-->
|
||||||
|
<!-- android:layout_marginBottom="58dp"-->
|
||||||
|
<!-- app:layout_constraintBottom_toTopOf="@id/dividerLine"-->
|
||||||
|
<!-- app:layout_constraintTop_toBottomOf="@id/flSearchBlock">-->
|
||||||
|
|
||||||
|
<!-- <com.scwang.smart.refresh.header.ClassicsHeader-->
|
||||||
|
<!-- android:layout_width="match_parent"-->
|
||||||
|
<!-- android:layout_height="wrap_content" />-->
|
||||||
|
|
||||||
|
<androidx.recyclerview.widget.RecyclerView
|
||||||
|
android:id="@+id/rvFoodList"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:overScrollMode="never"
|
||||||
|
android:layout_margin="10dp"
|
||||||
|
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
|
||||||
|
tools:itemCount="8"
|
||||||
|
tools:listitem="@layout/list_item_collected_data" />
|
||||||
|
|
||||||
|
<!-- <com.scwang.smart.refresh.footer.ClassicsFooter-->
|
||||||
|
<!-- android:layout_width="match_parent"-->
|
||||||
|
<!-- android:layout_height="wrap_content" />-->
|
||||||
|
<!-- </com.scwang.smart.refresh.layout.SmartRefreshLayout>-->
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
@@ -0,0 +1,182 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:orientation="vertical">
|
||||||
|
|
||||||
|
<FrameLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="88dp"
|
||||||
|
android:layout_marginTop="24dp">
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/ivBack"
|
||||||
|
android:layout_width="68dp"
|
||||||
|
android:layout_height="68dp"
|
||||||
|
android:padding="15dp"
|
||||||
|
android:layout_gravity="center_vertical"
|
||||||
|
android:src="@drawable/ic_back" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="center"
|
||||||
|
android:text="菜品采集"
|
||||||
|
android:textColor="@color/black"
|
||||||
|
android:textSize="26sp" />
|
||||||
|
</FrameLayout>
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="horizontal"
|
||||||
|
android:layout_marginBottom="24dp">
|
||||||
|
|
||||||
|
<FrameLayout
|
||||||
|
android:id="@+id/flPreview"
|
||||||
|
android:layout_width="180dp"
|
||||||
|
android:layout_height="180dp"
|
||||||
|
android:layout_marginStart="24dp">
|
||||||
|
|
||||||
|
<androidx.cardview.widget.CardView
|
||||||
|
android:id="@+id/flCameraPreview"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:layout_gravity="center"
|
||||||
|
android:layout_margin="2dp"
|
||||||
|
app:cardCornerRadius="10dp"
|
||||||
|
app:cardElevation="0dp"/>
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:id="@+id/llCameraFlag"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:layout_gravity="center"
|
||||||
|
android:gravity="center"
|
||||||
|
android:background="@color/white"
|
||||||
|
android:orientation="vertical"
|
||||||
|
android:visibility="visible">
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:layout_width="48dp"
|
||||||
|
android:layout_height="48dp"
|
||||||
|
android:layout_gravity="center"
|
||||||
|
android:scaleType="fitCenter"
|
||||||
|
android:src="@drawable/ic_camera256"
|
||||||
|
tools:ignore="ContentDescription" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="24dp"
|
||||||
|
android:text="图片采集"
|
||||||
|
android:textColor="#ffb4b4c8"
|
||||||
|
android:textSize="16sp" />
|
||||||
|
</LinearLayout>
|
||||||
|
</FrameLayout>
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:layout_marginStart="20dp"
|
||||||
|
android:orientation="horizontal"
|
||||||
|
android:gravity="center_vertical">
|
||||||
|
<Button
|
||||||
|
android:id="@+id/btnTakePhoto"
|
||||||
|
android:layout_width="140dp"
|
||||||
|
android:layout_height="80dp"
|
||||||
|
android:gravity="center"
|
||||||
|
android:text="开始拍照"
|
||||||
|
android:textColor="#ffffff"
|
||||||
|
android:textSize="18sp" />
|
||||||
|
<Button
|
||||||
|
android:id="@+id/btnClearData"
|
||||||
|
android:layout_width="120dp"
|
||||||
|
android:layout_height="80dp"
|
||||||
|
android:gravity="center"
|
||||||
|
android:text="清除"
|
||||||
|
android:layout_marginHorizontal="10dp"
|
||||||
|
android:textColor="#ffffff"
|
||||||
|
android:textSize="18sp" />
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/btnCollectedGoods"
|
||||||
|
android:layout_width="220dp"
|
||||||
|
android:layout_height="80dp"
|
||||||
|
android:gravity="center"
|
||||||
|
android:text="已采集菜品"
|
||||||
|
android:textColor="#ffffff"
|
||||||
|
android:textSize="18sp" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
<androidx.recyclerview.widget.RecyclerView
|
||||||
|
android:id="@+id/rvFoodCollection"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="0dp"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:layout_marginHorizontal="3dp"
|
||||||
|
android:layout_marginStart="24dp"
|
||||||
|
android:layout_marginEnd="24dp"
|
||||||
|
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
|
||||||
|
app:spanCount="3"
|
||||||
|
tools:itemCount="3"
|
||||||
|
tools:listitem="@layout/list_item_food_collection" />
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="66dp"
|
||||||
|
android:layout_marginLeft="24dp"
|
||||||
|
android:layout_marginTop="24dp"
|
||||||
|
android:layout_marginRight="24dp"
|
||||||
|
android:gravity="center_vertical"
|
||||||
|
android:orientation="horizontal">
|
||||||
|
|
||||||
|
<EditText
|
||||||
|
android:id="@+id/editFoodName"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:background="@drawable/bg_gray"
|
||||||
|
android:gravity="center_vertical"
|
||||||
|
android:hint="输入菜品名称"
|
||||||
|
android:imeOptions="actionSearch"
|
||||||
|
android:paddingStart="15dp"
|
||||||
|
android:paddingEnd="15dp"
|
||||||
|
android:singleLine="true"
|
||||||
|
android:textColor="@color/black"
|
||||||
|
android:textSize="26sp" />
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/btnFoodSearch"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginLeft="12dp"
|
||||||
|
android:text="菜品检索"
|
||||||
|
android:textColor="#ffffff"
|
||||||
|
android:textSize="26sp" />
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
<androidx.recyclerview.widget.RecyclerView
|
||||||
|
android:id="@+id/recyclerview"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="0dp"
|
||||||
|
android:layout_marginStart="24dp"
|
||||||
|
android:layout_marginTop="10dp"
|
||||||
|
android:layout_marginEnd="24dp"
|
||||||
|
android:layout_weight="1" />
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/btnSave"
|
||||||
|
android:layout_width="260dp"
|
||||||
|
android:layout_height="60dp"
|
||||||
|
android:layout_gravity="center"
|
||||||
|
android:layout_marginVertical="24dp"
|
||||||
|
android:text="保存"
|
||||||
|
android:textColor="#ffffff"
|
||||||
|
android:textSize="26sp" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
@@ -85,6 +85,7 @@
|
|||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_centerHorizontal="true"
|
android:layout_centerHorizontal="true"
|
||||||
android:layout_alignParentBottom="true" />
|
android:layout_alignParentBottom="true"
|
||||||
|
tools:src="@mipmap/ic_launcher"/>
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
@@ -24,9 +24,9 @@
|
|||||||
|
|
||||||
<Spinner
|
<Spinner
|
||||||
android:id="@+id/spinner"
|
android:id="@+id/spinner"
|
||||||
|
style="@style/customSpinnerStyle"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
style="@style/customSpinnerStyle"
|
|
||||||
android:layout_gravity="end"
|
android:layout_gravity="end"
|
||||||
android:padding="0dp"
|
android:padding="0dp"
|
||||||
android:popupBackground="@drawable/shape_for_custom_spinner" />
|
android:popupBackground="@drawable/shape_for_custom_spinner" />
|
||||||
@@ -43,6 +43,7 @@
|
|||||||
android:text="羊肉臊子荞面饸饹" />
|
android:text="羊肉臊子荞面饸饹" />
|
||||||
|
|
||||||
<FrameLayout
|
<FrameLayout
|
||||||
|
android:id="@+id/foodDisplayLayout"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="531dp"
|
android:layout_height="531dp"
|
||||||
android:layout_marginHorizontal="60dp"
|
android:layout_marginHorizontal="60dp"
|
||||||
@@ -60,6 +61,7 @@
|
|||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:contentDescription="食物图预览"
|
android:contentDescription="食物图预览"
|
||||||
|
android:scaleType="centerCrop"
|
||||||
android:visibility="gone" />
|
android:visibility="gone" />
|
||||||
|
|
||||||
<View
|
<View
|
||||||
@@ -76,6 +78,35 @@
|
|||||||
android:layout_marginHorizontal="60dp"
|
android:layout_marginHorizontal="60dp"
|
||||||
android:layout_weight="1" />
|
android:layout_weight="1" />
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:id="@+id/layout_rescan"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginBottom="140dp"
|
||||||
|
android:gravity="center"
|
||||||
|
android:orientation="vertical"
|
||||||
|
android:visibility="gone">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="未识别出菜品,请重试或手动搜索"
|
||||||
|
android:textColor="@color/white"
|
||||||
|
android:textSize="24sp" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tv_rescan"
|
||||||
|
android:layout_width="180dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="20dp"
|
||||||
|
android:background="@drawable/bg_rescan"
|
||||||
|
android:gravity="center"
|
||||||
|
android:padding="10dp"
|
||||||
|
android:text="重试"
|
||||||
|
android:textColor="#ffff9900"
|
||||||
|
android:textSize="32sp" />
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/tv_to_search"
|
android:id="@+id/tv_to_search"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
|
|||||||
@@ -0,0 +1,95 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:background="#E7EFF8"
|
||||||
|
android:orientation="vertical">
|
||||||
|
|
||||||
|
<Space
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="0dp"
|
||||||
|
android:layout_weight="1" />
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="167dp"
|
||||||
|
android:layout_gravity="bottom"
|
||||||
|
android:layout_marginHorizontal="32dp"
|
||||||
|
android:layout_marginBottom="64dp"
|
||||||
|
android:gravity="center_vertical"
|
||||||
|
android:orientation="horizontal">
|
||||||
|
|
||||||
|
<androidx.appcompat.widget.AppCompatCheckedTextView
|
||||||
|
android:id="@+id/btnPayQrCode"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:background="@drawable/bg_pay_icon"
|
||||||
|
android:checked="true"
|
||||||
|
android:drawablePadding="10dp"
|
||||||
|
android:gravity="center_horizontal"
|
||||||
|
android:paddingTop="30dp"
|
||||||
|
android:text="在线支付"
|
||||||
|
android:textAlignment="center"
|
||||||
|
android:textColor="@color/color_pay"
|
||||||
|
android:textSize="30sp"
|
||||||
|
android:textStyle="bold"
|
||||||
|
app:drawableTopCompat="@drawable/pay_qrcode" />
|
||||||
|
|
||||||
|
<androidx.appcompat.widget.AppCompatCheckedTextView
|
||||||
|
android:id="@+id/btnPayCash"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:layout_marginStart="32dp"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:background="@drawable/bg_pay_icon"
|
||||||
|
android:checked="false"
|
||||||
|
android:drawablePadding="10dp"
|
||||||
|
android:gravity="center_horizontal"
|
||||||
|
android:paddingTop="30dp"
|
||||||
|
android:text="现金"
|
||||||
|
android:textAlignment="center"
|
||||||
|
android:textColor="@color/color_pay"
|
||||||
|
android:textSize="30sp"
|
||||||
|
android:textStyle="bold"
|
||||||
|
app:drawableTopCompat="@drawable/pay_cash" />
|
||||||
|
|
||||||
|
<androidx.appcompat.widget.AppCompatCheckedTextView
|
||||||
|
android:id="@+id/btnPayNumber"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:layout_marginStart="32dp"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:background="@drawable/bg_pay_icon"
|
||||||
|
android:checked="false"
|
||||||
|
android:drawablePadding="11dp"
|
||||||
|
android:gravity="center_horizontal"
|
||||||
|
android:paddingTop="30dp"
|
||||||
|
android:text="消费码"
|
||||||
|
android:textAlignment="center"
|
||||||
|
android:textColor="@color/color_pay"
|
||||||
|
android:textSize="30sp"
|
||||||
|
android:textStyle="bold"
|
||||||
|
app:drawableTopCompat="@drawable/pay_number" />
|
||||||
|
|
||||||
|
<androidx.appcompat.widget.AppCompatCheckedTextView
|
||||||
|
android:id="@+id/btnPayFace"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:layout_marginStart="32dp"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:background="@drawable/bg_pay_icon"
|
||||||
|
android:checked="false"
|
||||||
|
android:drawablePadding="11dp"
|
||||||
|
android:gravity="center_horizontal"
|
||||||
|
android:paddingTop="30dp"
|
||||||
|
android:text="刷脸"
|
||||||
|
android:textAlignment="center"
|
||||||
|
android:textColor="@color/color_pay"
|
||||||
|
android:textSize="30sp"
|
||||||
|
android:textStyle="bold"
|
||||||
|
app:drawableTopCompat="@drawable/pay_face" />
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
android:layout_width="400dp"
|
||||||
|
android:layout_height="260dp"
|
||||||
|
android:gravity="center"
|
||||||
|
android:orientation="vertical"
|
||||||
|
android:background="@drawable/bg_white_radius12"
|
||||||
|
tools:ignore="HardcodedText">
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:layout_width="50dp"
|
||||||
|
android:layout_height="50dp"
|
||||||
|
android:src="@drawable/ic_tip_warn"
|
||||||
|
tools:ignore="ContentDescription" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tvWarnContent"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginHorizontal="30dp"
|
||||||
|
android:layout_marginVertical="30dp"
|
||||||
|
android:gravity="center"
|
||||||
|
tools:text="存在未收货的物品,请确认是否放弃收货,若放弃则数据不会保存?"
|
||||||
|
android:textColor="#ff141428"
|
||||||
|
android:textSize="20sp" />
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:gravity="center_vertical"
|
||||||
|
android:orientation="horizontal">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/btnCancel"
|
||||||
|
android:layout_width="120dp"
|
||||||
|
android:layout_height="50dp"
|
||||||
|
android:background="@drawable/bg_white_stroke_blue_ripple"
|
||||||
|
android:text="取消"
|
||||||
|
android:gravity="center"
|
||||||
|
android:textColor="#FF0033CC"
|
||||||
|
android:textSize="20sp" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/btnConfirm"
|
||||||
|
android:layout_width="120dp"
|
||||||
|
android:layout_height="50dp"
|
||||||
|
android:layout_marginStart="30dp"
|
||||||
|
android:background="@drawable/bg_blue_ripple"
|
||||||
|
android:text="确认"
|
||||||
|
android:gravity="center"
|
||||||
|
android:textColor="@color/white"
|
||||||
|
android:textSize="20sp" />
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||