Android - 如何更改条码扫描器 View 大小?

标签 android kotlin

我正在尝试通过关注此 YouTube video 来制作条码扫描器.通过观看此视频,我可以让应用程序正常运行。但由于条形码阅读器占据了整个页面,而扫描光标仅占据了大约 40% 的屏幕区域。所以我想知道是否有办法缩小条形码,剩余空间可用于放置一些按钮或警告 TexViews。 所以基本上我想设置 QR 相机 View 的宽度和高度。这可能吗?

enter image description here

很明显,绿色方 block 之外的空间是浪费,这就是为什么我想将其排列成大约 200dp*200dp 的尺寸。

代码如下:

package com.example.priyanka.qrbarcodescanner

import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.content.pm.PackageManager
import android.hardware.Camera
import android.net.Uri
import android.os.Build
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import android.support.v7.app.AlertDialog
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast

import com.google.zxing.Result

import me.dm7.barcodescanner.zxing.ZXingScannerView

import android.Manifest.permission.CAMERA

class MainActivity : AppCompatActivity(), ZXingScannerView.ResultHandler {
    private var scannerView: ZXingScannerView? = null
    internal var mcontext: Context? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mcontext = this

        scannerView = ZXingScannerView(this)
        setContentView(scannerView)
        val currentApiVersion = Build.VERSION.SDK_INT

        if (currentApiVersion >= Build.VERSION_CODES.M) {
            if (checkPermission()) {
                Toast.makeText(applicationContext, "Permission already granted!", Toast.LENGTH_LONG).show()
            } else {
                requestPermission()
            }
        }
    }

    private fun checkPermission(): Boolean {
        return ContextCompat.checkSelfPermission(applicationContext, CAMERA) == PackageManager.PERMISSION_GRANTED
    }

    private fun requestPermission() {
        ActivityCompat.requestPermissions(this, arrayOf(CAMERA), REQUEST_CAMERA)
    }

    public override fun onResume() {
        super.onResume()

        val currentapiVersion = android.os.Build.VERSION.SDK_INT
        if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
            if (checkPermission()) {
                if (scannerView == null) {
                    scannerView = ZXingScannerView(this)
                    setContentView(scannerView)
                }
                scannerView!!.setResultHandler(this)
                scannerView!!.startCamera()
            } else {
                requestPermission()
            }
        }
    }

    public override fun onDestroy() {
        super.onDestroy()
        scannerView!!.stopCamera()
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        when (requestCode) {
            REQUEST_CAMERA -> if (grantResults.size > 0) {

                val cameraAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED
                if (cameraAccepted) {
                    Toast.makeText(applicationContext, "Permission Granted, Now you can access camera", Toast.LENGTH_LONG).show()
                } else {
                    Toast.makeText(applicationContext, "Permission Denied, You cannot access and camera", Toast.LENGTH_LONG).show()
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        if (shouldShowRequestPermissionRationale(CAMERA)) {
                            showMessageOKCancel("You need to allow access to both the permissions",
                                    DialogInterface.OnClickListener { dialog, which ->
                                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                            requestPermissions(arrayOf(CAMERA),
                                                    REQUEST_CAMERA)
                                        }
                                    })
                            return
                        }
                    }
                }
            }
        }
    }

    private fun showMessageOKCancel(message: String, okListener: DialogInterface.OnClickListener) {
        android.support.v7.app.AlertDialog.Builder(mcontext!!)
                .setMessage(message)
                .setPositiveButton("OK", okListener)
                .setNegativeButton("Cancel", null)
                .create()
                .show()
    }

    override fun handleResult(result: Result) {
        val myResult = result.text
        Log.d("QRCodeScanner", result.text)
        Log.d("QRCodeScanner", result.barcodeFormat.toString())

        val builder = AlertDialog.Builder(this)
        builder.setTitle("Scan Result")
        builder.setPositiveButton("OK") { dialog, which -> scannerView!!.resumeCameraPreview(this@MainActivity) }
        builder.setNeutralButton("Visit") { dialog, which ->
            val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(myResult))
            startActivity(browserIntent)
        }
        builder.setMessage(result.text)
        val alert1 = builder.create()
        alert1.show()
    }

    companion object {

        private val REQUEST_CAMERA = 1
        private val camId = Camera.CameraInfo.CAMERA_FACING_BACK
    }
}

提前谢谢你。

最佳答案

您可以使用具有这些 gradle 依赖项的 zxing 嵌入式库:

implementation "com.journeyapps:zxing-android-embedded:3.5.0@aar"
implementation "com.google.zxing:core:3.3.0"

然后把它放在你的布局中

<com.journeyapps.barcodescanner.DecoratedBarcodeView
    android:id="@+id/qr_scanner_view"
    android:layout_width="@dimen/your_width"
    android:layout_height="@dimen/your_height" />

然后在你的代码中使用它

DecoratedBarcodeView qrView = findViewById(R.id.qr_scanner_view);
CameraSettings s = new CameraSettings();
s.setRequestedCameraId(0); // front/back/etc
qrView.getBarcodeView().setCameraSettings(s);
qrView.resume();

qrView.decodeSingle(new BarcodeCallback() {
    @Override
    public void barcodeResult(BarcodeResult result) {
        Log.d("barcode result: " + result.toString());
        // do your thing with result
    }

    @Override
    public void possibleResultPoints(List<ResultPoint> resultPoints) {}
});

关于Android - 如何更改条码扫描器 View 大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48307670/

相关文章:

java - 错误 :Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference

java - 如何使 Filereader 的 readLine() 转到上一个位置

generics - 我可以从化T创建KType吗

kotlin - 如何在 Kotlin 中制作编译时间常数 NaN 或 POSITIVE_INFINITY?

android - 当我将代码从 Java 转换为 Kotlin 时出现错误 "Expecting member declaration",为什么?

kotlin - 如果可空类型为空,我如何运行代码块?

android - 旧 java 项目中的新 kotlin 文件 - 'Unresolved reference' R

android - 如何通过Android应用中的链接重定向到youtube应用?

android - 如何为 Android 开发者切换动画?

android - 下载的位图图像显示太小