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 getMainCameraParameters(Camera camera) {
        android.hardware.Camera.Parameters parameters = camera.getParameters();

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

        if (Build.VERSION.SDK_INT > 13) {
            setAutoExposureLock(parameters);
        }

        if (Build.VERSION.SDK_INT > 7) {
            try {
                if (parameters.getMaxExposureCompensation() != 0 || parameters.getMinExposureCompensation() != 0) {
                    parameters.setExposureCompensation(0);
                }
            } catch (Throwable ignored) {
            }
        }

        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(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    static void setAutoExposureLock(android.hardware.Camera.Parameters parameters) {
        try {
            if (parameters.isAutoExposureLockSupported()) {
                parameters.setAutoExposureLock(false);
            }
        } catch (Throwable ignored) {
        }
    }

    @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;
    }
}