3

I am using SurfaceView and Google's Mobile Vision library. For many devices it looks fine but when using with few devices like Nexus 7 the camera view comes in Landscape mode. Which makes it difficult for Scanning barcodes etc as it is difficult to focus and position correctly.

In Vision library as I have explored there is no method such that they return the hardware camera so we can manage the orientation like if the camera view returns landscape then we can dynamically rotate the view to make it look like portrait.

So wanted to ask if there is any way for Devices like Nexus 7 to change the Camera or View to Portrait.

Any help will be welcomed! Thanks

2 Answers2

0

Many tabs has their camera mounted rotated, so that when held horizontally, the picture will be taken as "portrait", even though the image is actually wider than it is high. I learned it the hard way, on an app i built some time ago. The only way was to check the screen-aspect vs the image-aspect and image rotation. By comparing these, you can infer whether a camera image is rotated correctly, or whether it needs a 90 degree post-rotation.

Christian
  • 1,155
  • 8
  • 13
0

I found a solution for myself getting an idea from this persons answer: https://stackoverflow.com/a/41634379/5028531

So what I did:

        cameraPreview.getHolder().addCallback(new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
                try {

                    cameraSource.start(cameraPreview.getHolder());
                    Field[] declaredFields = CameraSource.class.getDeclaredFields();

                    for (Field field : declaredFields) {
                        if (field.getType() == Camera.class) {
                            field.setAccessible(true);
                            try {
                                Camera camera = (Camera) field.get(cameraSource);
                                if (camera != null) {
                                    Camera.Parameters params= camera.getParameters();
                                    camera.setDisplayOrientation(0);
                                }
                            } catch (IllegalAccessException | RuntimeException e) {
                                e.getMessage();
                            }

                            break;
                        }
                    }
                } catch (IOException e) {
                    Log.e("CAMERA SOURCE", e.getMessage());
                    e.printStackTrace();
                }
            } else {
                Log.w("CAMERA SOURCE", "Permission not granted");
                Toast.makeText(getActivity(), "Camera permission denied", Toast.LENGTH_SHORT).show();
            }
        }
Community
  • 1
  • 1
Arnold Balliu
  • 1,099
  • 10
  • 21