This repository has been archived by the owner on Aug 12, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ffeda0a
commit 29dfc15
Showing
10 changed files
with
306 additions
and
0 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
qqinterface/src/main/java/com/tencent/mobileqq/l0/b/a.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.tencent.mobileqq.l0.b; | ||
|
||
public class a { | ||
public void b () { | ||
|
||
} | ||
|
||
public void c (c cVar) { | ||
|
||
} | ||
|
||
public void onUpdate(int progress, boolean z, Object obj) { | ||
|
||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
qqinterface/src/main/java/com/tencent/mobileqq/l0/b/b.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.tencent.mobileqq.l0.b; | ||
|
||
import android.graphics.Point; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class b { | ||
// text | ||
public String a; | ||
|
||
// confidence | ||
public int b; | ||
|
||
// coordinates | ||
public ArrayList<Point> c; | ||
|
||
/* renamed from: d */ | ||
public int d; | ||
|
||
/* renamed from: e */ | ||
public boolean e; | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
qqinterface/src/main/java/com/tencent/mobileqq/l0/b/c.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.tencent.mobileqq.l0.b; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
|
||
public class c { | ||
// image | ||
public String a; | ||
|
||
// width | ||
public int b; | ||
|
||
// height | ||
public int c; | ||
|
||
// lang | ||
public String d; | ||
|
||
// url | ||
public String e; | ||
|
||
// results | ||
public ArrayList<b> f; | ||
|
||
public ArrayList<String> g; | ||
|
||
public HashMap<String, String> h; | ||
|
||
public int i; | ||
|
||
public int j; | ||
|
||
public int k; | ||
|
||
public String l; | ||
|
||
public int m; | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
qqinterface/src/main/java/com/tencent/mobileqq/ocr/a/a.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.tencent.mobileqq.ocr.a; | ||
|
||
public class a { | ||
public String a; | ||
|
||
// 1 | ||
public int b; | ||
|
||
// file location | ||
public String c; | ||
|
||
// null | ||
public String d; | ||
|
||
// 0 | ||
public long e; | ||
|
||
// md5 | ||
public String f; | ||
|
||
// null | ||
public String g; | ||
|
||
// false | ||
public boolean h; | ||
|
||
// 0 | ||
public int i; | ||
|
||
// 0 | ||
public int j; | ||
|
||
// 0 | ||
public long k; | ||
|
||
// null | ||
public String l; | ||
} |
10 changes: 10 additions & 0 deletions
10
qqinterface/src/main/java/com/tencent/mobileqq/ocr/api/IPicOcrService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.tencent.mobileqq.ocr.api; | ||
|
||
import mqq.app.api.IRuntimeService; | ||
|
||
public interface IPicOcrService extends IRuntimeService { | ||
void putOcrResult(String str, com.tencent.mobileqq.l0.b.c cVar); | ||
|
||
void requestOcr(com.tencent.mobileqq.ocr.a.a aVar, com.tencent.mobileqq.l0.b.a callback); | ||
|
||
} |
4 changes: 4 additions & 0 deletions
4
qqinterface/src/main/java/com/tencent/mobileqq/ocr/api/impl/OcrServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.tencent.mobileqq.ocr.api.impl; | ||
|
||
public class OcrServiceImpl { | ||
} |
4 changes: 4 additions & 0 deletions
4
qqinterface/src/main/java/com/tencent/mobileqq/ocr/api/impl/PicOcrServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.tencent.mobileqq.ocr.api.impl; | ||
|
||
public class PicOcrServiceImpl { | ||
} |
134 changes: 134 additions & 0 deletions
134
xposed/src/main/java/moe/fuqiuluo/shamrock/remote/action/handlers/OcrImage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package moe.fuqiuluo.shamrock.remote.action.handlers | ||
|
||
import com.tencent.mobileqq.l0.b.c | ||
import com.tencent.mobileqq.ocr.a.a | ||
import com.tencent.mobileqq.ocr.api.IPicOcrService | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.suspendCancellableCoroutine | ||
import kotlinx.coroutines.withContext | ||
import kotlinx.coroutines.withTimeoutOrNull | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.JsonElement | ||
import moe.fuqiuluo.qqinterface.servlet.BaseSvc | ||
import moe.fuqiuluo.shamrock.helper.Level | ||
import moe.fuqiuluo.shamrock.helper.LogCenter | ||
import moe.fuqiuluo.shamrock.remote.action.ActionSession | ||
import moe.fuqiuluo.shamrock.remote.action.IActionHandler | ||
import moe.fuqiuluo.shamrock.tools.EmptyJsonString | ||
import moe.fuqiuluo.shamrock.utils.FileUtils | ||
import moe.fuqiuluo.shamrock.utils.MD5 | ||
import moe.fuqiuluo.symbols.OneBotHandler | ||
import java.io.File | ||
import kotlin.coroutines.resume | ||
|
||
@OneBotHandler("ocr_image") | ||
internal object OcrImage: IActionHandler() { | ||
override suspend fun internalHandle(session: ActionSession): String { | ||
val image = session.getString("image") | ||
return invoke(image, session.echo) | ||
} | ||
|
||
suspend operator fun invoke(image: String, echo: JsonElement = EmptyJsonString): String { | ||
val file = FileUtils.getFile(image).let { | ||
if (!it.exists()) { | ||
return@let FileUtils.parseAndSave(image) | ||
} | ||
it | ||
} | ||
LogCenter.log("对${file.absoluteFile.absolutePath}进行OCR", Level.DEBUG) | ||
val retryCount = 10 | ||
val delayMillis = 1000L | ||
repeat(retryCount) { | ||
val result = getOcrResult(file) | ||
|
||
if (result.isSuccess) { | ||
return ok(result.getOrNull(), echo) | ||
} else if (result.isFailure && result.exceptionOrNull()?.message == "no cache") { | ||
withContext(Dispatchers.IO) { | ||
Thread.sleep(delayMillis) | ||
} | ||
} else { | ||
return error(result.exceptionOrNull()?.message ?: "", echo) | ||
} | ||
} | ||
return error("ocr failed", echo) | ||
} | ||
|
||
private suspend fun getOcrResult(file: File): Result<OcrResult> { | ||
val ocrService = BaseSvc.app.getRuntimeService(IPicOcrService::class.java, "all") | ||
?: return Result.failure(Error("获取OCR服务失败")) | ||
return withTimeoutOrNull(5000) { | ||
suspendCancellableCoroutine { continuation -> | ||
ocrService.requestOcr( | ||
a().apply { | ||
this.b = 1 | ||
this.c = file.absolutePath | ||
this.f = MD5.genFileMd5Hex(file.absolutePath) | ||
}, object : com.tencent.mobileqq.l0.b.a() { | ||
override fun b() { | ||
// call uploadOcrPic and then call b | ||
continuation.resume(Result.failure(Error("no cache"))) | ||
} | ||
|
||
override fun c(result: c?) { | ||
continuation.resume(Result.success(OcrResult( | ||
texts = result?.f?.map { | ||
TextDetection( | ||
text = it.a, | ||
confidence = it.b, | ||
coordinates = it.c.map { | ||
ArrayList<Int>().apply { | ||
add(it.x) | ||
add(it.y) | ||
} | ||
} | ||
) | ||
}!!, | ||
language = result.h[result.d] ?: result.d, | ||
url = result.e | ||
))) | ||
} | ||
override fun onUpdate(i2: Int, z: Boolean, obj: Any?) { | ||
LogCenter.log("$i2, $z, ${obj.toString()}") | ||
if (i2 == 100) { | ||
val result = obj as c | ||
continuation.resume(Result.success(OcrResult( | ||
texts = result.f.map { | ||
TextDetection( | ||
text = it.a, | ||
confidence = it.b, | ||
coordinates = it.c.map { | ||
ArrayList<Int>().apply { | ||
add(it.x) | ||
add(it.y) | ||
} | ||
} | ||
) | ||
}, | ||
language = result.h[result.d] ?: result.d, | ||
url = result.e | ||
))) | ||
} | ||
} | ||
}) | ||
|
||
} | ||
} ?: Result.failure(Exception("OCR timed out")) | ||
} | ||
|
||
override val requiredParams: Array<String> = arrayOf("group_id", "user_id") | ||
|
||
@Serializable | ||
data class OcrResult ( | ||
val texts: List<TextDetection>, | ||
val language: String, | ||
val url: String | ||
) | ||
|
||
@Serializable | ||
data class TextDetection ( | ||
val text: String, | ||
val confidence: Int, | ||
val coordinates: List<List<Int>> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters