-1

When I place the following code into my Android Studio for the first time, it highlighted a lot of errors:

import java.util.List;

public void enableAutofocus()
{
    camera = camera.open();
    Camera.Parameters parameters = camera.getParameters();
    List<String> focusModes = parameters.getSupportedFocusModes();
    if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO))
    {
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
    }
    camera.setParameters(parameters);
}

So, I did alt+enter and it inserted the right import:

import android.hardware.Camera;
import java.util.List;

public class OcrFocusPluginClass
{
    public void enableAutofocus()
    {
        camera = Camera.open();
        Camera.Parameters parameters = camera.getParameters();
        List<String> focusModes = parameters.getSupportedFocusModes();
        if ( focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO) )
        {
            parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
        }
        camera.setParameters(parameters);
    }
}

But then immediately, the lower-case cameras now have red underlines.

I realised the Camera interface was deprecated, so I included the new android.hardware.camera2 API for new applications:

import android.hardware.camera2;

But now in addition to the camera code, all the focus code is again highlighted wrong.

What am I missing?

enter image description here

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

1 Answers1

0

If you replace all of the Camera. instances with android.hardware.Camera., all of the errors will go away. The class is still deprecated, but it will likely work.

Example fixed code:

android.hardware.Camera camera = android.hardware.Camera.open();

If you'd like to know the camera2 implementation, you might want to look at Android camera android.hardware.Camera deprecated.

evanklicker
  • 121
  • 6
  • Thank you. I did that, but as can be seen in the image (edited question) I still have crossed-out bits and red bits. Can you help me further please? –  Jun 13 '17 at 06:49
  • 1
    When you assign `camera.open()` to the variable `camera`, you forgot to declare the type. So just add `android.hardware.Camera` in front of that line. The final result will look like `android.hardware.Camera camera = Camera.open();`. – evanklicker Jun 13 '17 at 19:27
  • Thank you. It still crosses out my `Camera` so I wonder if that is just a warning to tell me that it is deprecated or is that an error? The app APK builds and runs, but how do I establish if it accesses the camera? –  Jun 14 '17 at 07:58
  • 1
    The IDE will warn you that using a deprecated class is bad, but it won't error because of it. It wouldn't be a bad idea for you to look into using camera2 instead though. I'm not sure how to determine if the camera is being accessed when you're using an emulator. I'd suggest trying the application on an actual, physical android phone instead. You can find where to do that [here](https://developer.android.com/training/basics/firstapp/running-app.html). – evanklicker Jun 14 '17 at 13:09