0

Is it possible to set the focus area in an Android camera application without any touch interaction, but by coordinates, which are received via a Network?

UPDATE: So far I tried the following code snippet:

public void doFocus(final Rect focusRect) {

    try {
        if (camera != null) {
            List<Area> focusList = new ArrayList<Area>();
            Area focusArea = new Area(focusRect, 1000);
            focusList.add(focusArea);
            Parameters param = camera.getParameters();
            //param.setFocusMode(Parameters.FOCUS_MODE_AUTO);
            Log.d(TAG, param.getMaxNumFocusAreas() + ";" + param.getMaxNumMeteringAreas() + " >> " + focusRect.toString());
            param.setFocusAreas(focusList);
            param.setMeteringAreas(focusList);
            camera.setParameters(param);
            camera.autoFocus(autoFocusCallback);
        }
    } catch (Exception e) {
        e.printStackTrace();
        Log.i(TAG, "Unable to autofocus");
    }
}

This method is called in here:

// Fixation of focus with touch
public void setCoordinates() {
    float x = new MainActivity().coordinates().get(0);
    float y = 1490f - (new MainActivity().coordinates().get(1));         //previewHeight minus input to get y coord in Android

    Rect rect = new Rect(
            (int) (x - 100),
            (int) (y - 100),
            (int) (x + 100),
            (int) (y + 100));


    final Rect targetFocusRect = new Rect(
            rect.left * 2000 / previewWidth - 1000,
            rect.top * 2000 / previewHeight - 1000,
            rect.right * 2000 / previewWidth - 1000,
            rect.bottom * 2000 / previewHeight - 1000);

    out.println("FINAL RECT:" + targetFocusRect);

    cameraPreview.doFocus(targetFocusRect);

}

This code works fine with touch input, but since I modified it and put only specific coordinates, the focus won't be set.

petey
  • 16,914
  • 6
  • 65
  • 97
Viktoria
  • 533
  • 2
  • 7
  • 24

1 Answers1

0

You probably want auto-focus (not manual) but in a programmatically set area, without user input. This is possible:

Parameters parameters = camera.getParameters();
parameters.setFocusMode(Parameters.FOCUS_MODE_AUTO);
parameters.setFocusAreas(Lists.newArrayList(new Camera.Area(focusRect, 1000)));

camera.setParameters(parameters);
camera.autoFocus(this);

For the networking part, you will find enough resources online :)

KYL3R
  • 3,877
  • 1
  • 12
  • 26
  • The network works perfectly fine for me, I get constant coordinates data. But setting those coordinates into `setFocusArea` just doesn't work. – Viktoria May 17 '18 at 15:13
  • Updated my question for more clarification. – Viktoria May 17 '18 at 15:32
  • why is the auto-focus line commented? I think you need that. Also, if it works with touch, print the coordinates and make sure you use proper coordinates in your network-code. – KYL3R May 18 '18 at 07:50