Closed Reginer closed 6 years ago
xml:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".menu.open.qr.QrActivity"> <cn.bingoogolapple.qrcode.zbar.ZBarView android:id="@+id/qrView" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:qrcv_animTime="1000" app:qrcv_barcodeRectHeight="120dp" app:qrcv_borderColor="@android:color/white" app:qrcv_borderSize="1dp" app:qrcv_cornerColor="@color/colorPrimaryDark" app:qrcv_cornerLength="20dp" app:qrcv_cornerSize="3dp" app:qrcv_customGridScanLineDrawable="@drawable/custom_scan_line" app:qrcv_isBarcode="false" app:qrcv_isCenterVertical="false" app:qrcv_isOnlyDecodeScanBoxArea="false" app:qrcv_isShowDefaultGridScanLineDrawable="true" app:qrcv_isShowDefaultScanLineDrawable="true" app:qrcv_isShowTipBackground="true" app:qrcv_isShowTipTextAsSingleLine="false" app:qrcv_isTipTextBelowRect="false" app:qrcv_maskColor="#33FFFFFF" app:qrcv_rectWidth="200dp" app:qrcv_scanLineColor="@color/colorPrimaryDark" app:qrcv_toolbarHeight="56dp" app:qrcv_topOffset="20dp"> </cn.bingoogolapple.qrcode.zbar.ZBarView> <Button android:id="@+id/openFlashlight" android:layout_width="0dp" android:layout_height="30dp" android:layout_marginBottom="8dp" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:background="@drawable/bg_touch_round" android:text="@string/open_flashlight" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/closeFlashlight" app:layout_constraintStart_toStartOf="parent"/> <Button android:id="@+id/closeFlashlight" android:layout_width="0dp" android:layout_height="30dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:layout_marginStart="8dp" android:background="@drawable/bg_touch_round" android:text="@string/close_flashlight" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/openFlashlight" /> <Button android:id="@+id/scanBarcode" android:layout_width="0dp" android:layout_height="30dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:background="@drawable/bg_touch_round" android:text="@string/scan_barcode" app:layout_constraintBottom_toTopOf="@+id/openFlashlight" app:layout_constraintEnd_toEndOf="@+id/openFlashlight" app:layout_constraintStart_toStartOf="@+id/openFlashlight"/> <Button android:id="@+id/scanQrCode" android:layout_width="0dp" android:layout_height="30dp" android:layout_marginBottom="8dp" android:background="@drawable/bg_touch_round" android:text="@string/scan_qr_code" app:layout_constraintBottom_toTopOf="@+id/closeFlashlight" app:layout_constraintEnd_toEndOf="@+id/closeFlashlight" app:layout_constraintStart_toStartOf="@+id/closeFlashlight"/> <Button android:id="@+id/startScan" android:layout_width="0dp" android:layout_height="30dp" android:layout_marginBottom="8dp" android:background="@drawable/bg_touch_round" android:text="@string/start_scan" app:layout_constraintBottom_toTopOf="@+id/scanBarcode" app:layout_constraintEnd_toEndOf="@+id/scanBarcode" app:layout_constraintStart_toStartOf="@+id/scanBarcode"/> <Button android:id="@+id/stopScan" android:layout_width="0dp" android:layout_height="30dp" android:layout_marginBottom="8dp" android:background="@drawable/bg_touch_round" android:text="@string/stopScan" app:layout_constraintBottom_toTopOf="@+id/scanQrCode" app:layout_constraintEnd_toEndOf="@+id/scanQrCode" app:layout_constraintStart_toStartOf="@+id/scanQrCode"/> <Button android:id="@+id/showRect" android:layout_width="0dp" android:layout_height="30dp" android:layout_marginBottom="8dp" android:background="@drawable/bg_touch_round" android:text="@string/showRect" app:layout_constraintBottom_toTopOf="@+id/startScan" app:layout_constraintEnd_toEndOf="@+id/startScan" app:layout_constraintStart_toStartOf="@+id/startScan"/> <Button android:id="@+id/hideRect" android:layout_width="0dp" android:layout_height="30dp" android:layout_marginBottom="8dp" android:background="@drawable/bg_touch_round" android:text="@string/hideRect" app:layout_constraintBottom_toTopOf="@+id/stopScan" app:layout_constraintEnd_toEndOf="@+id/stopScan" app:layout_constraintStart_toStartOf="@+id/stopScan"/> </android.support.constraint.ConstraintLayout>
activity:
class QrActivity : BaseActivity(), QRCodeView.Delegate { override val actLayoutId: Int get() = R.layout.activity_qr override fun initView(savedInstanceState: Bundle?) { qrView.setDelegate(this) } override fun initToolBar() { } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) openFlashlight.setOnClickListener { qrView.openFlashlight() } closeFlashlight.setOnClickListener { qrView.closeFlashlight() } scanQrCode.setOnClickListener { qrView.changeToScanBarcodeStyle() } scanBarcode.setOnClickListener { qrView.changeToScanQRCodeStyle() } startScan.setOnClickListener { qrView.startSpot() } stopScan.setOnClickListener { qrView.stopSpot() } showRect.setOnClickListener { qrView.showScanRect() } hideRect.setOnClickListener { qrView.hiddenScanRect() } } override fun onStart() { super.onStart() qrView.startCamera() qrView.showScanRect() } override fun onStop() { qrView.stopCamera() super.onStop() } override fun onDestroy() { qrView.onDestroy() super.onDestroy() } override fun onScanQRCodeSuccess(result: String) { Logcat.d("result:$result") ToastUtils.showShortToastSafe(result) qrView.startSpot() } override fun onScanQRCodeOpenCameraError() { Logcat.e("打开相机错误") } }
把ZBarView的宽高改成match_parent可以了
xml:
activity: