Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.annotation.TargetApi;

import android.hardware.Camera;
import android.os.Build;

import java.util.Collection;
import java.util.List;

public class Main {
    static android.hardware.Camera.Parameters getFailsafeCameraParameters(Camera camera) {
        android.hardware.Camera.Parameters parameters = camera.getParameters();

        if (Build.VERSION.SDK_INT >= 9) {
            setFocusMode(parameters);
        }
        return parameters;
    }

    @TargetApi(9)
    static void setFocusMode(android.hardware.Camera.Parameters parameters) {
        String focusMode;

        List<String> focusModes = parameters.getSupportedFocusModes();

        if (Build.VERSION.SDK_INT >= 14) {
            focusMode = getFocusMode14(focusModes);
        } else {
            focusMode = getFocusMode9(focusModes);
        }

        if (null == focusMode) {
            focusMode = findSettableValue(focusModes, android.hardware.Camera.Parameters.FOCUS_MODE_MACRO,
                    android.hardware.Camera.Parameters.FOCUS_MODE_EDOF);
        }

        if (null != focusMode) {
            parameters.setFocusMode(focusMode);
        }
    }

    @TargetApi(14)
    static String getFocusMode14(List<String> focusModes) {

        boolean safeMode = false;

        if (safeMode) {
            return findSettableValue(focusModes, android.hardware.Camera.Parameters.FOCUS_MODE_AUTO);
        } else {
            return findSettableValue(focusModes, android.hardware.Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE,
                    android.hardware.Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO,
                    android.hardware.Camera.Parameters.FOCUS_MODE_AUTO);
        }
    }

    @TargetApi(9)
    static String getFocusMode9(List<String> focusModes) {
        return findSettableValue(focusModes, android.hardware.Camera.Parameters.FOCUS_MODE_AUTO);
    }

    static String findSettableValue(Collection<String> supportedValues, String... desiredValues) {
        String result = null;
        if (supportedValues != null) {
            for (String desiredValue : desiredValues) {
                if (supportedValues.contains(desiredValue)) {
                    result = desiredValue;
                    break;
                }
            }
        }
        return result;
    }
}