Example usage for android.hardware Camera getParameters

List of usage examples for android.hardware Camera getParameters

Introduction

In this page you can find the example usage for android.hardware Camera getParameters.

Prototype

public Parameters getParameters() 

Source Link

Document

Returns the current settings for this Camera service.

Usage

From source file:Main.java

public static boolean isFlashSupported(Camera camera, Context context) {

    mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    if (camera != null) {
        Camera.Parameters mParameters = camera.getParameters();

        if (mParameters.getFlashMode() == null) {
            mPrefFlash = mPreferences.edit().putBoolean("mPrefFlash", false).commit();
            return false;
        }/*from w ww.j  ava  2s. c o  m*/

        List<String> mSupportedFlashModes = mParameters.getSupportedFlashModes();
        if (mSupportedFlashModes == null || mSupportedFlashModes.isEmpty() || mSupportedFlashModes.size() == 1
                && mSupportedFlashModes.get(0).equals(Camera.Parameters.FLASH_MODE_OFF)) {
            mPrefFlash = mPreferences.edit().putBoolean("mPrefFlash", false).commit();
            return false;
        }
    } else {
        mPrefFlash = mPreferences.edit().putBoolean("mPrefFlash", false).commit();
        return false;
    }

    mPrefFlash = mPreferences.edit().putBoolean("mPrefFlash", true).commit();
    return true;
}

From source file:Main.java

private static void obtainCameraRatios() {
    for (int i = 0; i < Camera.getNumberOfCameras(); i++) {
        Camera camera = Camera.open(i);
        Camera.CameraInfo info = new Camera.CameraInfo();
        Camera.getCameraInfo(i, info);//from   www. j  av  a 2  s.co m
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            frontCameraRatio = camera.getParameters().getPreferredPreviewSizeForVideo();
        } else {
            backCameraRatio = camera.getParameters().getPreferredPreviewSizeForVideo();
        }
        camera.release();
    }
}

From source file:Main.java

public static Camera.Size getOptimalCameraPreviewSize(Camera camera, int width, int height) {
    if (camera == null) {
        return null;
    }//w  w  w  . j  ava 2s .  co  m

    double targetRatio = (double) height / width;

    Camera.Parameters params = camera.getParameters();
    List<Camera.Size> sizes = params.getSupportedPreviewSizes();

    Camera.Size optimalSize = null;

    // selecting optimal camera preview size
    int minDiff = Integer.MAX_VALUE;
    for (Camera.Size size : sizes) {
        double ratio = (double) size.height / size.width;
        if (Math.abs(ratio - targetRatio) > RATIO_TOLERANCE)
            continue;
        if (Math.abs(size.height - height) < minDiff) {
            optimalSize = size;
            minDiff = Math.abs(size.height - height);
        }
    }

    if (optimalSize == null) {
        minDiff = Integer.MAX_VALUE;
        for (Camera.Size size : sizes) {
            if (Math.abs(size.height - height) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - height);
            }
        }
    }
    return optimalSize;
}

From source file:org.webrtc.videoengine.VideoCaptureDeviceInfoAndroid.java

private static String getDeviceInfo() {
    try {/*from  w w  w .  ja v  a2 s  .  c o m*/
        JSONArray devices = new JSONArray();
        for (int i = 0; i < Camera.getNumberOfCameras(); ++i) {
            CameraInfo info = new CameraInfo();
            Camera.getCameraInfo(i, info);
            String uniqueName = deviceUniqueName(i, info);
            JSONObject cameraDict = new JSONObject();
            devices.put(cameraDict);
            List<Size> supportedSizes;
            List<int[]> supportedFpsRanges;
            Camera camera = null;
            try {
                camera = Camera.open(i);
                Parameters parameters = camera.getParameters();
                supportedSizes = parameters.getSupportedPreviewSizes();
                supportedFpsRanges = parameters.getSupportedPreviewFpsRange();
                Log.d(TAG, uniqueName);
            } catch (RuntimeException e) {
                Log.e(TAG, "Failed to open " + uniqueName + ", skipping", e);
                continue;
            } finally {
                if (camera != null) {
                    camera.release();
                }
            }

            JSONArray sizes = new JSONArray();
            for (Size supportedSize : supportedSizes) {
                JSONObject size = new JSONObject();
                size.put("width", supportedSize.width);
                size.put("height", supportedSize.height);
                sizes.put(size);
            }

            JSONArray mfpsRanges = new JSONArray();
            for (int[] range : supportedFpsRanges) {
                JSONObject mfpsRange = new JSONObject();
                // Android SDK deals in integral "milliframes per second"
                // (i.e. fps*1000, instead of floating-point frames-per-second) so we
                // preserve that through the Java->C++->Java round-trip.
                mfpsRange.put("min_mfps", range[Parameters.PREVIEW_FPS_MIN_INDEX]);
                mfpsRange.put("max_mfps", range[Parameters.PREVIEW_FPS_MAX_INDEX]);
                mfpsRanges.put(mfpsRange);
            }

            cameraDict.put("name", uniqueName);
            cameraDict.put("front_facing", isFrontFacing(info)).put("orientation", info.orientation)
                    .put("sizes", sizes).put("mfpsRanges", mfpsRanges);
        }
        String ret = devices.toString(2);
        Log.d(TAG, ret);
        return ret;
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static boolean setOptimalCameraPreviewSize(Camera camera, int width, int height) {
    if (camera == null) {
        return false;
    }/* w  w  w  .j  a  v  a  2s .  c o m*/

    double targetRatio = (double) height / width;

    Camera.Parameters params = camera.getParameters();
    List<Camera.Size> sizes = params.getSupportedPreviewSizes();

    Camera.Size optimalSize = null;

    // selecting optimal camera preview size
    int minDiff = Integer.MAX_VALUE;
    for (Camera.Size size : sizes) {
        double ratio = (double) size.height / size.width;
        if (Math.abs(ratio - targetRatio) > RATIO_TOLERANCE)
            continue;
        if (Math.abs(size.height - height) < minDiff) {
            optimalSize = size;
            minDiff = Math.abs(size.height - height);
        }
    }

    if (optimalSize == null) {
        minDiff = Integer.MAX_VALUE;
        for (Camera.Size size : sizes) {
            if (Math.abs(size.height - height) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - height);
            }
        }
    }

    params.setPreviewSize(optimalSize.width, optimalSize.height);

    List<String> focusModes = params.getSupportedFocusModes();
    if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
        params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
    }

    // for Mi3, only support ImageFormat.NV21
    // for most cameras, ImageFormat.NV21 are support worldwide
    // so use default preview format
    // params.setPreviewFormat(ImageFormat.JPEG);
    camera.setParameters(params);
    return true;
}

