• 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)

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:
Well...the problem is not reading the QR but since the camera is not "aligned" with screen orientation, it is a little problematic to position the QR code in front of the camera lens since when you move the code, the preview image shows a different position of the code.
 
[GALLERY=media, 1585]Tablet_C19 by washburn_it posted Mar 16, 2022 at 5:12 PM[/GALLERY]

as you can see, the sheet is "horizontal" in the camera preview but actually it has the same orientation of the tablet, vertical (or "portrait").
Then the camera shows the preview rotated 90° counterclockwise...
 
Back
Top Bottom