首页 > 解决方案 > 相机方向 横向 Android Zxing

问题描述

我正在开发使用 Zxing 库扫描 QR 的 Android 应用程序。我使用 Fragment 实现了 Zxing QR 扫描仪。我面临的主要问题是相机始终以横向打开,但在我关注的原始项目中始终以纵向模式打开。这是我正在关注的教程教程

我在清单和片段中添加了方向。

这是我的主要片段

class QRCodeFragment : Fragment() {

internal var txtName: TextView? = null
internal var txtSiteName: TextView? = null
internal var btnScan: Button? = null
internal var qrScanIntegrator: IntentIntegrator? = null


override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    return inflater?.inflate(R.layout.fragment_qr_code, container, false)
}


override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
   // activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    txtName = view.findViewById(R.id.name)
    txtSiteName = view.findViewById(R.id.site_name)

    btnScan = view.findViewById(R.id.btnScan)
    btnScan!!.setOnClickListener {
        performAction()
    }

    qrScanIntegrator = IntentIntegrator.forFragment(this)
    qrScanIntegrator?.setOrientationLocked(false)


    // Different Customization option...
    qrScanIntegrator?.setPrompt(getString(R.string.scan_a_code))
    qrScanIntegrator?.setCameraId(0)  // Use a specific camera of the device
    qrScanIntegrator?.setBeepEnabled(true)
    qrScanIntegrator?.setBarcodeImageEnabled(true)
    super.onViewCreated(view, savedInstanceState)
}

fun performAction() {
    qrScanIntegrator?.initiateScan()
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    val c = Calendar.getInstance()

    val df = SimpleDateFormat("dd-MMM-yyyy")
    val date = df.format(c.time)


    val result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data)
    if (result != null) {
        // If QRCode has no data.
        if (result.contents == null) {
            Toast.makeText(activity, R.string.result_not_found, Toast.LENGTH_LONG).show()
        } else {
            // If QRCode contains data.
            try {
                // Converting the data to json format
                val obj = JSONObject(result.contents)
                // Show values in UI.
                txtName?.text = obj.getString("name")
                txtSiteName?.text = obj.getString("site_name")

            } catch (e: JSONException) {
                e.printStackTrace()

                // Data not in the expected format. So, whole object as toast message.
                Toast.makeText(activity, result.contents +" "+ date, Toast.LENGTH_LONG).show()
            }
            scannedcontent.setText(result.contents)
            val value: String = scannedcontent.text.toString()
            val intent = Intent(activity, QRScannedResultActivity::class.java)
            intent.putExtra("sendedscannedcontent", value)
            startActivity(intent)
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data)
    }
}

}

我在我的清单中遵循了这些内容

        android:screenOrientation="fullSensor"
        tools:replace="screenOrientation"

我已关注此链接并添加了这些说明,但它没有用..

我无法理解我正在关注的项目显示在正确的位置但是我使用的相同代码给了我错误的方向。我也关注了这个链接,但它不起作用链接

标签: androidkotlinandroid-fragmentsqr-codezxing

解决方案


将此添加到清单文件中,您刚刚错过了您正在关注的教程

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

推荐阅读