Commit 9815d536 authored by yinjiacheng's avatar yinjiacheng

update 金山云存储逻辑

parent bfd5e754
......@@ -31,5 +31,6 @@ class XRouterPathConstants {
const val MINE = "/mine"
const val LIFE_ACCOUNT_IDENTITY = "/lifeAccountIdentity"
const val LIFE_ACCOUNT_AUTH = "/lifeAccountAuth"
const val ID_CARD_TEST = "/idCardTest"
}
}
\ No newline at end of file
......@@ -18,6 +18,7 @@ import com.yidian.common.XRouterPathConstants.Companion.COLLECTION
import com.yidian.common.XRouterPathConstants.Companion.FLASH
import com.yidian.common.XRouterPathConstants.Companion.GALLERY
import com.yidian.common.XRouterPathConstants.Companion.HOME
import com.yidian.common.XRouterPathConstants.Companion.ID_CARD_TEST
import com.yidian.common.XRouterPathConstants.Companion.LIFE_ACCOUNT_AUTH
import com.yidian.common.XRouterPathConstants.Companion.LIFE_ACCOUNT_IDENTITY
import com.yidian.common.XRouterPathConstants.Companion.LOGIN_LIFE_CIRCLE
......@@ -42,13 +43,13 @@ import com.yidian.shenghuoquan.newscontent.action.ChameleonActionExecutor
import com.yidian.shenghuoquan.newscontent.service.*
import com.yidian.shenghuoquan.newscontent.ui.*
import com.yidian.shenghuoquan.newscontent.ui.author.AuthorHomePageActivity
import com.yidian.shenghuoquan.newscontent.ui.idcard.IDCardTestActivity
import com.yidian.shenghuoquan.newscontent.ui.mine.BrowsingHistoryActivity
import com.yidian.shenghuoquan.newscontent.ui.mine.CollectionActivity
import com.yidian.shenghuoquan.newscontent.ui.mine.MineActivity
import com.yidian.shenghuoquan.newscontent.ui.mine.SubscriptionActivity
import com.yidian.shenghuoquan.newscontent.ui.search.SearchChannelActivity
import com.yidian.shenghuoquan.newscontent.ui.search.SearchResultPageActivity
import com.yidian.shenghuoquan.newscontent.utils.Ks3Core
import com.yidian.xpage.XPageManager
import com.yidian.xpage.node.XPageHandler
import com.yidian.xpage.node.XPageNode
......@@ -144,6 +145,18 @@ class NewsContentApplication : YdBaseApplication() {
}
}),
XPageNode(ID_CARD_TEST, XPageNodePageType.NATIVE, object : XPageHandler {
override fun handler(params: Map<String, Any?>?) {
val intent = Intent()
if (params != null) {
intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
}
intent.setClass(context, IDCardTestActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(intent)
}
}),
XPageNode(NEWS_CONTENT, XPageNodePageType.NATIVE, object : XPageHandler {
override fun handler(params: Map<String, Any?>?) {
val intent = Intent()
......
......@@ -55,7 +55,7 @@ class ApiService {
}
//获取金山云token接口
fun getKSYunToken(requestParams: GetKSYunTokenBean.Request): HttpResult<GetKSYunTokenBean.Response?>? {
fun getKSYunToken(requestParams: GetKSYunTokenBean.Request): HttpResult<GetKSYunTokenBean.Response>? {
val paramsMap = HttpParamsUtils.getParamsMap()
return ServiceFactory.getInstance().createService(CommonService::class.java)
.getKSYunToken(paramsMap, requestParams)
......@@ -68,13 +68,13 @@ class ApiService {
ServiceFactory.getInstance().createService(CommonService::class.java)
.getIDCardOCR(paramsMap, requestParams)
.compose(TransformUtil.defaultSchedulers())
.subscribe(object : HttpResultSubscriber<GetIDCardOCRBean.Response?>() {
override fun onSuccess(result: HttpResult<GetIDCardOCRBean.Response?>?) {
apiCallback.getIDCardOCR(result)
.subscribe(object : HttpResultSubscriber<GetIDCardOCRBean.Response>() {
override fun onSuccess(result: HttpResult<GetIDCardOCRBean.Response>?) {
apiCallback.getIDCardOCRSuccess(result?.result)
}
override fun onFailer(result: HttpResult<GetIDCardOCRBean.Response?>?) {
apiCallback.getIDCardOCR(result)
override fun onFailer(result: HttpResult<GetIDCardOCRBean.Response>?) {
apiCallback.getIDCardOCRFailure(result?.status)
}
})
}
......
......@@ -111,11 +111,11 @@ interface CommonService {
@Headers("Content-Type: application/json")
@POST(URLs.getKSYunToken)
fun getKSYunToken(@QueryMap commonParams: Map<String, String>, @Body requestParams: GetKSYunTokenBean.Request): Call<HttpResult<GetKSYunTokenBean.Response?>>
fun getKSYunToken(@QueryMap commonParams: Map<String, String>, @Body requestParams: GetKSYunTokenBean.Request): Call<HttpResult<GetKSYunTokenBean.Response>>
@Headers("Content-Type: application/json")
@POST(URLs.getIDCardOCR)
fun getIDCardOCR(@QueryMap commonParams: Map<String, String>, @Body requestParams: GetIDCardOCRBean.Request): Observable<HttpResult<GetIDCardOCRBean.Response?>>
fun getIDCardOCR(@QueryMap commonParams: Map<String, String>, @Body requestParams: GetIDCardOCRBean.Request): Observable<HttpResult<GetIDCardOCRBean.Response>>
@Headers("Content-Type: application/json")
@POST(URLs.identifyIdOcrVerify)
......
package com.yidian.shenghuoquan.newscontent.http.httpbean
/**
* author: yinjiacheng
* date: 5/23/21 12:13 PM
* description: 获取身份证OCR
*/
class GetIDCardOCRBean(val request: Request, val response: Response) {
data class Request(
val posit_image_objectid: String,
val back_image_objectid: String,
val bucket: String
val posit_image_objectid: String?,
val back_image_objectid: String?
)
data class Response(
......
......@@ -6,5 +6,5 @@ package com.yidian.shenghuoquan.newscontent.http.httpbean
* description: 获取金山云ObjectId
*/
class GetKSYunObjectIdBean {
data class Response(val bucket: String, val objectid: String)
data class Response(val bucket: String, val objectId: String)
}
\ No newline at end of file
package com.yidian.shenghuoquan.newscontent.http.httpbean
import com.yidian.common.http.HttpResult
/**
* author: yinjiacheng
* date: 5/23/21 12:18 PM
* description: 获取身份证OCR
*/
interface IGetIDCardOCRCallback {
fun getIDCardOCR(t: HttpResult<GetIDCardOCRBean.Response?>?)
fun getIDCardOCRSuccess(result: GetIDCardOCRBean.Response?)
fun getIDCardOCRFailure(message: String?)
}
\ No newline at end of file
......@@ -4,8 +4,11 @@ import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.megvii.demo.activity.IDCardDetectActivity
import com.megvii.demo.utils.Configuration
......@@ -13,6 +16,12 @@ import com.megvii.idcardquality.IDCardQualityLicenseManager
import com.megvii.licensemanager.Manager
import com.yidian.common.base.BaseActivity
import com.yidian.shenghuoquan.newscontent.databinding.ActivityIdcardBinding
import com.yidian.shenghuoquan.newscontent.http.ApiService
import com.yidian.shenghuoquan.newscontent.http.httpbean.GetIDCardOCRBean
import com.yidian.shenghuoquan.newscontent.http.httpbean.IGetIDCardOCRCallback
import com.yidian.shenghuoquan.newscontent.utils.KS3Core
import java.io.File
import java.io.FileOutputStream
/**
......@@ -24,6 +33,11 @@ class IDCardTestActivity : BaseActivity<ActivityIdcardBinding>() {
private lateinit var mIdCardLicenseManager: IDCardQualityLicenseManager
private val INTO_IDCARDSCAN_PAGE = 100
private lateinit var idCardFront: Bitmap
private lateinit var idCardBack: Bitmap
private lateinit var idCardFrontObjectKey: String
private lateinit var idCardBackObjectKey: String
override fun createViewBinding(): ActivityIdcardBinding {
return ActivityIdcardBinding.inflate(layoutInflater)
}
......@@ -40,6 +54,82 @@ class IDCardTestActivity : BaseActivity<ActivityIdcardBinding>() {
//2、请求授权信息
startGetLicense()
}
viewBind.btnOcr.setOnClickListener {
val frontFile = File(externalCacheDir?.absolutePath + "/front.jpg")
if (frontFile.exists()) frontFile.delete() else frontFile.createNewFile()
val backFile = File(externalCacheDir?.absolutePath + "/back.jpg")
if (backFile.exists()) backFile.delete() else backFile.createNewFile()
idCardFront.compress(Bitmap.CompressFormat.JPEG, 100, FileOutputStream(frontFile))
idCardBack.compress(Bitmap.CompressFormat.JPEG, 100, FileOutputStream(backFile))
KS3Core.INSTANCE.uploadObject(frontFile, object : KS3Core.OnKS3TaskListener {
override fun onTaskStart() {
}
override fun onTaskProgress(progress: Double) {
}
override fun onTaskFinish() {
}
override fun onTaskCancel() {
}
override fun onTaskSuccess(bucket: String?, objectKey: String?) {
if (objectKey != null) {
idCardFrontObjectKey = objectKey
}
KS3Core.INSTANCE.uploadObject(backFile, object : KS3Core.OnKS3TaskListener {
override fun onTaskStart() {
}
override fun onTaskProgress(progress: Double) {
}
override fun onTaskFinish() {
}
override fun onTaskCancel() {
}
override fun onTaskSuccess(bucket: String?, objectKey: String?) {
if (objectKey != null) {
idCardBackObjectKey = objectKey
}
val request = GetIDCardOCRBean.Request(idCardFrontObjectKey, idCardBackObjectKey)
ApiService.getIDCardOCR(object : IGetIDCardOCRCallback {
override fun getIDCardOCRSuccess(result: GetIDCardOCRBean.Response?) {
Log.d(KS3Core.TAG, "name: ${result?.posit?.name}, id num: ${result?.posit?.idcard_number}")
}
override fun getIDCardOCRFailure(message: String?) {
}
}, request)
}
override fun onTaskFailure(statesCode: Int, message: String?) {
}
})
}
override fun onTaskFailure(statesCode: Int, message: String?) {
}
})
}
requestCameraPerm()
......@@ -91,7 +181,11 @@ class IDCardTestActivity : BaseActivity<ActivityIdcardBinding>() {
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
//@TODO 权限通过后的回调
......@@ -128,14 +222,16 @@ class IDCardTestActivity : BaseActivity<ActivityIdcardBinding>() {
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == INTO_IDCARDSCAN_PAGE && resultCode == Activity.RESULT_OK) {
val intent: Intent = Intent(this, IDCardResultActivity::class.java)
if (data != null) {
intent.putExtra("portraitimg_bitmap", data.getByteArrayExtra("portraitimg_bitmap"))
}
if (data != null) {
intent.putExtra("idcardimg_bitmap", data.getByteArrayExtra("idcardimg_bitmap"))
val bytes = data.getByteArrayExtra("idcardimg_bitmap")
if (Configuration.getCardType(this) == 1) {
idCardFront = BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
viewBind.resultIdcardImageFront.setImageBitmap(idCardFront)
} else {
idCardBack = BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
viewBind.resultIdcardImageBack.setImageBitmap(idCardBack)
}
}
startActivity(intent)
}
}
}
\ No newline at end of file
......@@ -26,20 +26,14 @@ import java.io.File
* date: 5/20/21 11:08 AM
* description: 金山云存储
*/
class Ks3Core private constructor(val context: Context) : AuthListener {
class KS3Core private constructor(val context: Context) : AuthListener {
private val client by lazy { Ks3Client(this, context) }
private val gson by lazy { Gson() }
private val onKs3UploadListenerList: ArrayList<OnKs3UploadListener> by lazy { ArrayList<OnKs3UploadListener>() }
private val putObjectResponseHandler by lazy {
object : PutObjectResponseHandler() {
override fun onTaskProgress(progress: Double) {
// work thread
if (onKs3UploadListenerList.size > 0) {
for (listener in onKs3UploadListenerList) {
listener.onUploadProgress(progress)
}
}
}
override fun onTaskFailure(
......@@ -53,44 +47,22 @@ class Ks3Core private constructor(val context: Context) : AuthListener {
TAG,
"put object fail:, errorMsg: ${error?.errorMessage}"
)
if (onKs3UploadListenerList.size > 0) {
for (listener in onKs3UploadListenerList) {
listener.onUploadFailure(error?.errorMessage)
}
}
}
override fun onTaskSuccess(statesCode: Int, responceHeaders: Array<out Header>?) {
Log.d(TAG, "put object success.")
if (onKs3UploadListenerList.size > 0) {
for (listener in onKs3UploadListenerList) {
listener.onUploadSuccess()
}
}
}
override fun onTaskStart() {
if (onKs3UploadListenerList.size > 0) {
for (listener in onKs3UploadListenerList) {
listener.onUploadStart()
}
}
}
override fun onTaskFinish() {
if (onKs3UploadListenerList.size > 0) {
for (listener in onKs3UploadListenerList) {
listener.onUploadFinish()
}
}
}
override fun onTaskCancel() {
if (onKs3UploadListenerList.size > 0) {
for (listener in onKs3UploadListenerList) {
listener.onUploadCancel()
}
}
}
}
}
......@@ -114,13 +86,13 @@ class Ks3Core private constructor(val context: Context) : AuthListener {
}
companion object {
const val TAG = "Ks3Core"
const val TAG = "KS3Core"
const val DEFAULT_BUCKET_NAME = "bp-yidian"
const val DEFAULT_END_POINT = "ks3-cn-beijing.ksyun.com"
const val VIDEO_SUFFIX = ".mp4"
const val IMAGE_SUFFIX = ".png"
val instance: Ks3Core by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
Ks3Core(context = YdBaseApplication.context)
val INSTANCE: KS3Core by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
KS3Core(context = YdBaseApplication.context)
}
}
......@@ -140,18 +112,46 @@ class Ks3Core private constructor(val context: Context) : AuthListener {
* 文件上传
* @param path 本地文件全路径
*/
fun uploadObject(path: String) {
fun uploadObject(path: String, listener: OnKS3TaskListener) {
ApiService.getKSYunObjectId(object : IGetKSYunObjectIdCallback {
override fun getKSYunObjectIdSuccess(result: GetKSYunObjectIdBean.Response?) {
Log.d(
TAG,
"request getKSYun objectId success, objectId: ${result?.objectid}, bucket: ${result?.bucket}"
"request getKSYun objectId success, objectId: ${result?.objectId}, bucket: ${result?.bucket}"
)
// 执行金山云上传
// val request = PutObjectRequest(result?.bucket, result?.objectid, File(path))
val request = PutObjectRequest(DEFAULT_BUCKET_NAME, result?.objectid, File(path))
val request = PutObjectRequest(result?.bucket, result?.objectId, File(path))
request.cannedAcl = CannedAccessControlList.PublicRead
client.putObject(request, putObjectResponseHandler)
client.putObject(
request,
KS3CorePutObjectResponseHandler(result?.bucket, result?.objectId, listener)
)
}
override fun getKSYunObjectIdFailure(message: String?) {
Log.e(TAG, "request get KSYun objectId fail, message: $message")
}
})
}
/**
* 文件上传
* @param file 文件实体
*/
fun uploadObject(file: File, listener: OnKS3TaskListener) {
ApiService.getKSYunObjectId(object : IGetKSYunObjectIdCallback {
override fun getKSYunObjectIdSuccess(result: GetKSYunObjectIdBean.Response?) {
Log.d(
TAG,
"request getKSYun objectId success, objectId: ${result?.objectId}, bucket: ${result?.bucket}"
)
// 执行金山云上传
val request = PutObjectRequest(result?.bucket, result?.objectId, file)
request.cannedAcl = CannedAccessControlList.PublicRead
client.putObject(
request,
KS3CorePutObjectResponseHandler(result?.bucket, result?.objectId, listener)
)
}
override fun getKSYunObjectIdFailure(message: String?) {
......@@ -202,26 +202,16 @@ class Ks3Core private constructor(val context: Context) : AuthListener {
return ApiService.getKSYunToken(GetKSYunTokenBean.Request(tok))?.result?.token
}
fun addOnKs3UploadListener(listener: OnKs3UploadListener) {
if (!onKs3UploadListenerList.contains(listener)) {
onKs3UploadListenerList.add(listener)
}
}
fun removeOnKs3UploadListener(listener: OnKs3UploadListener) {
onKs3UploadListenerList.remove(listener)
}
/**
* 业务监听
*/
interface OnKs3UploadListener {
fun onUploadStart()
fun onUploadProgress(progress: Double)
fun onUploadFinish()
fun onUploadCancel()
fun onUploadSuccess()
fun onUploadFailure(message: String?)
interface OnKS3TaskListener {
fun onTaskStart()
fun onTaskProgress(progress: Double)
fun onTaskFinish()
fun onTaskCancel()
fun onTaskSuccess(bucket: String?, objectKey: String?)
fun onTaskFailure(statesCode: Int, message: String?)
}
/**
......@@ -231,4 +221,50 @@ class Ks3Core private constructor(val context: Context) : AuthListener {
VIDEO, IMAGE
}
/**
* 金山云object上传handler
*/
class KS3CorePutObjectResponseHandler(
private val bucket: String?,
private val objectKey: String?,
val listener: OnKS3TaskListener
) : PutObjectResponseHandler() {
override fun onTaskProgress(progress: Double) {
// work thread
listener.onTaskProgress(progress)
}
override fun onTaskFailure(
statesCode: Int,
error: Ks3Error?,
responceHeaders: Array<out Header>?,
response: String?,
throwable: Throwable?
) {
Log.e(
TAG,
"put object fail:, errorMsg: ${error?.errorMessage}"
)
listener.onTaskFailure(statesCode, error?.errorMessage)
}
override fun onTaskSuccess(statesCode: Int, responceHeaders: Array<out Header>?) {
Log.d(TAG, "put object success.")
listener.onTaskSuccess(bucket, objectKey)
}
override fun onTaskStart() {
listener.onTaskStart()
}
override fun onTaskFinish() {
listener.onTaskFinish()
}
override fun onTaskCancel() {
listener.onTaskCancel()
}
}
}
\ No newline at end of file
......@@ -82,4 +82,30 @@
android:textColor="@android:color/white"
android:textSize="22sp" />
<ImageView
android:id="@+id/result_idcard_image_front"
android:layout_width="300dp"
android:layout_height="200dp"
android:scaleType="fitCenter"
android:layout_centerInParent="true"/>
<ImageView
android:id="@+id/result_idcard_image_back"
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_marginTop="10dp"
android:scaleType="fitCenter"
android:layout_centerHorizontal="true"
android:layout_below="@id/result_idcard_image_front" />
<Button
android:id="@+id/btn_ocr"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="开始OCR识别"
android:textColor="@android:color/white"
android:textSize="22sp" />
</RelativeLayout>
\ No newline at end of file
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