From 06348b272692fe2609dd4b38e17bf67ba528ca1c Mon Sep 17 00:00:00 2001 From: aiWean <1581209979@qq.com> Date: Wed, 22 Jul 2020 18:25:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E5=8D=87=E7=A8=B3=E5=AE=9A=E6=80=A7?= =?UTF-8?q?=EF=BC=8C=E5=AE=8C=E5=96=84=E5=9B=BE=E7=89=87demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 1 + .../java/com/android/NBZxing/CusZxingView.kt | 75 +++++++++++++++-- .../com/android/NBZxing/MainActivity.java | 25 +++++- app/src/main/res/drawable/ic_fanhui.xml | 5 ++ app/src/main/res/layout/floorview_layout.xml | 8 +- app/src/main/res/layout/tool_title.xml | 84 +++++++++++++++++++ app/src/main/res/xml/file_paths.xml | 9 ++ .../com/ailiwean/core/view/ZxingCameraView.kt | 35 ++++---- .../android/cameraview/BaseCameraView.kt | 2 +- 9 files changed, 216 insertions(+), 28 deletions(-) create mode 100644 app/src/main/res/drawable/ic_fanhui.xml create mode 100644 app/src/main/res/layout/tool_title.xml create mode 100644 app/src/main/res/xml/file_paths.xml diff --git a/app/build.gradle b/app/build.gradle index 0822a57..14f0832 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -37,6 +37,7 @@ android { dependencies { implementation project(path: ':module_camera') api "androidx.appcompat:appcompat:1.1.0" + implementation 'com.zhihu.android:matisse:0.5.3-beta3' // implementation 'com.github.ailiwean:NBZxing:0.0.7' // api 'com.google.zxing:core:3.3.3' // implementation 'com.github.ailiwean:NBZxing:0.0.12' diff --git a/app/src/main/java/com/android/NBZxing/CusZxingView.kt b/app/src/main/java/com/android/NBZxing/CusZxingView.kt index 07e7252..2553e5f 100644 --- a/app/src/main/java/com/android/NBZxing/CusZxingView.kt +++ b/app/src/main/java/com/android/NBZxing/CusZxingView.kt @@ -1,13 +1,23 @@ package com.android.NBZxing +import android.Manifest +import android.app.Activity import android.content.Context -import android.os.Environment +import android.content.pm.ActivityInfo +import android.content.pm.PackageManager +import android.graphics.Color +import android.os.Build import android.util.AttributeSet import android.view.LayoutInflater import android.view.View +import android.widget.TextView import android.widget.Toast import com.ailiwean.core.view.ZxingCameraView import com.ailiwean.core.zxing.ScanTypeConfig +import com.zhihu.matisse.Matisse +import com.zhihu.matisse.MimeType +import com.zhihu.matisse.engine.impl.GlideEngine + /** * @Package: com.android.NBZXing @@ -27,12 +37,42 @@ class CusZxingView @JvmOverloads constructor(context: Context, attributeSet: Att */ override fun provideFloorView(): View? { val v = LayoutInflater.from(context) - .inflate(R.layout.floorview_layout, this, false) + .inflate(R.layout.tool_title, this, false) + + v.findViewById(R.id.vToolBar) + .setBackgroundColor(Color.parseColor("#2f000000")) + + v.findViewById(R.id.vTitle).text = "扫一扫" - v.findViewById(R.id.img) - .setOnClickListener { - parseFile(Environment.getExternalStorageDirectory().absolutePath + "/scan.jpg") + v.findViewById(R.id.vLeftImage) + .setOnClickListener { v: View? -> + if (context is Activity) { + (context as Activity).finish() + } } + + v.findViewById(R.id.vRightTextView).text = "相册" + v.findViewById(R.id.vRightTextView) + .setOnClickListener { v: View? -> + if (!checkPermissionRW()) { + requstPermissionRW() + return@setOnClickListener + } + if (context is Activity) { + Matisse.from(context as Activity) + .choose(MimeType.ofAll()) + .countable(true) + .maxSelectable(9) + .gridExpectedSize(300) + .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) + .thumbnailScale(0.85f) + .imageEngine(GlideEngine()) + .showPreview(false) // Default is `true` + .forResult(1) + } + + } + return v } @@ -48,4 +88,29 @@ class CusZxingView @JvmOverloads constructor(context: Context, attributeSet: Att return ScanTypeConfig.HIGH_FREQUENCY } + fun toParse(string: String) { + parseFile(string) + } + + + fun checkPermissionRW(): Boolean { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + context.checkSelfPermission( + Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED + + context.checkSelfPermission( + Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED + + } else { + return true + } + } + + + fun requstPermissionRW() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + (context as? Activity)?.requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE), 200) + } + } + } \ No newline at end of file diff --git a/app/src/main/java/com/android/NBZxing/MainActivity.java b/app/src/main/java/com/android/NBZxing/MainActivity.java index cfb631f..1154370 100644 --- a/app/src/main/java/com/android/NBZxing/MainActivity.java +++ b/app/src/main/java/com/android/NBZxing/MainActivity.java @@ -1,10 +1,19 @@ package com.android.NBZxing; +import android.content.Intent; import android.os.Bundle; +import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; +import com.bumptech.glide.Glide; +import com.bumptech.glide.request.RequestOptions; +import com.bumptech.glide.request.target.Target; +import com.zhihu.matisse.Matisse; + +import java.util.List; + public class MainActivity extends AppCompatActivity { @Override @@ -20,4 +29,18 @@ protected void onCreate(Bundle savedInstanceState) { // .replace(R.id.parent, fragment) // .commit(); } -} + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if (requestCode == 1) { + if (resultCode == RESULT_OK) { + String path = Matisse.obtainPathResult(data).get(0); + this.findViewById(R.id.zxingview).toParse(path); + } else + Toast.makeText(this, "uri为null", Toast.LENGTH_SHORT).show(); + } + + } + +} \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_fanhui.xml b/app/src/main/res/drawable/ic_fanhui.xml new file mode 100644 index 0000000..52c97cf --- /dev/null +++ b/app/src/main/res/drawable/ic_fanhui.xml @@ -0,0 +1,5 @@ + + + + diff --git a/app/src/main/res/layout/floorview_layout.xml b/app/src/main/res/layout/floorview_layout.xml index 42a07d3..1247ecb 100644 --- a/app/src/main/res/layout/floorview_layout.xml +++ b/app/src/main/res/layout/floorview_layout.xml @@ -4,12 +4,6 @@ android:layout_height="match_parent" android:orientation="vertical"> - + \ No newline at end of file diff --git a/app/src/main/res/layout/tool_title.xml b/app/src/main/res/layout/tool_title.xml new file mode 100644 index 0000000..4c53790 --- /dev/null +++ b/app/src/main/res/layout/tool_title.xml @@ -0,0 +1,84 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/xml/file_paths.xml b/app/src/main/res/xml/file_paths.xml new file mode 100644 index 0000000..d845585 --- /dev/null +++ b/app/src/main/res/xml/file_paths.xml @@ -0,0 +1,9 @@ + + + + + + + \ No newline at end of file diff --git a/module_camera/src/main/java/com/ailiwean/core/view/ZxingCameraView.kt b/module_camera/src/main/java/com/ailiwean/core/view/ZxingCameraView.kt index 26766e3..4f0e383 100644 --- a/module_camera/src/main/java/com/ailiwean/core/view/ZxingCameraView.kt +++ b/module_camera/src/main/java/com/ailiwean/core/view/ZxingCameraView.kt @@ -10,7 +10,6 @@ import android.graphics.PointF import android.os.Handler import android.os.Message import android.util.AttributeSet -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -23,7 +22,6 @@ import com.ailiwean.core.zxing.BitmapLuminanceSource import com.ailiwean.core.zxing.CustomMultiFormatReader import com.ailiwean.core.zxing.ScanTypeConfig import com.ailiwean.core.zxing.core.BinaryBitmap -import com.ailiwean.core.zxing.core.RGBLuminanceSource import com.ailiwean.core.zxing.core.common.HybridBinarizer import com.google.android.cameraview.AspectRatio import com.google.android.cameraview.BaseCameraView @@ -86,7 +84,7 @@ abstract class ZxingCameraView @JvmOverloads constructor(context: Context, attri * 扫码成功后的一些动作 */ fun scanSucHelper() { - stop() + onComPause() scan_bar.stopAnim() VibrateHelper.playVibrate() VibrateHelper.playBeep() @@ -191,20 +189,29 @@ abstract class ZxingCameraView @JvmOverloads constructor(context: Context, attri if (!file.exists()) { return } - val bitmap = BitmapFactory.decodeFile(filePath) - parseBitmap(bitmap) + cameraHandler.post { + val bitmap = BitmapFactory.decodeFile(filePath) + parseBitmap(bitmap) + } } protected fun parseBitmap(bitmap: Bitmap) { - val source = BitmapLuminanceSource(bitmap) - val result = CustomMultiFormatReader.getInstance() - .decode(BinaryBitmap(HybridBinarizer(source))) - if (result != null) { - resultBackFile(result.text) - scanSucHelper() - } else { - resultBackFile("") - onComResume() + onComPause() + cameraHandler.post { + val source = BitmapLuminanceSource(bitmap) + val result = CustomMultiFormatReader.getInstance() + .decode(BinaryBitmap(HybridBinarizer(source))) + if (result != null) { + hand.post { + resultBackFile(result.text) + scanSucHelper() + } + } else { + hand.post { + resultBackFile("") + onComResume() + } + } } } } \ No newline at end of file diff --git a/module_camera/src/main/java/com/google/android/cameraview/BaseCameraView.kt b/module_camera/src/main/java/com/google/android/cameraview/BaseCameraView.kt index 28ce85b..618be24 100644 --- a/module_camera/src/main/java/com/google/android/cameraview/BaseCameraView.kt +++ b/module_camera/src/main/java/com/google/android/cameraview/BaseCameraView.kt @@ -182,7 +182,7 @@ abstract class BaseCameraView @JvmOverloads constructor(context: Context, attrib isShoudCreateOpen = false } - private val cameraHandler by lazy { + protected val cameraHandler by lazy { val handlerThread: HandlerThread = HandlerThread(System.currentTimeMillis().toString()) handlerThread.start() Handler(handlerThread.looper)