• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Forcing camera rotation (Android 7.1.2)

washburn_it

Lurker
Mar 16, 2022
3
0
Hi,

I'm developing an app to scan QR codes (not for Covid-19) that will run on a (chinese) tablet equipped with Android 7.1.2.
The app works and I'm able to read QR codes but unfortunately the camera is rotated 90° (counterclockwise) respect to the display that is in "portrait" mode.
This tablet doesn't have options for display rotation so it is "fixed" to portrait mode.
Since (apparently) it's not possible to rotate the camera through the operating system (no option found, on the camera app neither), I tried to "force" the rotation through my app but all the examples that I found are relating to deprecated functions that are not supported anymore.
I'm using Android Studio "Bumblebee" and "targetSDK" is 25.
This is the code to initialize camera and code scanner:
Java:
private void initialiseDetectorsAndSources()
    {
        String str="";

        Toast.makeText(getApplicationContext(), "Barcode scanner started", Toast.LENGTH_SHORT).show();

        barcodeDetector = new BarcodeDetector.Builder(this)
                .setBarcodeFormats(Barcode.ALL_FORMATS)
                .build();

        cameraSource = new CameraSource.Builder(this, barcodeDetector)
                .setRequestedPreviewSize(1920, 1080)
                .setAutoFocusEnabled(true) //you should add this feature
                .build();

       
        surfaceView.getHolder().addCallback(new SurfaceHolder.Callback()
        {
            @Override
            public void surfaceCreated(SurfaceHolder holder)
            {
                try
                {
                    if (ActivityCompat.checkSelfPermission(ScannerBarcodeActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)
                    {
                        cameraSource.start(surfaceView.getHolder());
                    }
                    else
                    {
                        ActivityCompat.requestPermissions(ScannerBarcodeActivity.this, new
                                String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
                    }

                } catch (IOException e)
                {
                    e.printStackTrace();
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
            {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder)
            {
                cameraSource.stop();
            }
        });

        barcodeDetector.setProcessor(new Detector.Processor<Barcode>()
        {
            @Override
            public void release()
            {
                Toast.makeText(getApplicationContext(), "To prevent memory leaks barcode scanner has been stopped", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void receiveDetections(Detector.Detections<Barcode> detections)
            {
                final SparseArray<Barcode> barcodes = detections.getDetectedItems();
                if (barcodes.size() != 0)
                {
                    String str= barcodes.valueAt(0).displayValue; /**scanned code*/
                    TextView txt= (TextView) findViewById(R.id.txtBarcodeValue);
                    txt.setText(str);

                    Toast.makeText(ScannerBarcodeActivity.this, str, Toast.LENGTH_LONG).show(); 
                }
            }
        });
    }

How can I force rotation of the camera?
Thank you, regards.

Roberto
 
Hi,

I'm developing an app to scan QR codes (not for Covid-19) that will run on a (chinese) tablet equipped with Android 7.1.2.
The app works and I'm able to read QR codes but unfortunately the camera is rotated 90° (counterclockwise) respect to the display that is in "portrait" mode.
This tablet doesn't have options for display rotation so it is "fixed" to portrait mode.
Since (apparently) it's not possible to rotate the camera through the operating system (no option found, on the camera app neither), I tried to "force" the rotation through my app but all the examples that I found are relating to deprecated functions that are not supported anymore.
I'm using Android Studio "Bumblebee" and "targetSDK" is 25.
This is the code to initialize camera and code scanner:
Java:
private void initialiseDetectorsAndSources()
    {
        String str="";

        Toast.makeText(getApplicationContext(), "Barcode scanner started", Toast.LENGTH_SHORT).show();

        barcodeDetector = new BarcodeDetector.Builder(this)
                .setBarcodeFormats(Barcode.ALL_FORMATS)
                .build();

        cameraSource = new CameraSource.Builder(this, barcodeDetector)
                .setRequestedPreviewSize(1920, 1080)
                .setAutoFocusEnabled(true) //you should add this feature
                .build();

 
        surfaceView.getHolder().addCallback(new SurfaceHolder.Callback()
        {
            @Override
            public void surfaceCreated(SurfaceHolder holder)
            {
                try
                {
                    if (ActivityCompat.checkSelfPermission(ScannerBarcodeActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)
                    {
                        cameraSource.start(surfaceView.getHolder());
                    }
                    else
                    {
                        ActivityCompat.requestPermissions(ScannerBarcodeActivity.this, new
                                String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
                    }

                } catch (IOException e)
                {
                    e.printStackTrace();
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
            {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder)
            {
                cameraSource.stop();
            }
        });

        barcodeDetector.setProcessor(new Detector.Processor<Barcode>()
        {
            @Override
            public void release()
            {
                Toast.makeText(getApplicationContext(), "To prevent memory leaks barcode scanner has been stopped", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void receiveDetections(Detector.Detections<Barcode> detections)
            {
                final SparseArray<Barcode> barcodes = detections.getDetectedItems();
                if (barcodes.size() != 0)
                {
                    String str= barcodes.valueAt(0).displayValue; /**scanned code*/
                    TextView txt= (TextView) findViewById(R.id.txtBarcodeValue);
                    txt.setText(str);

                    Toast.makeText(ScannerBarcodeActivity.this, str, Toast.LENGTH_LONG).show();
                }
            }
        });
    }

How can I force rotation of the camera?
Thank you, regards.

Roberto

I'm not a dev of course, so can't help with your code. But surely device and camera orientation doesn't matter if it's only for scanning QR codes? One thing QRs are square, and Nippon Denso, who invented and patented the QR code, designed them to be read in any orientation. As the QR code's original purpose was for tracking parts and assemblies in vehicle factories.

This QR is rotated, but it can still be scanned no problem.
QR.jpg
 
Last edited:
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones