Java Code Examples for android.hardware.Camera.Parameters#setFlashMode()

The following examples show how to use android.hardware.Camera.Parameters#setFlashMode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CameraView.java    From LLApp with Apache License 2.0 6 votes vote down vote up
/**
 *  设置闪光灯类型
 *  @param flashMode
 */
@Override
public void setFlashMode(FlashMode flashMode) {
	if(mCamera==null) return;
	mFlashMode = flashMode;
	Parameters parameters=mCamera.getParameters();
	switch (flashMode) {
	case ON:
		parameters.setFlashMode(Parameters.FLASH_MODE_ON);
		break;
	case AUTO:
		parameters.setFlashMode(Parameters.FLASH_MODE_AUTO);
		break;
	case TORCH:
		parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
		break;
	default:
		parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
		break;
	}
	mCamera.setParameters(parameters);
}
 
Example 2
Source File: FlashlightManager.java    From BigApp_Discuz_Android with Apache License 2.0 6 votes vote down vote up
public static void setFlashLightNormal(boolean active) {

        if (camera == null)
            camera = getCamera();


        Parameters params = camera.getParameters();
        if (active) {
            params.setFlashMode(Parameters.FLASH_MODE_TORCH);
            camera.setParameters(params);
            camera.startPreview(); // 开始亮灯;
        } else {
            params.setFlashMode(Parameters.FLASH_MODE_OFF);
            camera.stopPreview(); // 关掉亮灯
            camera.release(); // 关掉照相机
            camera =null;
        }

        ZogUtils.printLog(FlashlightManager.class, "setFlashLightNormal active:" + active);
    }
 
Example 3
Source File: Camera1Controller.java    From pixelvisualcorecamera with Apache License 2.0 5 votes vote down vote up
/**
 * Configures camera parameters common to all configurations.
 * Must be called before preview started.
 */
