簡體   English   中英

相機方向 風景 Android Zxing

[英]Camera Orientation Landscape Android Zxing

我正在開發使用 Zxing 庫掃描 QR 的 Android 應用程序。 我使用 Fragment 實現了 Zxing QR 掃描儀。 我面臨的主要問題是相機始終以橫向模式打開,但在我所關注的原始項目中始終以縱向模式打開。 這是我正在關注的教程

我已經在 Manifest 和 Fragment 中添加了 Orientation。

這是我的主要片段

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"

我已經按照此鏈接添加了這些說明但它沒有用..

我無法理解我所關注的項目顯示在正確的位置,但我使用的相同代碼給了我錯誤的方向。 我也跟着這個鏈接,但它不起作用鏈接

將此添加到清單文件中,您剛剛錯過了您正在關注的教程

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM