Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
522 views
in Technique[技术] by (71.8m points)

android - How to use Zxing in portrait mode?

Currently zxing library supports only on landscape mode.For my app i need to use in portrait mode

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is the Solution for Portrait mode Scanning

first declare these two lines in your app level gradle file

implementation 'com.journeyapps:zxing-android-embedded:3.0.1@aar'
implementation 'com.google.zxing:core:3.2.0'

Define a button in your xml file and in Onclick Listener of button write below code in MainActivity java file

IntentIntegrator integrator = new IntentIntegrator(this);
    integrator.setPrompt("Scan a barcode");
    integrator.setCameraId(0);  // Use a specific camera of the device
    integrator.setOrientationLocked(true);
    integrator.setBeepEnabled(true);
    integrator.setCaptureActivity(CaptureActivityPortrait.class);
    integrator.initiateScan();

Write below code in your MainActivity java file after onCreate() method

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
    if(result.getContents() == null) {
        Log.d("MainActivity", "Cancelled scan");
        Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
    } else {
        Log.d("MainActivity", "Scanned");
        st_scanned_result = result.getContents();
        Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();

    }
  }

}

then create a class with name CaptureActivityPortrait that extends CaptureActivity. The class looks like below

  package soAndSo(Your PackageName);

  import com.journeyapps.barcodescanner.CaptureActivity;

  public class CaptureActivityPortrait extends CaptureActivity {
  }

And Most Important declare your CaptureActivityPortrait in manifest file like below

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...