我正在尝试在 Kotlin 中制作一个应用程序,该应用程序扫描 QR 码,在页面中心生成带有扫描结果的 PDF,并在其下方添加相同文本的
125x125 px
QR 码图像(本质上是扫描的二维码)然后将文件存储在我的下载文件夹中。我正在使用 PdfDocument
类来生成 PDF,使用 com.github.yuriy-budiyev:code-scanner:2.3.2
依赖项来扫描二维码,并使用 Zxing 库来为 PDF 生成二维码图像。
我面临的问题是当我在我的
build.gradle
文件中为 zxing 库添加以下依赖项时,
implementation 'com.google.zxing:core:3.4.1'
implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
我在构建我的应用程序时收到一个错误,基本上是说 - (完整错误:https://pastebin.com/m6Vj0bAJ)
Duplicate class com.google.zxing.Result found in modules zxing-core-3.4.1.jar (com.google.zxing:core:3.4.1) and zxing-android-embedded-3.6.0.jar (com.journeyapps:zxing-android-embedded:3.6.0)
这是我的
build.gradle
文件中的依赖项的样子 -
dependencies {
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
implementation 'com.github.yuriy-budiyev:code-scanner:2.3.2' //for scanning qr
implementation 'com.google.zxing:core:3.4.1'
implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
}
以及我使用 zxing lib 生成二维码的导入和代码。在我的科特林代码中 -
import com.google.zxing.integration.android.IntentIntegrator
import com.google.zxing.BarcodeFormat
import com.google.zxing.qrcode.QRCodeWriter
import com.journeyapps.barcodescanner.BarcodeEncoder
//code in the function which creates the PDF using PdfDocument lib.
val qrCodeWriter = QRCodeWriter()
val bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, 125, 125)
val barcodeEncoder = BarcodeEncoder()
val qrCodeBitmap = barcodeEncoder.createBitmap(bitMatrix)
val qrCodeX = (pageInfo.pageWidth - qrCodeBitmap.width) / 2f
val qrCodeY = y + textBounds.height() + 20f
canvas.drawBitmap(qrCodeBitmap, qrCodeX, qrCodeY, null)
如果我不在 pdf 部分做整个二维码图像,一切正常;当我为 zxing lib 添加我提到的两个依赖项时,我的构建失败并出现我上面提到的错误。这里出了什么问题?
根据this page,
zxing-android-embedded
3.6.0 依赖于zxing:core
3.3.2。
您已将 zxing:core
3.4.1 添加为依赖项,因此您将获得 zxing:core
. 的副本
有几种不同的可能解决方案:
zxing:core
相同的zxing-android-embedded
版本(即3.3.2)。zxing-android-embedded
的传递依赖,所以只有你的zxing:core
版本(3.4.1)被使用:implementation ('com.journeyapps:zxing-android-embedded:3.6.0') { transitive false }
zxing-android-embedded
是否兼容其依赖项的任何其他版本。