Skip to content Skip to sidebar Skip to footer

How To Set Camera Focus Value Manually In Android

I intend to create an application that can take photos in the following way: When the user touches the screen, it starts to take photos It takes several photos within a few micros

Solution 1:

Answer -- Android setFocusArea and Auto Focus

All I had to do is cancel previously called autofocus. Basically the correct order of actions is this:

protectedvoidfocusOnTouch(MotionEvent event) {
    if (camera != null) {

        camera.cancelAutoFocus();
        Rect focusRect = calculateTapArea(event.getX(), event.getY(), 1f);
        Rect meteringRect = calculateTapArea(event.getX(), event.getY(), 1.5f);

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

        if (meteringAreaSupported) {
            parameters.setMeteringAreas(Lists.newArrayList(new Camera.Area(meteringRect, 1000)));
        }

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

..... update

@OverridepublicvoidsurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    ...
    Parametersp= camera.getParameters();
    if (p.getMaxNumMeteringAreas() > 0) {
       this.meteringAreaSupported = true;
    }
    ...
}

/**
 * Convert touch position x:y to {@link Camera.Area} position -1000:-1000 to 1000:1000.
 */private Rect calculateTapArea(float x, float y, float coefficient) {
    intareaSize= Float.valueOf(focusAreaSize * coefficient).intValue();

    intleft= clamp((int) x - areaSize / 2, 0, getSurfaceView().getWidth() - areaSize);
    inttop= clamp((int) y - areaSize / 2, 0, getSurfaceView().getHeight() - areaSize);

    RectFrectF=newRectF(left, top, left + areaSize, top + areaSize);
    matrix.mapRect(rectF);

    returnnewRect(Math.round(rectF.left), Math.round(rectF.top), Math.round(rectF.right), Math.round(rectF.bottom));
}

privateintclamp(int x, int min, int max) {
    if (x > max) {
        return max;
    }
    if (x < min) {
        return min;
    }
    return x;
}

Post a Comment for "How To Set Camera Focus Value Manually In Android"