From ad0773683141fe7fa0ca7dee0d5c03bb4e61c1ef Mon Sep 17 00:00:00 2001 From: aiWean <1581209979@qq.com> Date: Tue, 4 Aug 2020 12:08:45 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8Dandroid=2010=E6=B2=99?= =?UTF-8?q?=E7=AE=B1=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- module_camera/src/main/AndroidManifest.xml | 3 ++ .../com/ailiwean/core/view/ZxingCameraView.kt | 39 ++++++++++++++----- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/module_camera/src/main/AndroidManifest.xml b/module_camera/src/main/AndroidManifest.xml index 266ce46..a3d5852 100644 --- a/module_camera/src/main/AndroidManifest.xml +++ b/module_camera/src/main/AndroidManifest.xml @@ -14,6 +14,7 @@ xmlns:tools="http://schemas.android.com/tools" package="com.google.android.cameraview"> + @@ -28,4 +29,6 @@ + + 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 c525899..fb11744 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 @@ -3,19 +3,20 @@ package com.ailiwean.core.view import android.animation.Animator import android.animation.AnimatorListenerAdapter import android.annotation.SuppressLint +import android.content.ContentUris import android.content.Context +import android.database.Cursor import android.graphics.Bitmap import android.graphics.BitmapFactory import android.graphics.ImageDecoder import android.graphics.PointF -import android.graphics.drawable.BitmapDrawable +import android.net.Uri import android.os.Build import android.os.Handler import android.os.HandlerThread import android.os.Message import android.provider.MediaStore import android.util.AttributeSet -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -193,6 +194,12 @@ abstract class ZxingCameraView @JvmOverloads constructor(context: Context, attri var busHandle: Handler? = null + private fun initBusHandle() { + val handlerThread = HandlerThread(System.currentTimeMillis().toString()) + handlerThread.start() + busHandle = Handler(handlerThread.looper) + } + protected fun parseFile(filePath: String) { if (!checkPermissionRW()) @@ -207,7 +214,7 @@ abstract class ZxingCameraView @JvmOverloads constructor(context: Context, attri busHandle?.post { val bitmap: Bitmap = (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { - ImageDecoder.decodeBitmap(ImageDecoder.createSource(file)) + ImageDecoder.decodeBitmap(ImageDecoder.createSource(context.contentResolver, getMediaUriFromPath(context, filePath))) .copy(Bitmap.Config.RGB_565, false) } else BitmapFactory.decodeFile(filePath)) @@ -216,13 +223,9 @@ abstract class ZxingCameraView @JvmOverloads constructor(context: Context, attri } } - private fun initBusHandle() { - val handlerThread = HandlerThread(System.currentTimeMillis().toString()) - handlerThread.start() - busHandle = Handler(handlerThread.looper) - } - - protected fun parseBitmap(bitmap: Bitmap) { + protected fun parseBitmap(bitmap: Bitmap?) { + if (bitmap == null) + return if (busHandle == null) initBusHandle() busHandle?.post { @@ -257,4 +260,20 @@ abstract class ZxingCameraView @JvmOverloads constructor(context: Context, attri } } } + + open fun getMediaUriFromPath(context: Context, path: String): Uri { + val mediaUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI + val cursor: Cursor? = context.contentResolver.query(mediaUri, + null, + MediaStore.Images.Media.DISPLAY_NAME + "= ?", arrayOf(path.substring(path.lastIndexOf("/") + 1)), + null) + var uri: Uri? = null + cursor?.let { + it.moveToFirst() + uri = ContentUris.withAppendedId(mediaUri, + it.getLong(it.getColumnIndex(MediaStore.Images.Media._ID))) + } + cursor?.close() + return uri ?: Uri.EMPTY + } } \ No newline at end of file