Skip to content

Commit

Permalink
feat(app): ocr support auto detect language
Browse files Browse the repository at this point in the history
  • Loading branch information
Leon406 committed Jul 29, 2023
1 parent 5221f2a commit 81467ba
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
12 changes: 7 additions & 5 deletions app/src/main/kotlin/me/leon/ext/ocr/BaiduOcr.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package me.leon.ext.ocr

import me.leon.ext.*
import me.leon.ext.fromJson
import me.leon.ext.fx.Prefs
import tornadofx.*
import me.leon.ext.readBytesFromNet
import me.leon.ext.simpleReadFromNet
import tornadofx.urlEncoded

object BaiduOcr {
private var token: String? = null
Expand All @@ -16,11 +18,11 @@ object BaiduOcr {
?.also { token = it as String }

fun ocr(url: String): String {
return ocrData("url=$url")
return ocrData("url=$url&detect_language=true")
}

fun ocrBase64(base64: String): String {
return ocrData("image=${base64.urlEncoded}")
return ocrData("image=${base64.urlEncoded}&detect_language=true")
}

private fun ocrData(data: String): String {
Expand All @@ -38,6 +40,6 @@ object BaiduOcr {
.fromJson(BaiduOcrBean::class.java)
.results
?.joinToString(System.lineSeparator()) { it.words }
?: kotlin.error("request failed")
?: error("request failed")
}
}
1 change: 1 addition & 0 deletions app/src/test/kotlin/me/leon/Ocr.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Ocr {

@Test
fun fileOcr() {
println(BaiduOcr.ocrBase64(File(TEST_OCR_DIR, "ja.jpg").toBase64()))
assertTrue(BaiduOcr.ocrBase64(File(TEST_OCR_DIR, "ocr.jpg").toBase64()).contains("你们别看我平时"))
}

Expand Down
2 changes: 1 addition & 1 deletion plugin-apipost/src/test/kotlin/me/leon/JbLicenseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ class JbLicenseTest {
.map { it.first }
.also {
println("${it.size} / ${sortedServers.size} ")
println("\tfail\n${sortedServers - it}")
println("\tok\n${it.joinToString("\n")}")
println("\tfail\n${(sortedServers - it).joinToString("\n")}")
}
}
}
Expand Down
Binary file added testdata/qrcode/ja.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 81467ba

Please sign in to comment.