set Camera Torch - Android Camera

Android examples for Camera:Camera Attribute

Description

set Camera Torch

Demo Code


import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import android.hardware.Camera;

public class Main {
  private static final String TAG = "";

  public static void setTorch(Camera.Parameters parameters, boolean on) {
    List<String> supportedFlashModes = parameters.getSupportedFlashModes();
    String flashMode;/*from  www.j  ava 2 s.c o  m*/
    if (on) {
      flashMode = findSettableValue("flash mode", supportedFlashModes, Camera.Parameters.FLASH_MODE_TORCH,
          Camera.Parameters.FLASH_MODE_ON);
    } else {
      flashMode = findSettableValue("flash mode", supportedFlashModes, Camera.Parameters.FLASH_MODE_OFF);
    }
    if (flashMode != null) {
      if (flashMode.equals(parameters.getFlashMode())) {
      } else {

        parameters.setFlashMode(flashMode);
      }
    }
  }

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

}

Related Tutorials