This commit is contained in:
kunfei 2023-10-07 10:38:59 +08:00
parent d61d30c77e
commit 275e492c9f
7 changed files with 46 additions and 24 deletions

View File

@ -160,7 +160,7 @@ dependencies {
//androidX //androidX
implementation('androidx.core:core-ktx:1.12.0') implementation('androidx.core:core-ktx:1.12.0')
implementation('androidx.appcompat:appcompat:1.6.1') implementation('androidx.appcompat:appcompat:1.6.1')
implementation('androidx.activity:activity-ktx:1.7.2') implementation('androidx.activity:activity-ktx:1.8.0')
implementation('androidx.fragment:fragment-ktx:1.6.1') implementation('androidx.fragment:fragment-ktx:1.6.1')
implementation('androidx.preference:preference-ktx:1.2.1') implementation('androidx.preference:preference-ktx:1.2.1')
implementation('androidx.constraintlayout:constraintlayout:2.1.4') implementation('androidx.constraintlayout:constraintlayout:2.1.4')
@ -169,7 +169,7 @@ dependencies {
implementation('androidx.webkit:webkit:1.8.0') implementation('androidx.webkit:webkit:1.8.0')
//google //google
implementation('com.google.android.material:material:1.9.0') implementation('com.google.android.material:material:1.10.0')
implementation('com.google.android.flexbox:flexbox:3.0.0') implementation('com.google.android.flexbox:flexbox:3.0.0')
implementation('com.google.code.gson:gson:2.10.1') implementation('com.google.code.gson:gson:2.10.1')
@ -234,7 +234,7 @@ dependencies {
// //
//noinspection GradleDependency //noinspection GradleDependency
implementation('com.github.jenly1314:zxing-lite:2.4.0') implementation('com.github.jenly1314:zxing-lite:3.0.1')
// //
implementation('com.jaredrummler:colorpicker:1.1.0') implementation('com.jaredrummler:colorpicker:1.1.0')

View File

@ -6,7 +6,6 @@ import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import com.google.zxing.Result import com.google.zxing.Result
import com.king.zxing.CameraScan.OnScanResultCallback
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseActivity import io.legado.app.base.BaseActivity
import io.legado.app.databinding.ActivityQrcodeCaptureBinding import io.legado.app.databinding.ActivityQrcodeCaptureBinding
@ -16,7 +15,7 @@ import io.legado.app.utils.launch
import io.legado.app.utils.readBytes import io.legado.app.utils.readBytes
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
class QrCodeActivity : BaseActivity<ActivityQrcodeCaptureBinding>(), OnScanResultCallback { class QrCodeActivity : BaseActivity<ActivityQrcodeCaptureBinding>(), ScanResultCallback {
override val binding by viewBinding(ActivityQrcodeCaptureBinding::inflate) override val binding by viewBinding(ActivityQrcodeCaptureBinding::inflate)
@ -47,12 +46,11 @@ class QrCodeActivity : BaseActivity<ActivityQrcodeCaptureBinding>(), OnScanResul
return super.onCompatOptionsItemSelected(item) return super.onCompatOptionsItemSelected(item)
} }
override fun onScanResultCallback(result: Result?): Boolean { override fun onScanResultCallback(result: Result?) {
val intent = Intent() val intent = Intent()
intent.putExtra("result", result?.text) intent.putExtra("result", result?.text)
setResult(RESULT_OK, intent) setResult(RESULT_OK, intent)
finish() finish()
return true
} }
} }

View File

@ -1,16 +1,19 @@
package io.legado.app.ui.qrcode package io.legado.app.ui.qrcode
import com.google.zxing.Result import com.google.zxing.Result
import com.king.zxing.CaptureFragment import com.king.camera.scan.AnalyzeResult
import com.king.camera.scan.CameraScan
import com.king.camera.scan.analyze.Analyzer
import com.king.zxing.BarcodeCameraScanFragment
import com.king.zxing.DecodeConfig import com.king.zxing.DecodeConfig
import com.king.zxing.DecodeFormatManager import com.king.zxing.DecodeFormatManager
import com.king.zxing.analyze.MultiFormatAnalyzer import com.king.zxing.analyze.MultiFormatAnalyzer
import com.king.zxing.analyze.QRCodeAnalyzer
class QrCodeFragment : BarcodeCameraScanFragment() {
class QrCodeFragment : CaptureFragment() { override fun initCameraScan(cameraScan: CameraScan<Result>) {
super.initCameraScan(cameraScan)
override fun initCameraScan() {
super.initCameraScan()
//初始化解码配置 //初始化解码配置
val decodeConfig = DecodeConfig() val decodeConfig = DecodeConfig()
//如果只有识别二维码的需求这样设置效率会更高不设置默认为DecodeFormatManager.DEFAULT_HINTS //如果只有识别二维码的需求这样设置效率会更高不设置默认为DecodeFormatManager.DEFAULT_HINTS
@ -24,9 +27,22 @@ class QrCodeFragment : CaptureFragment() {
cameraScan.setAnalyzer(MultiFormatAnalyzer(decodeConfig)) cameraScan.setAnalyzer(MultiFormatAnalyzer(decodeConfig))
} }
override fun onScanResultCallback(result: Result?): Boolean { override fun createAnalyzer(): Analyzer<Result> {
(activity as? QrCodeActivity)?.onScanResultCallback(result) // 初始化解码配置
return true val decodeConfig = DecodeConfig()
decodeConfig.setHints(DecodeFormatManager.QR_CODE_HINTS) //如果只有识别二维码的需求这样设置效率会更高不设置默认为DecodeFormatManager.DEFAULT_HINTS
.setFullAreaScan(false) //设置是否全区域识别默认false
.setAreaRectRatio(0.8f) //设置识别区域比例默认0.8,设置的比例最终会在预览区域裁剪基于此比例的一个矩形进行扫码识别
.setAreaRectVerticalOffset(0).areaRectHorizontalOffset =
0 //设置识别区域水平方向偏移量默认为0为0表示居中可以为负数
// BarcodeCameraScanActivity默认使用的MultiFormatAnalyzer这里可以改为使用QRCodeAnalyzer
return QRCodeAnalyzer(decodeConfig)
}
override fun onScanResultCallback(result: AnalyzeResult<Result>) {
cameraScan.setAnalyzeImage(false)
(activity as? QrCodeActivity)?.onScanResultCallback(result.result)
} }
} }

View File

@ -0,0 +1,9 @@
package io.legado.app.ui.qrcode
import com.google.zxing.Result
interface ScanResultCallback {
fun onScanResultCallback(result: Result?)
}

View File

@ -24,7 +24,6 @@ import com.google.zxing.common.HybridBinarizer
import com.google.zxing.qrcode.QRCodeWriter import com.google.zxing.qrcode.QRCodeWriter
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel
import com.king.zxing.DecodeFormatManager import com.king.zxing.DecodeFormatManager
import com.king.zxing.util.LogUtils
import java.util.EnumMap import java.util.EnumMap
import kotlin.math.max import kotlin.math.max
@ -103,7 +102,7 @@ object QRCodeUtils {
} }
return bitmap return bitmap
} catch (e: WriterException) { } catch (e: WriterException) {
LogUtils.w(e.message) e.printOnDebug()
} }
return null return null
} }
@ -161,7 +160,7 @@ object QRCodeUtils {
canvas.restore() canvas.restore()
} catch (e: Exception) { } catch (e: Exception) {
bitmap = null bitmap = null
LogUtils.w(e.message) e.printOnDebug()
} }
return bitmap return bitmap
} }
@ -222,7 +221,7 @@ object QRCodeUtils {
} }
} }
} catch (e: java.lang.Exception) { } catch (e: java.lang.Exception) {
LogUtils.w(e.message) e.printOnDebug()
} finally { } finally {
reader.reset() reader.reset()
} }
@ -295,7 +294,7 @@ object QRCodeUtils {
result = decodeInternal(reader, source.rotateCounterClockwise()) result = decodeInternal(reader, source.rotateCounterClockwise())
} }
} catch (e: Exception) { } catch (e: Exception) {
LogUtils.w(e.message) e.printOnDebug()
} finally { } finally {
reader.reset() reader.reset()
} }
@ -416,7 +415,7 @@ object QRCodeUtils {
addCode(bitmap, content, textSize, codeColor, textSize / 2) addCode(bitmap, content, textSize, codeColor, textSize / 2)
} else bitmap } else bitmap
} catch (e: WriterException) { } catch (e: WriterException) {
LogUtils.w(e.message) e.printOnDebug()
} }
return null return null
} }
@ -471,7 +470,7 @@ object QRCodeUtils {
canvas.restore() canvas.restore()
} catch (e: Exception) { } catch (e: Exception) {
bitmap = null bitmap = null
LogUtils.w(e.message) e.printOnDebug()
} }
return bitmap return bitmap
} }

View File

@ -6,7 +6,7 @@ buildscript {
build_tool_version = '34.0.0' build_tool_version = '34.0.0'
kotlin_version = '1.9.10' kotlin_version = '1.9.10'
ksp_version = "1.0.13" ksp_version = "1.0.13"
agp_version = '8.1.1' agp_version = '8.1.2'
media3_version = "1.1.1" media3_version = "1.1.1"
splitties_version = '3.0.0' splitties_version = '3.0.0'
room_version = '2.5.2' room_version = '2.5.2'

View File

@ -30,5 +30,5 @@ android {
dependencies { dependencies {
compileOnly "com.android.tools.build:gradle:$agp_version" compileOnly "com.android.tools.build:gradle:$agp_version"
implementation "androidx.annotation:annotation:1.6.0" implementation 'androidx.annotation:annotation:1.7.0'
} }