0

In my app I open the camera for the user to take a picture of a text. Unfortunately the autofocus does not work at all and the picture is very blurry on an s7. I don't really understand the code either as I just copied it. Please find below the code:

mCamera = getCameraInstance();

            Camera.Parameters params = mCamera.getParameters();
         params.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);

        //params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);


        Camera.Size desiredSize = getPictureSize(params.getSupportedPictureSizes());
        System.out.println(desiredSize.width);
       params.setPictureSize(desiredSize.width, desiredSize.height);
            mCamera.setParameters(params);

            mPreview = new CameraPreview(this, mCamera);
            FrameLayout preview = findViewById(R.id.camera_preview);
            preview.addView(mPreview);
            rotation = CameraPreview.correctCameraDisplayOrientation(MainActivity.this, mCamera);
            apiInterface = RetrofitInstance.getRetrofitInstance().create(ApiInterface.class);

            Bundle extras = getIntent().getExtras();
            if(extras!=null){
                Uri imgUri = account.getPhotoUrl();
                new ImageLoadTask(imgUri, toolbar).execute();
            }

      //  }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);

        // Associate searchable configuration with the SearchView
        SearchManager searchManager =
               (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));


       // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
         //   searchView.getSuggestionsAdapter().setDropDownViewTheme(this.getTheme());
       // }

        return true;
    }

    private Camera.Size getPictureSize(List<Camera.Size> sizes) {

        for (Camera.Size size : sizes) {
            if ((size.width * size.height) / 1024000 <= 2.5) {
                return size;
            }
        }

        return null; 

Does someone now what the problem here is? I am very grateful for any kind of help!

  • *"the autofocus does not work at all and the picture is very blurry on an s7"*: do you mean that on other devices same app works well? – Alex Cohn Dec 10 '18 at 07:56
  • Please understand that working with Android camera is not trivial. Consider using some library that wraps it for you, like [fotoapparat](https://github.com/RedApparat/Fotoapparat). – Alex Cohn Dec 10 '18 at 08:00
  • Thanks for sharing the library. Yes, the apps works well on HTC phone. On an S8 the camera is blurry aswell though. All devices run Android 8.0 – Christopher H Dec 12 '18 at 12:05
  • If you *only need to capture a picture once in a while, and at best quality each device can produce*, consider [delegating this task to the Camera app](https://developer.android.com/training/camera/photobasics#TaskCaptureIntent). This approach has its limitations and caveats. Until recently, the doc was titled "Taking Photos Simply". I am happy that it has been renamed. – Alex Cohn Dec 12 '18 at 12:34

1 Answers1

0

You need to set focus on the camera. use below code for setting focus

parameters = mCamera.getParameters();
    if (parameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
    } else {
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
    }       
    parameters.setJpegQuality(100);
Sanjay Bhalani
  • 2,424
  • 18
  • 44