public void setDefaultParameters(int displayRotation) {
  Log.i(TAG, "setDefaultParameters");
  assertState(STATE_ACQUIRED,
        "Default parameters may only be set before a preview is started");

  CameraInfo info = new CameraInfo();
  Camera.getCameraInfo(cameraId, info);
  boolean lensFacingFront = (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT);

  int previewOrientationDegrees =
      Orientation.getPreviewOrientation(lensFacingFront, displayRotation, info.orientation);
  camera.setDisplayOrientation(previewOrientationDegrees);
  Parameters params = camera.getParameters();

  // We happen to know the preview sizes available for Pixel 2.
  params.setPreviewSize(Utils.MAX_PREVIEW_WIDTH, Utils.MAX_PREVIEW_HEIGHT);
  params.setRotation(
      Orientation.getOutputOrientation(lensFacingFront, displayRotation, info.orientation));

  // Continuous picture is not supported Pixel 2's front camera.
  List<String> supportFocusModes = params.getSupportedFocusModes();
  if (supportFocusModes.contains(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
    Log.i(TAG, "setting continuous picture focus mode");
    params.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
  }

  // HDR+: Flash mode must be off.
  params.setFlashMode(Parameters.FLASH_MODE_OFF);

  // HDR+: Color effect must be none.
  params.setColorEffect(Parameters.EFFECT_NONE);

  // HDR+: White balance must be auto.
  params.setWhiteBalance(Parameters.WHITE_BALANCE_AUTO);

  camera.setParameters(params);
}
 
Example 4
Source File: Utils.java    From code-scanner with MIT License 5 votes vote down vote up
public static void setFlashMode(@NonNull final Parameters parameters,
        @NonNull final String flashMode) {
    if (flashMode.equals(parameters.getFlashMode())) {
        return;
    }
    final List<String> flashModes = parameters.getSupportedFlashModes();
    if (flashModes != null && flashModes.contains(flashMode)) {
        parameters.setFlashMode(flashMode);
    }
}
 
Example 5
Source File: CameraManager.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
public void openLight()   //打开闪光灯
{
    if(camera!=null)
    {
         Parameters parameter=camera.getParameters();  
         parameter.setFlashMode(Parameters.FLASH_MODE_TORCH); 
         camera.setParameters(parameter);
    }
}
 
Example 6
Source File: CameraManager.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
public void closeLight()  //关闭闪光灯
{
    if(camera!=null)
    {
         Parameters parameter=camera.getParameters();  
         parameter.setFlashMode(Parameters.FLASH_MODE_OFF); 
         camera.setParameters(parameter);
    }
}
 
Example 7
Source File: CameraManager.java    From LLApp with Apache License 2.0 5 votes vote down vote up
public void openFlashLight() {
	if (camera != null) {
		Parameters parameter = camera.getParameters();
		parameter.setFlashMode(Parameters.FLASH_MODE_TORCH);
		camera.setParameters(parameter);
	}
}
 
Example 8
Source File: CameraManager.java    From LLApp with Apache License 2.0 5 votes vote down vote up
public void offFlashLight() {
	if (camera != null) {
		Parameters parameter = camera.getParameters();
		parameter.setFlashMode(Parameters.FLASH_MODE_OFF);
		camera.setParameters(parameter);
	}
}
 
Example 9
Source File: CameraManager.java    From LLApp with Apache License 2.0 5 votes vote down vote up
public void switchFlashLight() {
	if (camera != null) {
		Parameters parameter = camera.getParameters();
		if (parameter.getFlashMode().equals(Parameters.FLASH_MODE_TORCH)) {
			parameter.setFlashMode(Parameters.FLASH_MODE_OFF);
		} else {
			parameter.setFlashMode(Parameters.FLASH_MODE_TORCH);
		}

		camera.setParameters(parameter);
	}
}
 
Example 10
Source File: Camera1Handler.java    From Augendiagnose with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Update the flashlight.
 */
private void updateFlashlight() {
	if (mCamera != null && SystemUtil.hasFlashlight()) {
		Parameters parameters = mCamera.getParameters();
		if (parameters.getSupportedFlashModes() != null && parameters.getSupportedFlashModes().contains(mCurrentFlashlightMode)) {
			parameters.setFlashMode(mCurrentFlashlightMode);
			mCamera.setParameters(parameters);
		}
	}
}
 
Example 11
Source File: CameraEncoder.java    From kickflip-android-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the requested flash mode and restarts the
 * camera preview. This will take effect immediately
 * or as soon as the camera preview becomes active.
 * <p/>
 * <p/>
 * Called from UI thread
 */
public void requestFlash(String desiredFlash) {
    mDesiredFlash = desiredFlash;
    /* If mCamera for some reason is null now flash mode will be applied
     * next time the camera opens through mDesiredFlash. */
    if (mCamera == null) {
        Log.w(TAG, "Ignoring requestFlash: Camera isn't available now.");
        return;
    }
    Parameters params = mCamera.getParameters();
    List<String> flashModes = params.getSupportedFlashModes();
    /* If the device doesn't have a camera flash or
     * doesn't support our desired flash modes return */
    if (VERBOSE) {
        Log.i(TAG, "Trying to set flash to: " + mDesiredFlash + " modes available: " + flashModes);
    }

    if (isValidFlashMode(flashModes, mDesiredFlash) && mDesiredFlash != mCurrentFlash) {
        mCurrentFlash = mDesiredFlash;
        mDesiredFlash = null;
        try {
            params.setFlashMode(mCurrentFlash);
            mCamera.setParameters(params);
            if (VERBOSE) {
                Log.i(TAG, "Changed flash successfully!");
            }
        } catch (RuntimeException e) {
            Log.d(TAG, "Unable to set flash" + e);
        }
    }
}
 
Example 12
Source File: VideoStream.java    From spydroid-ipcamera with GNU General Public License v3.0 5 votes vote down vote up
/** Turns the LED on or off if phone has one. */
public synchronized void setFlashState(boolean state) {
	// If the camera has already been opened, we apply the change immediately
	if (mCamera != null) {

		if (mStreaming && mMode == MODE_MEDIARECORDER_API) {
			lockCamera();
		}

		Parameters parameters = mCamera.getParameters();

		// We test if the phone has a flash
		if (parameters.getFlashMode()==null) {
			// The phone has no flash or the choosen camera can not toggle the flash
			throw new RuntimeException("Can't turn the flash on !");
		} else {
			parameters.setFlashMode(state?Parameters.FLASH_MODE_TORCH:Parameters.FLASH_MODE_OFF);
			try {
				mCamera.setParameters(parameters);
				mFlashEnabled = state;
			} catch (RuntimeException e) {
				mFlashEnabled = false;
				throw new RuntimeException("Can't turn the flash on !");
			} finally {
				if (mStreaming && mMode == MODE_MEDIARECORDER_API) {
					unlockCamera();
				}
			}
		}
	} else {
		mFlashEnabled = state;
	}
}
 
Example 13
Source File: VideoStream.java    From libstreaming with Apache License 2.0 5 votes vote down vote up
/** Turns the LED on or off if phone has one. */
public synchronized void setFlashState(boolean state) {
	// If the camera has already been opened, we apply the change immediately
	if (mCamera != null) {

		if (mStreaming && mMode == MODE_MEDIARECORDER_API) {
			lockCamera();
		}

		Parameters parameters = mCamera.getParameters();

		// We test if the phone has a flash
		if (parameters.getFlashMode()==null) {
			// The phone has no flash or the choosen camera can not toggle the flash
			throw new RuntimeException("Can't turn the flash on !");
		} else {
			parameters.setFlashMode(state?Parameters.FLASH_MODE_TORCH:Parameters.FLASH_MODE_OFF);
			try {
				mCamera.setParameters(parameters);
				mFlashEnabled = state;
			} catch (RuntimeException e) {
				mFlashEnabled = false;
				throw new RuntimeException("Can't turn the flash on !");
			} finally {
				if (mStreaming && mMode == MODE_MEDIARECORDER_API) {
					unlockCamera();
				}
			}
		}
	} else {
		mFlashEnabled = state;
	}
}
 
Example 14
Source File: LegacyRequestMapper.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private static void mapAeAndFlashMode(CaptureRequest r, /*out*/Parameters p) {
    int flashMode = ParamsUtils.getOrDefault(r, FLASH_MODE, FLASH_MODE_OFF);
    int aeMode = ParamsUtils.getOrDefault(r, CONTROL_AE_MODE, CONTROL_AE_MODE_ON);

    List<String> supportedFlashModes = p.getSupportedFlashModes();

    String flashModeSetting = null;

    // Flash is OFF by default, on cameras that support flash
    if (ListUtils.listContains(supportedFlashModes, Parameters.FLASH_MODE_OFF)) {
        flashModeSetting = Parameters.FLASH_MODE_OFF;
    }

    /*
     * Map all of the control.aeMode* enums, but ignore AE_MODE_OFF since we never support it
     */

    // Ignore flash.mode controls unless aeMode == ON
    if (aeMode == CONTROL_AE_MODE_ON) {
        if (flashMode == FLASH_MODE_TORCH) {
                if (ListUtils.listContains(supportedFlashModes, Parameters.FLASH_MODE_TORCH)) {
                    flashModeSetting = Parameters.FLASH_MODE_TORCH;
                } else {
                    Log.w(TAG, "mapAeAndFlashMode - Ignore flash.mode == TORCH;" +
                            "camera does not support it");
                }
        } else if (flashMode == FLASH_MODE_SINGLE) {
            if (ListUtils.listContains(supportedFlashModes, Parameters.FLASH_MODE_ON)) {
                flashModeSetting = Parameters.FLASH_MODE_ON;
            } else {
                Log.w(TAG, "mapAeAndFlashMode - Ignore flash.mode == SINGLE;" +
                        "camera does not support it");
            }
        } else {
            // Use the default FLASH_MODE_OFF
        }
    } else if (aeMode == CONTROL_AE_MODE_ON_ALWAYS_FLASH) {
            if (ListUtils.listContains(supportedFlashModes, Parameters.FLASH_MODE_ON)) {
                flashModeSetting = Parameters.FLASH_MODE_ON;
            } else {
                Log.w(TAG, "mapAeAndFlashMode - Ignore control.aeMode == ON_ALWAYS_FLASH;" +
                        "camera does not support it");
            }
    } else if (aeMode == CONTROL_AE_MODE_ON_AUTO_FLASH) {
        if (ListUtils.listContains(supportedFlashModes, Parameters.FLASH_MODE_AUTO)) {
            flashModeSetting = Parameters.FLASH_MODE_AUTO;
        } else {
            Log.w(TAG, "mapAeAndFlashMode - Ignore control.aeMode == ON_AUTO_FLASH;" +
                    "camera does not support it");
        }
    } else if (aeMode == CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE) {
            if (ListUtils.listContains(supportedFlashModes, Parameters.FLASH_MODE_RED_EYE)) {
                flashModeSetting = Parameters.FLASH_MODE_RED_EYE;
            } else {
                Log.w(TAG, "mapAeAndFlashMode - Ignore control.aeMode == ON_AUTO_FLASH_REDEYE;"
                        + "camera does not support it");
            }
    } else {
        // Default to aeMode == ON, flash = OFF
    }

    if (flashModeSetting != null) {
        p.setFlashMode(flashModeSetting);
    }

    if (DEBUG) {
            Log.v(TAG,
                    "mapAeAndFlashMode - set flash.mode (api1) to " + flashModeSetting
                    + ", requested (api2) " + flashMode
                    + ", supported (api1) " + ListUtils.listToString(supportedFlashModes));
    }
}
 
Example 15
Source File: AndroidCameraAgentImpl.java    From Camera2 with Apache License 2.0 4 votes vote down vote up
private void applySettingsToParameters(final CameraSettings settings,
        final Parameters parameters) {
    final CameraCapabilities.Stringifier stringifier = mCapabilities.getStringifier();
    Size photoSize = settings.getCurrentPhotoSize();
    parameters.setPictureSize(photoSize.width(), photoSize.height());
    Size previewSize = settings.getCurrentPreviewSize();
    parameters.setPreviewSize(previewSize.width(), previewSize.height());
    if (settings.getPreviewFrameRate() == -1) {
        parameters.setPreviewFpsRange(settings.getPreviewFpsRangeMin(),
                settings.getPreviewFpsRangeMax());
    } else {
        parameters.setPreviewFrameRate(settings.getPreviewFrameRate());
    }
    parameters.setPreviewFormat(settings.getCurrentPreviewFormat());
    parameters.setJpegQuality(settings.getPhotoJpegCompressionQuality());
    if (mCapabilities.supports(CameraCapabilities.Feature.ZOOM)) {
        parameters.setZoom(zoomRatioToIndex(settings.getCurrentZoomRatio(),
                parameters.getZoomRatios()));
    }
    parameters.setExposureCompensation(settings.getExposureCompensationIndex());
    if (mCapabilities.supports(CameraCapabilities.Feature.AUTO_EXPOSURE_LOCK)) {
        parameters.setAutoExposureLock(settings.isAutoExposureLocked());
    }
    parameters.setFocusMode(stringifier.stringify(settings.getCurrentFocusMode()));
    if (mCapabilities.supports(CameraCapabilities.Feature.AUTO_WHITE_BALANCE_LOCK)) {
        parameters.setAutoWhiteBalanceLock(settings.isAutoWhiteBalanceLocked());
    }
    if (settings.getWhiteBalance() != null) {
        parameters.setWhiteBalance(stringifier.stringify(settings.getWhiteBalance()));
    }
    if (mCapabilities.supports(CameraCapabilities.Feature.FOCUS_AREA)) {
        if (settings.getFocusAreas().size() != 0) {
            parameters.setFocusAreas(settings.getFocusAreas());
        } else {
            parameters.setFocusAreas(null);
        }
    }
    if (mCapabilities.supports(CameraCapabilities.Feature.METERING_AREA)) {
        if (settings.getMeteringAreas().size() != 0) {
            parameters.setMeteringAreas(settings.getMeteringAreas());
        } else {
            parameters.setMeteringAreas(null);
        }
    }
    if (settings.getCurrentFlashMode() != CameraCapabilities.FlashMode.NO_FLASH) {
        parameters.setFlashMode(stringifier.stringify(settings.getCurrentFlashMode()));
    }
    if (settings.getCurrentSceneMode() != CameraCapabilities.SceneMode.NO_SCENE_MODE) {
        if (settings.getCurrentSceneMode() != null) {
            parameters
                    .setSceneMode(stringifier.stringify(settings.getCurrentSceneMode()));
        }
    }
    parameters.setRecordingHint(settings.isRecordingHintEnabled());
    Size jpegThumbSize = settings.getExifThumbnailSize();
    if (jpegThumbSize != null) {
        parameters.setJpegThumbnailSize(jpegThumbSize.width(), jpegThumbSize.height());
    }
    parameters.setPictureFormat(settings.getCurrentPhotoFormat());

    CameraSettings.GpsData gpsData = settings.getGpsData();
    if (gpsData == null) {
        parameters.removeGpsData();
    } else {
        parameters.setGpsTimestamp(gpsData.timeStamp);
        if (gpsData.processingMethod != null) {
            // It's a hack since we always use GPS time stamp but does
            // not use other fields sometimes. Setting processing
            // method to null means the other fields should not be used.
            parameters.setGpsAltitude(gpsData.altitude);
            parameters.setGpsLatitude(gpsData.latitude);
            parameters.setGpsLongitude(gpsData.longitude);
            parameters.setGpsProcessingMethod(gpsData.processingMethod);
        }
    }

}