LoginSignup
6
9

More than 3 years have passed since last update.

[Kotlin] Zxing QRコードリーダーをカスタマイズ

Last updated at Posted at 2019-12-27

他のサイトを参考しながらZxingQRコードリーダー実装しましたが、赤いラインが表示されているのは微妙なので、QRコードリーダーの見た目をカスタマイズしていきましょう!

問題:赤いライン嫌です
画像お借りします(リンクは下記に記載)
スクリーンショット 2019-12-27 15.42.43.png

成果物:高級ホワイトになった
スクリーンショット 2019-12-27 15.49.26.png

ライブラリ

Zxingライブラリ
https://github.com/journeyapps/zxing-android-embedded

コード

QRCodeCaptureActivity.kt
// 省略
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.fragment_qrcode)

        barcodeScannerView = findViewById(R.id.qrcode_reader)

        // これを追加して赤い線消そう
        disableLaser(barcodeScannerView!!)

        capture = CaptureManager(this, barcodeScannerView!!)
        capture!!.initializeFromIntent(intent, savedInstanceState)
        capture!!.decode()
    }

    // これを追加して赤い線消そう
    private fun disableLaser(decoratedBarcodeView: DecoratedBarcodeView) {
        val scannerAlphaField = ViewfinderView::class.java.getDeclaredField("SCANNER_ALPHA")
        scannerAlphaField.isAccessible = true
        scannerAlphaField.set(decoratedBarcodeView.viewFinder, intArrayOf(0))
    }

// 省略
fragment_qrcode.xml
// 省略
<com.journeyapps.barcodescanner.CompoundBarcodeView
        android:id="@+id/qrcode_reader"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        // カスタマイズQRコードリーダー作ろう
        zxing_view:zxing_scanner_layout="@layout/qr_code_reader" />
qr_code_reader.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:zxing_barcode_view="http://schemas.android.com/apk/res-auto"
    xmlns:zxing_finder="http://schemas.android.com/apk/res-auto">

    <com.journeyapps.barcodescanner.BarcodeView
        android:id="@+id/zxing_barcode_surface"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        zxing_barcode_view:zxing_framing_rect_height="300dp"
        zxing_barcode_view:zxing_framing_rect_width="300dp" />

    <View
        android:layout_width="305dp"
        android:layout_height="305dp"
        // カスタマイズフレーム作ろう
        android:background="@drawable/qr_code_frame"
        android:layout_gravity="center"></View>

    <com.journeyapps.barcodescanner.ViewfinderView
        android:id="@+id/zxing_viewfinder_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        zxing_finder:zxing_possible_result_points="@color/zxing_transparent"
        zxing_finder:zxing_result_view="@color/zxing_result_view"
        zxing_finder:zxing_viewfinder_laser="@color/zxing_transparent"
        zxing_finder:zxing_viewfinder_mask="@color/zxing_viewfinder_mask" />

</merge>
qr_code_frame.xml
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="220dp"
    android:height="220dp"
    android:viewportHeight="230.0"
    android:viewportWidth="230.0">
    <path
        android:fillColor="#00000000"
        android:fillType="evenOdd"
        android:pathData="M165,5C178.2,5 198.2,5 225,5L225,30.79L225,65"
        android:strokeColor="@android:color/white"
        android:strokeWidth="5" />
    <path
        android:fillColor="#00000000"
        android:fillType="evenOdd"
        android:pathData="M5,65C5,51.8 5,31.8 5,5L30.79,5L65,5"
        android:strokeColor="@android:color/white"
        android:strokeWidth="5" />
    <path
        android:fillColor="#00000000"
        android:fillType="evenOdd"
        android:pathData="M165,225C178.2,225 198.2,225 225,225L225,199.21L225,165"
        android:strokeColor="@android:color/white"
        android:strokeWidth="5" />
    <path
        android:fillColor="#00000000"
        android:fillType="evenOdd"
        android:pathData="M5,165C5,178.2 5,198.2 5,225L30.79,225L65,225"
        android:strokeColor="@android:color/white"
        android:strokeWidth="5" />
</vector>

完成

スクリーンショット 2019-12-27 15.49.26.png

参考
https://blog.shg25.com/?p=184

6
9
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
6
9