Currently zxing library supports only on landscape mode.For my app i need to use in portrait mode
I wanted to use the Barcode reader in portrait mode. I found the solution here as mentioned in a comment posted earlier in this thread. I thought of putting this as an answer so it is easier to find the solution for the people having the same problem.
To use the scanner in portrait mode, you need to add the following activity in your AndroidManifest.xml
.
<activity
android:name="com.journeyapps.barcodescanner.CaptureActivity"
android:screenOrientation="portrait"
tools:replace="screenOrientation" />
And that's it.
See the link for more details.
Please check as following (Official Documentation)
Add this activity to manifest file
<activity
android:name="com.journeyapps.barcodescanner.CaptureActivity"
android:screenOrientation="fullSensor"
tools:replace="screenOrientation" />
Set integrator OrientationLocked to false
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setOrientationLocked(false);
integrator.initiateScan();
Hope this helps
Use this android library https://github.com/SudarAbisheck/ZXing-Orient
It supports both portrait and landscape orientation.
setDisplayOrientation(int)
does not affect the order of byte array passed in PreviewCallback.onPreviewFrame
. (Refer to JavaDoc for additional info)
It means that you need to rotate the data return from previewCallback, but this is yuv data, you need to convert to rgb data then rotate them. it is possible to scan barcode in portrait mode but it will take longer because it need more time to process data from yuv to rgb and rotate rgb data. so the matter here is how can we get yuv data from previewcallback for portrait mode? when we set setPreviewSize for camera parameters it will get exception if previewsize is not supported. Here is the matter of this issue. If the camera driver does not support previewSize for portrait mode (height > width) you cannot get yuv data in portrait mode. And the rest depends on you, you can scan barcode in portrait mode but it take time longer or you have to change orient of screen to landscape to get result faster.
Just add these code in the AndroidManifest.xml of your project
<activity android:name="com.journeyapps.barcodescanner.CaptureActivity"
android:screenOrientation="sensorPortrait"
android:stateNotNeeded="true"
android:theme="@style/zxing_CaptureTheme"
android:windowSoftInputMode="stateAlwaysHidden"
tools:replace="android:screenOrientation" />
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>