From f219c57fad14ac8cae78bc3cfa2f1fea63d8eb10 Mon Sep 17 00:00:00 2001 From: lvmeng <848755140@qq.com> Date: Tue, 3 Mar 2026 18:31:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sw/inbound/activity/GoodsListActivity.kt | 27 +++++++++--- .../java/com/sw/inbound/objbox/FoodModule.kt | 2 +- .../java/com/sw/inbound/utils/FileUtils.kt | 43 +++++++++++++++++++ 3 files changed, 65 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/sw/inbound/activity/GoodsListActivity.kt b/app/src/main/java/com/sw/inbound/activity/GoodsListActivity.kt index e33b563..466af83 100644 --- a/app/src/main/java/com/sw/inbound/activity/GoodsListActivity.kt +++ b/app/src/main/java/com/sw/inbound/activity/GoodsListActivity.kt @@ -41,6 +41,7 @@ import com.sw.inbound.model.response.SearchGoodsInfo import com.sw.inbound.objbox.Food import com.sw.inbound.objbox.FoodModule 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.ObjectBox 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.CameraUtils import com.sw.inbound.utils.DateTimeUtils +import com.sw.inbound.utils.FileUtils import com.sw.inbound.utils.ImageUtil import com.sw.inbound.utils.LogSaveUtil import com.sw.inbound.utils.PreciseDelayHandler @@ -369,13 +371,20 @@ abstract class GoodsListActivity : ComponentActivity() { //物品名称 //foodList = foodList.map { it.split("WP")[0] } - val foodList = FoodModule.getFoodScoreList(bitmap) - if (bitmap.isRecycled.not()) { - bitmap.recycle() - } -// if (newBmp.isRecycled.not()) { -// newBmp.recycle() + // TODO: --------------------------- +// file?.let { +// FileUtils.fileToBitmap(it)?.let { bitmap2 -> +// val floatArray = bitmap2FloatArray(bitmap2, false) +// val floatArray2 = bitmap2FloatArray(bitmap, false) +// +// 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.size if (count == 0) { @@ -428,6 +437,12 @@ abstract class GoodsListActivity : ComponentActivity() { offsetX = 30, offsetY = 100 ) readBitmap(newBmp) + if (newBmp.isRecycled.not()) { + newBmp.recycle() + } + if (bitmap.isRecycled.not()) { + bitmap.recycle() + } } } diff --git a/app/src/main/java/com/sw/inbound/objbox/FoodModule.kt b/app/src/main/java/com/sw/inbound/objbox/FoodModule.kt index 49c563b..ae77b33 100644 --- a/app/src/main/java/com/sw/inbound/objbox/FoodModule.kt +++ b/app/src/main/java/com/sw/inbound/objbox/FoodModule.kt @@ -145,7 +145,7 @@ object FoodModule { suspend fun getFoodScoreList(bitmap: Bitmap, queryCount: Int = 15): List { val floatArray = bitmap2FloatArray(bitmap, false) ?: return emptyList() - logInfo("向量:${floatArray.toJsonString()}") + logInfo("getFoodScoreList向量:${floatArray.toJsonString()}") val nameScoreList = queryFoodNameScore(floatArray, queryCount) if (nameScoreList.isEmpty()) { return emptyList() diff --git a/app/src/main/java/com/sw/inbound/utils/FileUtils.kt b/app/src/main/java/com/sw/inbound/utils/FileUtils.kt index 00da3c3..c3f8322 100644 --- a/app/src/main/java/com/sw/inbound/utils/FileUtils.kt +++ b/app/src/main/java/com/sw/inbound/utils/FileUtils.kt @@ -2,6 +2,8 @@ package com.sw.inbound.utils import android.content.ContentUris import android.content.Context +import android.graphics.Bitmap +import android.graphics.BitmapFactory import android.net.Uri import android.os.Build import android.provider.MediaStore @@ -156,4 +158,45 @@ object FileUtils { 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 + } + } \ No newline at end of file