Selaa lähdekoodia

处理文字识别结果并输入到算法中。

Ulric 2 kuukautta sitten
vanhempi
commit
93aee3c45e

+ 6 - 0
app/src/main/java/com/ys/imageProcess/cv/Process.java

@@ -19,6 +19,12 @@ public class Process {
         public double[] widthContour;
     }
 
+    /**
+     * @param bitmap 图像
+     * @param um 比例尺长度 (um)
+     * @param pos 文字左上角坐标
+     * @return 识别结果
+     */
     public native DetectResult Detect(Bitmap bitmap, int um, int[] pos);
 
     static {

+ 8 - 3
app/src/main/java/com/ys/imageProcess/utils/StringTools.kt

@@ -1,6 +1,5 @@
 package com.ys.imageProcess.utils
 
-import java.sql.Timestamp
 import java.text.SimpleDateFormat
 import java.util.Date
 import java.util.Locale
@@ -33,8 +32,14 @@ fun formatDate(timestamp: Long, pattern: String, locale: Locale): String {
 
 fun scaleLength(label: String): Int {
     var len = 100
-    if (label.contains("um")) {
-        len = label.toInt()
+    if (label.isNotEmpty()) {
+        var str = ""
+        label.forEach {
+            if (it.isDigit()) {
+                str += it
+            }
+        }
+        len = str.toInt()
     }
 
     return len

+ 9 - 9
app/src/main/java/com/ys/imageProcess/viewModel/WorkViewModel.kt

@@ -1,13 +1,10 @@
 package com.ys.imageProcess.viewModel
 
-import android.content.ContentResolver
 import android.content.ContentValues.TAG
 import android.content.Context
-import android.database.Cursor
 import android.graphics.Bitmap
 import android.graphics.PointF
 import android.net.Uri
-import android.provider.MediaStore
 import android.util.Log
 import androidx.compose.ui.geometry.Offset
 import androidx.compose.ui.geometry.Rect
@@ -27,7 +24,6 @@ import com.ys.imageProcess.ui.work.zoomStep
 import com.ys.imageProcess.utils.fileSizeString
 import com.ys.imageProcess.utils.formatDate
 import com.ys.imageProcess.utils.scaleLength
-import com.ys.imageProcess.utils.uriToBitmap
 import com.ys.imageProcess.utils.zoomToFit
 import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.Dispatchers
@@ -123,7 +119,10 @@ class WorkViewModel : ViewModel() {
                 val bitmap = ProcessDataStore.getProcessedImage(name)
 
                 val ocrRes = OCRModelLoader.runModel(bitmap!!)
-                Log.i(TAG, "OCR result: $ocrRes")
+                for (model in ocrRes) {
+                    val scale = scaleLength(model.label)
+                    Log.i(TAG, "OCR result: ${model.label}, $scale, ${model.points}")
+                }
 
                 val store = ProcessedData(bitmap)
                 store.setMap(storedMap)
@@ -135,19 +134,20 @@ class WorkViewModel : ViewModel() {
                     isAlgRunning = true
                     updateInfoMap()
                     val ocrRes = OCRModelLoader.runModel(it)
-                    var ocrStr = 100
+                    var scale = 100
                     val ocrPos = mutableListOf<Int>()
                     if (ocrRes.isNotEmpty()) {
-                        ocrStr = scaleLength(ocrRes.first().label)
+                        scale = scaleLength(ocrRes.first().label)
                         for (point in ocrRes.first().points) {
                             ocrPos.add(point.x)
                             ocrPos.add(point.y)
+                            break
                         }
                     }
 
                     val result =
-                        Process().Detect(it, ocrStr, ocrPos.toIntArray())
-                    Log.i(TAG, "--- $result")
+                        Process().Detect(it, scale, ocrPos.toIntArray())
+                    Log.i(TAG, "--- $scale, $ocrPos")
                     val processed = ProcessedData(result.processedImage, result)
                     ProcessDataStore.saveResult(processed.map(), name)
                     ProcessDataStore.saveProcessedImage(