From source file:Main.java

/**
 * Checks if the device has a camera flash
 *
 * @param context the context/*from w  w w  .j  av  a  2  s  . c  o m*/
 * @return true if camera flash is available
 */
public static boolean hasCameraFlash(@NonNull Context context, @NonNull Camera camera) {
    boolean hasFlash = context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
    try {
        Camera.Parameters p;

        if (hasFlash) {
            p = camera.getParameters();
            if (p.getSupportedFlashModes() == null) {
                hasFlash = false;
            } else {
                if (p.getSupportedFlashModes().size() == 1 && p.getSupportedFlashModes().get(0).equals("off")) {
                    hasFlash = false;
                }
            }
        }
    } finally {
        camera.release();
    }
    return hasFlash;
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public static void focusOnTouch(Camera camera, MotionEvent event) {
    Rect focusRect = calculateTapArea(camera, event.getRawX(), event.getRawY(), 1f);
    Rect meteringRect = calculateTapArea(camera, event.getRawX(), event.getRawY(), 1.5f);

    Camera.Parameters parameters = camera.getParameters();
    parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
    int version = Build.VERSION.SDK_INT;
    if (version > Build.VERSION_CODES.ICE_CREAM_SANDWICH && parameters.getMaxNumFocusAreas() > 0) {
        List<Camera.Area> focusAreas = new ArrayList<Camera.Area>();
        focusAreas.add(new Camera.Area(focusRect, 1000));

        parameters.setFocusAreas(focusAreas);
    }/*from   w  w  w . j a  va 2 s  .  c  o m*/

    if (version > Build.VERSION_CODES.ICE_CREAM_SANDWICH && parameters.getMaxNumMeteringAreas() > 0) {
        List<Camera.Area> meteringAreas = new ArrayList<Camera.Area>();
        meteringAreas.add(new Camera.Area(meteringRect, 1000));

        parameters.setMeteringAreas(meteringAreas);
    }

    camera.setParameters(parameters);
}

From source file:at.wada811.utils.CameraUtils.java

/**
 * ?/*from   ww  w  .j a  v  a2s  .  co m*/
 * 
 * @param camera
 * @param pictureSize
 */
public static void setPictureSize(Camera camera, Size pictureSize) {
    Parameters params = camera.getParameters();
    params.setPictureSize(pictureSize.width, pictureSize.height);
    camera.setParameters(params);
}

From source file:at.wada811.utils.CameraUtils.java

/**
 * ???//from w ww . java 2  s .  c  o  m
 * 
 * @param camera
 * @param previewSize
 */
public static void setPreviewSize(Camera camera, Size previewSize) {
    Parameters params = camera.getParameters();
    params.setPreviewSize(previewSize.width, previewSize.height);
    camera.setParameters(params);
}

From source file:edu.winlab.minijarvis.activity.CameraActivity.java

/**
 * To get camera instance//from ww  w  .  ja v a 2 s. c  o  m
 * @return Camera
 */
private static Camera getCameraInstance() {
    Camera camera = null;
    try {
        camera = Camera.open();

        // Work around for Camera preview issues
        Camera.Parameters params = camera.getParameters();
        params.setPreviewFpsRange(30000, 30000);
        camera.setParameters(params);
    } catch (Exception e) {
        System.out.println(e.toString());
        e.printStackTrace();
    }
    return camera;
}