activity_youdao_ques.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:visibility="visible"> <RelativeLayout android:layout_width="match_parent" android:layout_height="48dp" android:background="@color/white"> <ImageView android:id="@+id/back_btn" android:layout_width="38dp" android:layout_height="38dp" android:layout_centerVertical="true" android:layout_marginStart="5dp" android:background="@color/transparent" android:onClick="clickBack" android:padding="9dp" android:src="@mipmap/back_ic" /> <com.google.android.material.tabs.TabLayout android:id="@+id/tl_tabs" android:layout_width="wrap_content" android:layout_height="@dimen/dp50" android:layout_centerInParent="true" android:overScrollMode="never" app:tabIndicatorFullWidth="false" app:tabIndicatorHeight="0dp" app:tabMinWidth="@dimen/dp24" app:tabMode="scrollable" app:tabPaddingEnd="@dimen/dp16" app:tabPaddingStart="@dimen/dp16" app:tabRippleColor="@color/transparent" app:tabSelectedTextColor="@color/white" app:tabTextColor="@color/black" /> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:layout_alignParentBottom="true" android:background="@color/line_color" /> </RelativeLayout> <ImageView android:id="@+id/previewImg" android:layout_width="match_parent" android:layout_height="@dimen/dp200" android:scaleType="fitXY" /> <androidx.viewpager.widget.ViewPager android:id="@+id/vp_content" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:layout_alignParentBottom="true" android:orientation="horizontal"> <LinearLayout android:id="@+id/addWrongBookLinLayout" android:layout_width="@dimen/dp0" android:layout_height="match_parent" android:layout_marginStart="@dimen/dp26" android:layout_marginTop="@dimen/dp7" android:layout_marginEnd="@dimen/dp14" android:layout_marginBottom="@dimen/dp7" android:layout_weight="1" android:background="@drawable/btn_bg_oval_slide" android:gravity="center"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/icon_wrong_book"></ImageView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/dp6" android:text="加入错题本" android:textColor="#ff333333" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:id="@+id/stayBeatTopicLinLayout" android:layout_width="@dimen/dp0" android:layout_height="match_parent" android:layout_marginTop="@dimen/dp7" android:layout_marginEnd="@dimen/dp26" android:layout_marginBottom="@dimen/dp7" android:layout_weight="1" android:background="@drawable/btn_topic_shape_bg" android:gravity="center"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/icon_photo"></ImageView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/dp6" android:text="再拍一题" android:textColor="#ffffffff" android:textSize="16sp" /> </LinearLayout> </LinearLayout> </LinearLayout> package com.sskj.parent.ui.youdao import android.content.Intent import android.graphics.Bitmap import android.graphics.BitmapFactory import android.os.Bundle import android.text.TextUtils import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.LinearLayout import android.widget.TextView import com.blankj.utilcode.util.LogUtils import com.google.android.material.tabs.TabLayout import com.google.gson.Gson import com.rmondjone.camera.CameraActivity import com.sskj.base.core.base.EcardActivity import com.sskj.base.core.common.app.AppStatusTracker import com.sskj.base.core.common.constant.APP_OFFLINE import com.sskj.base.core.common.constant.APP_ONLINE import com.sskj.base.core.data.UserCacheManager import com.sskj.base.core.ext.toast import com.sskj.parent.R import com.sskj.parent.data.entity.FragmentInfo import com.sskj.parent.data.entity.SearchQuestionResult import com.sskj.parent.ui.adapter.fragment.TabFragment import com.sskj.parent.utils.ImageUtils import com.youdao.ocr.question.OCRListener import com.youdao.ocr.question.OCRParameters import com.youdao.ocr.question.OcrErrorCode import com.youdao.ocr.question.SearchingImageQuestion import com.youdao.sdk.app.EncryptHelper import kotlinx.android.synthetic.main.activity_youdao_ques.* import java.io.ByteArrayOutputStream /** * 拍照搜题界面 */ class YouDaoSearchQues : EcardActivity<Nothing>() { lateinit var fragmentList: ArrayList<FragmentInfo> companion object { const val REQUEST_SMALL: Int = 1 } override val mLayout: Any get() = R.layout.activity_youdao_ques override fun beforeSetContentView() { super.beforeSetContentView() var loginIsValid = UserCacheManager.getCacheToken() if (!loginIsValid.isNullOrEmpty()) { UserCacheManager.clearUserCache() } AppStatusTracker.appStatus = if (!loginIsValid.isNullOrEmpty()) APP_ONLINE else APP_OFFLINE fragmentList = ArrayList() } override fun onBindView(savedInstanceState: Bundle?) { var intent = Intent(context, CameraActivity::class.java) // intent.putExtra(CameraActivity.TAKE_PHOTO_NUMBER,3) intent.putExtra(CameraActivity.KEY_IMAGE_CROP, true) startActivityForResult(intent, REQUEST_SMALL) addWrongBookLinLayout.setOnClickListener { //TODO 加入错题本 } stayBeatTopicLinLayout.setOnClickListener { //TODO 再拍一题 } } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { //裁剪照片 if (requestCode == REQUEST_SMALL) { layout.visibility = View.VISIBLE data?.apply { showLoading() var imagePath = data.getStringArrayListExtra(CameraActivity.KEY_IMAGE_PATH) var s = imagePath[0] var bitmap = BitmapFactory.decodeFile(s.replace("file://", "")) //加载默认图 bitmap = ImageUtils.readBitmapFromAsset(resources, R.drawable.ocr_question) //设置显示图片 previewImg.setImageBitmap(bitmap) var quality: Int = 100 var baos = ByteArrayOutputStream() bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos) var datas: ByteArray = baos.toByteArray() var bases64 = EncryptHelper.getBase64(datas) var count: Int = bases64.length while (count > 0.5 * 1024 * 1024) { quality -= 10 baos = ByteArrayOutputStream() bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos) datas = baos.toByteArray() bases64 = EncryptHelper.getBase64(datas) count = bases64.length } //搜题 searchQuestions(datas) } } super.onActivityResult(requestCode, resultCode, data) } /** * 有道搜题 */ fun searchQuestions(datas: ByteArray) { //构造识别输入对象 val tps = OCRParameters .Builder() .timeout(100000) .packageName(packageName) .build() //将图片转化成base64编码 val base64 = EncryptHelper.getBase64(datas) //设置回调,开始搜索 整个识别是异步的,回调在子线程进行,若涉及到界面操作,请切回主线程。 SearchingImageQuestion.getInstance(tps).startSearching(base64, object : OCRListener { override fun onError(error: OcrErrorCode) { runOnUiThread { LogUtils.d(error.code) LogUtils.d(error.toString()) hideLoading() if (error.code == 0) { toast("对不起,没有搜到这道题") } else { toast(error.code.toString() + error.toString()) } } } override fun onResult(result: String) { runOnUiThread { hideLoading() LogUtils.d(result) if (!TextUtils.isEmpty(result)) { showQuestionResult(result) } } } }) } private fun showQuestionResult(result: String) { var gson = Gson() var searchQuestionResult = gson.fromJson(result, SearchQuestionResult::class.java) var list = searchQuestionResult.questions for (i in list?.indices!!) { PhotoSearchFragment().newInstance(result, i)?.let { FragmentInfo( i.toString(), it ) }?.let { fragmentList.add(it) } } var fragmentAdapter = TabFragment(supportFragmentManager, fragmentList) vp_content.adapter = fragmentAdapter tl_tabs.setupWithViewPager(vp_content) for (i in 0 until fragmentAdapter.count) { val tab: TabLayout.Tab? = tl_tabs.getTabAt(i) //获得每一个tab tab?.setCustomView(R.layout.view_venue_tab) //给每一个tab设置view if (i == 0) { // 设置第一个tab的TextView是被选择的样式 tab?.customView!!.findViewById<View>(R.id.tvTitle).isSelected = true //第一个tab被选中 } tab?.customView?.layoutParams = LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT ) val textView = tab?.customView!!.findViewById<View>(R.id.tvTitle) as TextView textView.text = i.toString() //设置tab上的文字 } } }view_venue_tab.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/tvTitle" android:layout_width="@dimen/dp24" android:layout_height="@dimen/dp24" android:layout_centerInParent="true" android:background="@drawable/tab_background" android:gravity="center" android:textColor="@color/white"></TextView> </RelativeLayout>btn_bg_oval_slide.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/iv_menu_choosed" android:state_selected="true" /> <item android:drawable="@drawable/iv_menu_normal" android:state_selected="false" android:state_focused="false" android:state_pressed="false" /> </selector>btn_bg_oval_slide.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" android:useLevel="false"> <solid android:color="@color/color_21bf73" /> <corners android:radius="100dp" /> <size android:width="25dp" android:height="25dp" /> </shape>iv_menu_normal.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="@color/color_f5f5f5" /> <corners android:radius="5dp" /> <size android:width="6dp" android:height="6dp" /> </shape>