From 462c94e7a2ead135f14c684da314ac5c71c3f211 Mon Sep 17 00:00:00 2001 From: srowen Date: Thu, 8 Nov 2012 12:57:55 +0000 Subject: [PATCH] Infrastructure for automated torch on based on ambient light (not turned on yet) git-svn-id: https://zxing.googlecode.com/svn/trunk@2504 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- AUTHORS | 1 + .../client/android/AmbientLightManager.java | 78 +++++++++++++++++++ .../zxing/client/android/CaptureActivity.java | 4 + .../camera/CameraConfigurationManager.java | 5 ++ .../client/android/camera/CameraManager.java | 16 ++-- 5 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 android/src/com/google/zxing/client/android/AmbientLightManager.java diff --git a/AUTHORS b/AUTHORS index f95433c08..0302c8eef 100644 --- a/AUTHORS +++ b/AUTHORS @@ -68,6 +68,7 @@ mikej06 Mohamad Fairol Morgan Courbet Nikolaos Ftylitakis +Nikolaus Huber Olli Pablo Orduña (University of Deusto) Paul Hackenberger diff --git a/android/src/com/google/zxing/client/android/AmbientLightManager.java b/android/src/com/google/zxing/client/android/AmbientLightManager.java new file mode 100644 index 000000000..2d193b3ad --- /dev/null +++ b/android/src/com/google/zxing/client/android/AmbientLightManager.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2012 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.zxing.client.android; + +import android.content.Context; +import android.hardware.Sensor; +import android.hardware.SensorEvent; +import android.hardware.SensorEventListener; +import android.hardware.SensorManager; +import com.google.zxing.client.android.camera.CameraManager; + +/** + * Detects ambient light and switches on the front light when very dark, and off again when sufficiently light. + * + * @author Sean Owen + * @author Nikolaus Huber + */ +final class AmbientLightManager implements SensorEventListener { + + private static final float TOO_DARK_LUX = 90.0f; + private static final float BRIGHT_ENOUGH_LUX = 225.0f; + + private final Context context; + private CameraManager cameraManager; + private Sensor lightSensor; + + AmbientLightManager(Context context) { + this.context = context; + } + + void start(CameraManager cameraManager) { + SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); + lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); + if (lightSensor != null) { + sensorManager.registerListener(this, lightSensor, SensorManager.SENSOR_DELAY_NORMAL); + this.cameraManager = cameraManager; + } + } + + void stop() { + if (lightSensor != null) { + SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); + sensorManager.unregisterListener(this); + cameraManager = null; + lightSensor = null; + } + } + + @Override + public void onSensorChanged(SensorEvent sensorEvent) { + float ambientLightLux = sensorEvent.values[0]; + if (ambientLightLux <= TOO_DARK_LUX) { + cameraManager.setTorch(true); + } else if (ambientLightLux >= BRIGHT_ENOUGH_LUX) { + cameraManager.setTorch(false); + } + } + + @Override + public void onAccuracyChanged(Sensor sensor, int accuracy) { + // do nothing + } + +} diff --git a/android/src/com/google/zxing/client/android/CaptureActivity.java b/android/src/com/google/zxing/client/android/CaptureActivity.java index 8f68442b6..fe4f179c3 100755 --- a/android/src/com/google/zxing/client/android/CaptureActivity.java +++ b/android/src/com/google/zxing/client/android/CaptureActivity.java @@ -121,6 +121,7 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal private HistoryManager historyManager; private InactivityTimer inactivityTimer; private BeepManager beepManager; + private AmbientLightManager ambientLightManager; ViewfinderView getViewfinderView() { return viewfinderView; @@ -147,6 +148,7 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal historyManager.trimHistory(); inactivityTimer = new InactivityTimer(this); beepManager = new BeepManager(this); + ambientLightManager = new AmbientLightManager(this); PreferenceManager.setDefaultValues(this, R.xml.preferences, false); @@ -187,6 +189,7 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal } beepManager.updatePrefs(); + // ambientLightManager.start(cameraManager); inactivityTimer.onResume(); @@ -270,6 +273,7 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal handler = null; } inactivityTimer.onPause(); + // ambientLightManager.stop(); cameraManager.closeDriver(); if (!hasSurface) { SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view); diff --git a/android/src/com/google/zxing/client/android/camera/CameraConfigurationManager.java b/android/src/com/google/zxing/client/android/camera/CameraConfigurationManager.java index fe0a7d9ad..637254b37 100644 --- a/android/src/com/google/zxing/client/android/camera/CameraConfigurationManager.java +++ b/android/src/com/google/zxing/client/android/camera/CameraConfigurationManager.java @@ -130,6 +130,11 @@ final class CameraConfigurationManager { return screenResolution; } + boolean getTorchSetting() { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + return prefs.getBoolean(PreferencesActivity.KEY_FRONT_LIGHT, false); + } + void setTorch(Camera camera, boolean newSetting) { Camera.Parameters parameters = camera.getParameters(); doSetTorch(parameters, newSetting, false); diff --git a/android/src/com/google/zxing/client/android/camera/CameraManager.java b/android/src/com/google/zxing/client/android/camera/CameraManager.java index 005d80789..3e8068887 100755 --- a/android/src/com/google/zxing/client/android/camera/CameraManager.java +++ b/android/src/com/google/zxing/client/android/camera/CameraManager.java @@ -166,13 +166,15 @@ public final class CameraManager { * Convenience method for {@link com.google.zxing.client.android.CaptureActivity} */ public synchronized void setTorch(boolean newSetting) { - if (camera != null) { - if (autoFocusManager != null) { - autoFocusManager.stop(); - } - configManager.setTorch(camera, newSetting); - if (autoFocusManager != null) { - autoFocusManager.start(); + if (newSetting != configManager.getTorchSetting()) { + if (camera != null) { + if (autoFocusManager != null) { + autoFocusManager.stop(); + } + configManager.setTorch(camera, newSetting); + if (autoFocusManager != null) { + autoFocusManager.start(); + } } } }