Change screen orientation automatically while scanning (using ZXING library)

南笙酒味 提交于 2020-01-24 02:49:20

问题


I've MainActivity.kt where I show different fragments for different needs. At some point, I press button 'X' that calls startScanner() function:

private fun startScanner() {
    IntentIntegrator(this)
            .setOrientationLocked(false)
            .setPrompt("SCANNING?")
            .initiateScan()
}

Manifest.xml:

<activity
    android:name=".MainActiity"
    android:theme="@style/AppTheme"
    tools:replace="android:screenOrientation"
    android:stateNotNeeded="true"
    android:screenOrientation="fullSensor"
    android:windowSoftInputMode="stateHidden" />

Gradle.file:

compile 'com.journeyapps:zxing-android-embedded:3.6.0'

It does open scanner and everything, but in landscape mode.

Why is this not working?


回答1:


There is a shortcut to do this. Just add this to the manifest:

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



回答2:


You can set the orientation programmatically (in your activity):

ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

ActivityInfo.SCREEN_ORIENTATION_PORTRAIT

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);



回答3:


in addition of this answer https://stackoverflow.com/a/35465968/7666442

I found way to change Orientation of zxing scanner activity automatically when device Orientation change

Try this way

CaptureActivityPortrait

public class CaptureActivityPortrait extends CaptureActivity {
//Nothing in side.
}

CaptureActivityPortrait in manifest file

    <activity
        android:name=".CaptureActivityPortrait"
        android:stateNotNeeded="false"
        android:theme="@style/zxing_CaptureTheme"
        android:windowSoftInputMode="stateAlwaysHidden"/>

use this way in your activity

public class MyActivity extends AppCompatActivity {

    IntentIntegrator qrScan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        qrScan = new IntentIntegrator(this).setCaptureActivity(CaptureActivityPortrait.class);

        qrScan.setOrientationLocked(false);
        qrScan.initiateScan();
    }


}


来源:https://stackoverflow.com/questions/51307323/change-screen-orientation-automatically-while-scanning-using-zxing-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!