优化
This commit is contained in:
@@ -41,6 +41,7 @@ import com.sw.inbound.model.response.SearchGoodsInfo
|
|||||||
import com.sw.inbound.objbox.Food
|
import com.sw.inbound.objbox.Food
|
||||||
import com.sw.inbound.objbox.FoodModule
|
import com.sw.inbound.objbox.FoodModule
|
||||||
import com.sw.inbound.objbox.FoodModule.IdNameScore
|
import com.sw.inbound.objbox.FoodModule.IdNameScore
|
||||||
|
import com.sw.inbound.objbox.FoodModule.bitmap2FloatArray
|
||||||
import com.sw.inbound.objbox.Food_
|
import com.sw.inbound.objbox.Food_
|
||||||
import com.sw.inbound.objbox.ObjectBox
|
import com.sw.inbound.objbox.ObjectBox
|
||||||
import com.sw.inbound.sdk.SensorScaleUtils
|
import com.sw.inbound.sdk.SensorScaleUtils
|
||||||
@@ -49,6 +50,7 @@ import com.sw.inbound.utils.BitmapCropper
|
|||||||
import com.sw.inbound.utils.BitmapSaver
|
import com.sw.inbound.utils.BitmapSaver
|
||||||
import com.sw.inbound.utils.CameraUtils
|
import com.sw.inbound.utils.CameraUtils
|
||||||
import com.sw.inbound.utils.DateTimeUtils
|
import com.sw.inbound.utils.DateTimeUtils
|
||||||
|
import com.sw.inbound.utils.FileUtils
|
||||||
import com.sw.inbound.utils.ImageUtil
|
import com.sw.inbound.utils.ImageUtil
|
||||||
import com.sw.inbound.utils.LogSaveUtil
|
import com.sw.inbound.utils.LogSaveUtil
|
||||||
import com.sw.inbound.utils.PreciseDelayHandler
|
import com.sw.inbound.utils.PreciseDelayHandler
|
||||||
@@ -369,13 +371,20 @@ abstract class GoodsListActivity : ComponentActivity() {
|
|||||||
//物品名称
|
//物品名称
|
||||||
//foodList = foodList.map { it.split("WP")[0] }
|
//foodList = foodList.map { it.split("WP")[0] }
|
||||||
|
|
||||||
val foodList = FoodModule.getFoodScoreList(bitmap)
|
// TODO: ---------------------------
|
||||||
if (bitmap.isRecycled.not()) {
|
// file?.let {
|
||||||
bitmap.recycle()
|
// FileUtils.fileToBitmap(it)?.let { bitmap2 ->
|
||||||
}
|
// val floatArray = bitmap2FloatArray(bitmap2, false)
|
||||||
// if (newBmp.isRecycled.not()) {
|
// val floatArray2 = bitmap2FloatArray(bitmap, false)
|
||||||
// newBmp.recycle()
|
//
|
||||||
|
// val arrayJson = floatArray.toJsonString()
|
||||||
|
// val arrayJson2 = floatArray2.toJsonString()
|
||||||
|
// toast("${arrayJson == arrayJson2}")
|
||||||
// }
|
// }
|
||||||
|
// }
|
||||||
|
// TODO: ---------------------------
|
||||||
|
|
||||||
|
val foodList = FoodModule.getFoodScoreList(bitmap)
|
||||||
// val count = foodList.count { it.score < 0.15 }
|
// val count = foodList.count { it.score < 0.15 }
|
||||||
val count = foodList.size
|
val count = foodList.size
|
||||||
if (count == 0) {
|
if (count == 0) {
|
||||||
@@ -428,6 +437,12 @@ abstract class GoodsListActivity : ComponentActivity() {
|
|||||||
offsetX = 30, offsetY = 100
|
offsetX = 30, offsetY = 100
|
||||||
)
|
)
|
||||||
readBitmap(newBmp)
|
readBitmap(newBmp)
|
||||||
|
if (newBmp.isRecycled.not()) {
|
||||||
|
newBmp.recycle()
|
||||||
|
}
|
||||||
|
if (bitmap.isRecycled.not()) {
|
||||||
|
bitmap.recycle()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -145,7 +145,7 @@ object FoodModule {
|
|||||||
|
|
||||||
suspend fun getFoodScoreList(bitmap: Bitmap, queryCount: Int = 15): List<IdNameScore> {
|
suspend fun getFoodScoreList(bitmap: Bitmap, queryCount: Int = 15): List<IdNameScore> {
|
||||||
val floatArray = bitmap2FloatArray(bitmap, false) ?: return emptyList()
|
val floatArray = bitmap2FloatArray(bitmap, false) ?: return emptyList()
|
||||||
logInfo("向量:${floatArray.toJsonString()}")
|
logInfo("getFoodScoreList向量:${floatArray.toJsonString()}")
|
||||||
val nameScoreList = queryFoodNameScore(floatArray, queryCount)
|
val nameScoreList = queryFoodNameScore(floatArray, queryCount)
|
||||||
if (nameScoreList.isEmpty()) {
|
if (nameScoreList.isEmpty()) {
|
||||||
return emptyList()
|
return emptyList()
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package com.sw.inbound.utils
|
|||||||
|
|
||||||
import android.content.ContentUris
|
import android.content.ContentUris
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.graphics.Bitmap
|
||||||
|
import android.graphics.BitmapFactory
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.provider.MediaStore
|
import android.provider.MediaStore
|
||||||
@@ -156,4 +158,45 @@ object FileUtils {
|
|||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 File 转换为 Bitmap
|
||||||
|
* @param file 图片文件
|
||||||
|
* @param reqWidth 目标宽度(用于采样压缩,0 表示不压缩)
|
||||||
|
* @param reqHeight 目标高度(用于采样压缩,0 表示不压缩)
|
||||||
|
* @return 解码后的 Bitmap,失败返回 null
|
||||||
|
*/
|
||||||
|
fun fileToBitmap(file: File, reqWidth: Int = 0, reqHeight: Int = 0): Bitmap? {
|
||||||
|
if (!file.exists() || !file.isFile) return null
|
||||||
|
|
||||||
|
return if (reqWidth > 0 && reqHeight > 0) {
|
||||||
|
// 带采样压缩,避免 OOM
|
||||||
|
val options = BitmapFactory.Options().apply {
|
||||||
|
inJustDecodeBounds = true
|
||||||
|
BitmapFactory.decodeFile(file.absolutePath, this)
|
||||||
|
inSampleSize = calculateInSampleSize(this, reqWidth, reqHeight)
|
||||||
|
inJustDecodeBounds = false
|
||||||
|
}
|
||||||
|
BitmapFactory.decodeFile(file.absolutePath, options)
|
||||||
|
} else {
|
||||||
|
BitmapFactory.decodeFile(file.absolutePath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 计算采样率
|
||||||
|
*/
|
||||||
|
private fun calculateInSampleSize(options: BitmapFactory.Options, reqWidth: Int, reqHeight: Int): Int {
|
||||||
|
val (height, width) = options.outHeight to options.outWidth
|
||||||
|
var inSampleSize = 1
|
||||||
|
if (height > reqHeight || width > reqWidth) {
|
||||||
|
val halfHeight = height / 2
|
||||||
|
val halfWidth = width / 2
|
||||||
|
while (halfHeight / inSampleSize >= reqHeight && halfWidth / inSampleSize >= reqWidth) {
|
||||||
|
inSampleSize *= 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return inSampleSize
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user