Rogerio Camorim
Rogerio Camorim

Reputation: 381

ZXing auto start with front camera

I am trying to build an app that reads a QRCode from an IDCard that everyone on my company will use.

I would like to use the front camera as default when I run the app. I managed to do it with the rear camera but ideally I don't want to have to use a button to switch.

MainActivity:

public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
    private static final int REQUEST_CAMERA = 1;
    private ZXingScannerView mScannerView;

    Passageiros mPassageiro; //

    Collection<Passageiros> listaPassageiros;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mScannerView = new ZXingScannerView(this){

            @Override
            protected IViewFinder createViewFinderView(Context context) {
                return new CustomZXingScannerView(context);
            }

        };
        List<BarcodeFormat> formats = new ArrayList<>();

        formats.add(BarcodeFormat.QR_CODE);

        setContentView(mScannerView);

        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
            if (checkPermission()) {
                Toast.makeText(getApplicationContext(), "Permission already granted", Toast.LENGTH_LONG).show();

            } else {
                requestPermission();
            }

        }
    }

...

}

How can I do this?

Upvotes: 3

Views: 4096

Answers (1)

shtolik
shtolik

Reputation: 1368

Looking at the sources of ZXing library

ZXingCameraView is extending BarcodeScannerView, which has a private CameraWrapper which you can set with setupLayout method. CameraWrapper allows you to initialize com.android.Camera.

And how to choose necessary out of available cameras is shown in that question: How do I open the "front camera" on the Android platform?

Edit: Actually there is even a ZXing CameraUtils class selecting camera on the back of device. Just change it to return id of front camera and pass that id to startCamera(frontCameraId) on your ZXingCameraView.

Upvotes: 1

Related Questions