Commit 7ea9e582 authored by liuyang's avatar liuyang

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

#BYLSERVER-1438
parent b31b21f7
......@@ -128,6 +128,9 @@ class Task : Thread, IAppBaseCallback {
}
}
// 图片识别
imageOcrToNumber()
// 上传图片
this.uploadImage()
this.status = State.UPLOADING
......@@ -139,7 +142,6 @@ class Task : Thread, IAppBaseCallback {
// 退出应用
AdbUtil.stopApp(this.apkInfo.packageName)
imageOcrToNumber();
// 上报数据,上传图片在后台进行
report()
}
......@@ -271,12 +273,16 @@ class Task : Thread, IAppBaseCallback {
var time = imageDateFormat.format(Date())
val absName = this.taskPath + "/${this.apkInfo.packageName}_${channelIndex}_$time"
screenShot(absName)
AppUtil.cropImage("$absName.png")
screenImages.add("$absName.jpg")
// 删除png图片
val pngFile = File("$absName.png")
pngFile.delete()
AdbUtil.sendMultiKey(this.apkInfo.menuKeyEvent)
time = imageDateFormat.format(Date())
val absOkName = this.taskPath + "/${this.apkInfo.packageName}_${channelIndex}_${time}_ok"
screenShot(absOkName)
screenShot(absOkName, true)
screenImages.add("$absOkName.jpg")
// 对于一些epg弹出时间过长的应用,模拟点击一次返回键,比如电视家3.0
......@@ -407,7 +413,7 @@ class Task : Thread, IAppBaseCallback {
}
// 截图处理,转成jpg
private fun screenShot(absName: String) {
private fun screenShot(absName: String, deletePng: Boolean = false) {
val pngPath = "$absName.png"
val jpgPath = pngPath.replace(".png", ".jpg")
AdbUtil.screenShot(pngPath)
......@@ -415,7 +421,8 @@ class Task : Thread, IAppBaseCallback {
val pngFile = File(pngPath)
if (pngFile.exists()) {
AppUtil.pngToJpg(pngPath, jpgPath)
pngFile.delete()
if (deletePng)
pngFile.delete()
}
else {
Log.i(TAG, "screen shot failed")
......@@ -437,10 +444,15 @@ class Task : Thread, IAppBaseCallback {
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)
var imagePath = this.taskPath + "/" + channelJson.optString("channelImage")
imagePath = imagePath.replace(".jpg", "_ocr.jpg")
val phoneNumber = OcrManager.instance.imageOcr(imagePath)
channelJson.putOpt("phoneNumber", phoneNumber)
// 识别之后删除图片文件
val imageFile = File(imagePath)
imageFile.delete()
} catch (e : java.lang.Exception) {
e.printStackTrace()
}
......
......@@ -3,6 +3,7 @@ package com.duolebo.blyrobot.tools
import android.annotation.SuppressLint
import android.content.Context
import android.text.TextUtils
import android.util.Log
import com.baidu.aip.ocr.AipOcr
import java.io.File
......@@ -34,8 +35,8 @@ class OcrManager {
// 初始化百度ocr调用
private fun initOcr() {
this.aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)
aipOcr.setConnectionTimeoutInMillis(CONN_TIMEOUT);
aipOcr.setSocketTimeoutInMillis(SOCKET_TIMEOUT);
aipOcr.setConnectionTimeoutInMillis(CONN_TIMEOUT)
aipOcr.setSocketTimeoutInMillis(SOCKET_TIMEOUT)
}
fun imageOcr(filePath: String) : String {
......@@ -55,8 +56,7 @@ class OcrManager {
res = getOcrWords(result)
}
val imageFile = File(filePath)
imageFile.delete()
Log.i(TAG, "imageOrc: $res")
} catch (e: Exception) {
e.printStackTrace()
}
......
......@@ -96,30 +96,31 @@ object AppUtil {
}
}
fun cropImage(imagePath: String) : String {
fun cropImage(imagePath: 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 height = bitmap.height
val cropBitmap = Bitmap.createBitmap(bitmap, 0, height - 100, 1024, 100)
val index = imagePath.lastIndexOf('.')
val partName = imagePath.substring(0, index)
val newPath = partName + "_ocr.jpg"
val file = File(newPath)
val fos = FileOutputStream(file)
BufferedOutputStream(fos).use { bos ->
if (cropBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos)) {
if (cropBitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos)) {
bos.flush()
}
bos.close()
}
fos.close()
cropBitmap?.recycle()
} 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