LoginSignup
3
2

More than 1 year has passed since last update.

ZXing-android-embedded QRコードリーダーの、RegisterActivityResultとKotlin対応

Last updated at Posted at 2021-12-03

スマホで簡易にQRコードリーダーを動かしたいとき、ZXingというのがあります。それを含むパッケージに、https://github.com/journeyapps/zxing-android-embedded というのがあります。この秋に、それがRegisterActivityResult対応し、Integratorとか機能をDeprecateしました。

それを取り込むのにひと手間あったので、ここへ記録。

1.zxing-android-embedded は、Javaコードしか提供していません。Kotlinほしい。Android StudioでそのSampleをコンバートすりゃいいのですが。ネットで調べた限り、まだ誰も載せていないので、以下にKotlinでのスケルトン載せておきます。これだけ。非常にSimple。

2.以前動いていたコードが、もう動かない、原因を調べたところ、ManifestでActivity属性で、android:noHistory="true" という変更をしたのが邪魔していた。なんでか、理解していない。誰か教えてくれ。

import com.journeyapps.barcodescanner.ScanContract
import com.journeyapps.barcodescanner.ScanOptions
...
val barcodeLauncher = registerForActivityResult(ScanContract()) { result ->
    if (result.contents != null) {
          読み取ったQRドテキストの処理
     } else {
          失敗処理
   }
val options = ScanOptions()
options.setOrientationLocked(false)
barcodeLauncher.launch(options)

あとはbuild.gradleに、以下。


implementation 'com.journeyapps:zxing-android-embedded:4.3.0'

また、Manifestに、以下を追加。

<activity
android:name="com.journeyapps.barcodescanner.CaptureActivity"
android:screenOrientation="fullSensor"
tools:replace="screenOrientation" />

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2