Search code examples
javaandroidzxing

How to use ZXing bar code scanner in both landscape and portrait in android


I am using me.dm7.barcodescanner.zxing.ZXingScannerView in my project. Now I can scan the bar code. But I can not able to use this in landscape mode. I am trying to use rotation.

 scannerView = new ZXingScannerView(this);
        scannerView.setAutoFocus(true);
//        scannerView.setRotation(90);
//        scannerView.setRotationX(90);
        contentFrame.addView(scannerView);

But this is not working. Please help me to use this scanner in portrait and landscape. Herewith I attached the code which I am using.

<activity
            android:name="com.trackx.mobile.barcodescanner.BarcodeScanner"
            android:theme="@style/TM_MaterialTheme"
            android:configChanges="orientation|screenSize"
            android:label="SCAN BARCODE"
            android:launchMode="singleTop"
            />

My Scanner code is:

 public class BarcodeScanner extends AppCompatActivity implements ZXingScannerView.ResultHandler, View.OnClickListener{
    private ZXingScannerView scannerView;
    private ImageView mTorchButton = null;
    private static boolean torchStatus = false;



    @Override
    public void onCreate(Bundle state) {
        super.onCreate(state);
        setContentView(R.layout.scanning_layout);
        ViewGroup contentFrame = (ViewGroup) findViewById(R.id.content_frame);

        scannerView = new ZXingScannerView(this);
        scannerView.setAutoFocus(true);
        contentFrame.addView(scannerView);
        mTorchButton = (ImageView)findViewById(R.id.torch_button);
        mTorchButton.setOnClickListener(this);

        boolean hasFlash =this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
        if(!hasFlash){
            mTorchButton.setVisibility(View.GONE);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        scannerView.setResultHandler(this);
        scannerView.startCamera();
    }

    @Override
    public void onPause() {
        super.onPause();
        scannerView.stopCamera();
    }

    @Override
    public void handleResult(Result rawResult) {
        //Call back data to main activity
        Intent intent = new Intent();
        intent.putExtra(Constants.FORMAT, rawResult.getBarcodeFormat().toString());
        intent.putExtra(Constants.CONTENT, rawResult.getText());

        setResult(Activity.RESULT_OK, intent);
        finish();
    }

Solution

  • I solved this issue. Added the following in manifest

    android:screenOrientation="sensor"
    

    Now I can use in both orientations.