Commit 7ea9e582 authored by liuyang's avatar liuyang

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

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