ZXing 3.6.0在人像模式下?

时间:2018-12-13 10:53:26

标签: android zxing

我使用XZing 3.6.0扫描Android应用程序中的QR码。

MainActivity.java

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.Result;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

public class MainActivity extends AppCompatActivity  {

    Button btnScan;
    TextView tv;

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

        btnScan = (Button) findViewById(R.id.btn_scan);
        tv = (TextView) findViewById(R.id.tv);

        btnScan.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                IntentIntegrator scanIntegrator = new IntentIntegrator(MainActivity.this);
                scanIntegrator.setOrientationLocked(false);
                scanIntegrator.initiateScan();
            }
        });
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
        if (scanningResult != null) {
            String scanContent = scanningResult.getContents();
            tv.setText(scanContent);
        }
        else {
            Toast toast = Toast.makeText(getApplicationContext(),
                    "No scan data received!", Toast.LENGTH_SHORT);
            toast.show();
        }
    }

}

AndroidManifest.xml

 <activity
        android:name=".MainActivity"
        android:screenOrientation="portrait"
        >

它起作用...除了照相机以横向模式显示之外。如何将其更改为纵向模式?

2 个答案:

答案 0 :(得分:1)

将此添加到清单文件中:

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

答案 1 :(得分:0)

在XML文件中尝试:

   <com.journeyapps.barcodescanner.CompoundBarcodeView
            android:id="@+id/barcode_scanner"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:zxing_viewfinder_mask="#ffffff"
            android:layout_weight="40"
            >

        </com.journeyapps.barcodescanner.CompoundBarcodeView>

这是片段代码:

public  class ScanFragment extends Fragment {
private String toast;
private LinearLayout qrCameraLayout;
IntentIntegrator intentIntegrator;
private CompoundBarcodeView barcodeView;
private TextView tv_qr_print;


public ScanFragment() {
}


public static ScanFragment newInstance(int page, String title, ViewPager viewPager) {
    ScanFragment fragmentFirst = new ScanFragment();
    Bundle args = new Bundle();
    args.putInt("someInt", page);
    args.putString("someTitle", title);
    fragmentFirst.setArguments(args);
    return fragmentFirst;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    displayToast();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    intentIntegrator = new IntentIntegrator(getActivity());


    View view = inflater.inflate(R.layout.screen_scanner, container, false);
    qrCameraLayout = (LinearLayout) view.findViewById(R.id.ll_qrcamera);

    barcodeView = (CompoundBarcodeView) view.findViewById(R.id.barcode_scanner);
    tv_qr_print = (TextView) view.findViewById(R.id.tv_qr_print);

    barcodeView.decodeContinuous(callback);
    /* View view = inflater.inflate(R.layout.fragment_scan, container, false);*/
    CustomButton scan = (CustomButton) view.findViewById(R.id.scan_from_fragment);
    scan.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           /* scanFromFragment();*/
        }
    });
    return view;
}

private BarcodeCallback callback = new BarcodeCallback() {
    @Override
    public void barcodeResult(BarcodeResult result) {
        if (result.getText() != null) {
            barcodeView.setStatusText(result.getText());
            tv_qr_print.setText(result.getText());
            displayToast();

        }

        //Do something with code result
    }

    @Override
    public void possibleResultPoints(List<ResultPoint> resultPoints) {
    }
};


public void scanFromFragment() {
    intentIntegrator.setOrientationLocked(true);

    intentIntegrator.forSupportFragment(this).initiateScan();



}

private void displayToast() {
    if(getActivity() != null && toast != null) {
        Toast.makeText(getActivity(), toast, Toast.LENGTH_LONG).show();
        toast = null;
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if(result != null) {
        if(result.getContents() == null) {
            toast = "Cancelled from fragment";
        } else {
            toast = "Scanned from fragment: " + result.getContents();
        }

        // At this point we may or may not have a reference to the activity
        displayToast();
    }
}

@Override
public void onResume() {
    barcodeView.resume();
    barcodeView.getViewFinder().setVisibility(View.GONE);
    super.onResume();
}

@Override
public void onPause() {
    barcodeView.pause();
    super.onPause();
}
}