Commit b31b21f7 authored by liuyang's avatar liuyang

APK图片抓取识别及轮播流地址抓取, 客户端增加电话号码识别

#BYLSERVER-1438
parent 41174c60
......@@ -46,7 +46,7 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation(name: 'appbase-release', ext: 'aar')
implementation 'com.android.volley:volley:1.1.0'
implementation 'com.android.volley:volley:1.1.1'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'org.nanohttpd:nanohttpd:2.2.0'
implementation(name: 'vpnadaptercore-release', ext: 'aar')
......
......@@ -19,3 +19,6 @@
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
-keep class com.baidu.ocr.sdk.**{*;}
-dontwarn com.baidu.ocr.**
......@@ -10,6 +10,7 @@ import com.duolebo.appbase.IAppBaseCallback
import com.duolebo.appbase.IProtocol
import com.duolebo.blyrobot.protocol.ApkReportProtocol
import com.duolebo.blyrobot.tools.FtpManager
import com.duolebo.blyrobot.tools.OcrManager
import com.duolebo.blyrobot.util.AdbUtil
import com.duolebo.blyrobot.util.AppUtil
import com.duolebo.blyrobot.util.Config
......@@ -20,6 +21,7 @@ import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList
/**
* 任务类
*/
......@@ -137,6 +139,7 @@ class Task : Thread, IAppBaseCallback {
// 退出应用
AdbUtil.stopApp(this.apkInfo.packageName)
imageOcrToNumber();
// 上报数据,上传图片在后台进行
report()
}
......@@ -427,6 +430,24 @@ class Task : Thread, IAppBaseCallback {
this.uploadTask = uploadTask
}
private fun imageOcrToNumber() {
val channelArr = this.reportJson.optJSONArray("channels")
for ( i in 0..channelArr.length()) {
try {
val channelJson = channelArr.optJSONObject(i)
val imagePath = this.taskPath + "/" + channelJson.optString("channelImage")
val newImagePath = AppUtil.cropImage(imagePath)
val phoneNumber = OcrManager.instance.imageOcr(newImagePath)
channelJson.putOpt("phoneNumber", phoneNumber)
} catch (e : java.lang.Exception) {
e.printStackTrace()
}
}
}
/**
* 先上报数据,截图上传继续在后台进行
*/
......
......@@ -17,6 +17,7 @@ import com.duolebo.blyrobot.data.AppInfoData
import com.duolebo.blyrobot.data.Task
import com.duolebo.blyrobot.protocol.GetAppInfoProtocol
import com.duolebo.blyrobot.tools.FtpManager
import com.duolebo.blyrobot.tools.OcrManager
import com.duolebo.blyrobot.tools.TaskManager
import com.duolebo.blyrobot.util.AppUtil
import com.duolebo.blyrobot.util.Config
......@@ -46,6 +47,7 @@ class BylRobotService: Service(), IAppBaseCallback {
override fun onCreate() {
super.onCreate()
OcrManager.instance.init(this)
FtpManager.instance.start()
initBroadcast()
// requestAppList()
......
package com.duolebo.blyrobot.tools
import android.annotation.SuppressLint
import android.content.Context
import android.text.TextUtils
import com.baidu.aip.ocr.AipOcr
import java.io.File
class OcrManager {
companion object {
@SuppressLint("StaticFieldLeak")
val instance = OcrManager()
val TAG = "RobotOcrManager"
const val AK = "zy4gq9vvYa0UF133COi2ITHI"
const val SK = "cGG1wblcyMfpKjo8bG68lZYeysrEVOFq"
const val APP_ID = "11067709"
const val API_KEY = "APMf6m40GKnZK74ArXYmdMCf"
const val SECRET_KEY = "KfiA2R9BTWo5xN4R9gHeztMQ6h3seyZL"
const val CONN_TIMEOUT = 2000
const val SOCKET_TIMEOUT = 60000
}
private lateinit var context: Context
private lateinit var aipOcr: AipOcr
fun init(context: Context) {
this.context = context
initOcr()
}
// 初始化百度ocr调用
private fun initOcr() {
this.aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)
aipOcr.setConnectionTimeoutInMillis(CONN_TIMEOUT);
aipOcr.setSocketTimeoutInMillis(SOCKET_TIMEOUT);
}
fun imageOcr(filePath: String) : String {
var res = ""
try {
val options = HashMap<String, String>()
options["recognize_granularity"] = "big"
options["detect_direction"] = "true"
// 先用简单识别,每天有5w次免费额度
var result = aipOcr.basicGeneral(filePath, options)
res = getOcrWords(result)
// 如果无法识别,调用高精度识别,每天500次额度
if (!TextUtils.isDigitsOnly(res)) {
result = aipOcr.basicAccurateGeneral(filePath, options)
res = getOcrWords(result)
}
val imageFile = File(filePath)
imageFile.delete()
} catch (e: Exception) {
e.printStackTrace()
}
return res
}
private fun getOcrWords(result: org.json.JSONObject): String {
var words = ""
val wordsResult = result.getJSONArray("words_result")
val num = result.getInt("words_result_num")
if (wordsResult != null && num > 0) {
val wordsObject = wordsResult.getJSONObject(0)
words = wordsObject.getString("words")
words = words.replace("-", "")
}
return words
}
interface OcrListener {
fun onResult(result: String)
}
}
\ No newline at end of file
......@@ -96,20 +96,30 @@ object AppUtil {
}
}
fun pngToJpg2(pngFilePath: String, jpgFilePath: String) {
val bitmap: Bitmap? = BitmapFactory.decodeFile(pngFilePath)
fun cropImage(imagePath: String) : String {
val bitmap = BitmapFactory.decodeFile(imagePath)
val cropBitmap = Bitmap.createBitmap(bitmap, 0, 905, 1024, 100)
val index = imagePath.lastIndexOf('.')
val partName = imagePath.substring(0, index)
val newPath = partName + "_ocr.jpg"
val file = File(newPath)
try {
val bos = BufferedOutputStream(FileOutputStream(jpgFilePath))
bitmap?.run {
compress(Bitmap.CompressFormat.JPEG, 100, bos)
bos.flush()
val fos = FileOutputStream(file)
BufferedOutputStream(fos).use { bos ->
if (cropBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos)) {
bos.flush()
}
bos.close()
}
bos.close()
fos.close()
} catch (e: IOException) {
e.printStackTrace()
} finally {
bitmap!!.recycle()
cropBitmap!!.recycle()
}
return newPath
}
fun readFromLocal(file: File): String {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment