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
This commit is contained in:
srowen 2012-11-08 12:57:55 +00:00
parent fc867ccf73
commit 462c94e7a2
5 changed files with 97 additions and 7 deletions

View file

@ -68,6 +68,7 @@ mikej06
Mohamad Fairol Mohamad Fairol
Morgan Courbet Morgan Courbet
Nikolaos Ftylitakis Nikolaos Ftylitakis
Nikolaus Huber
Olli Olli
Pablo Orduña (University of Deusto) Pablo Orduña (University of Deusto)
Paul Hackenberger Paul Hackenberger

View file

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

View file

@ -121,6 +121,7 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
private HistoryManager historyManager; private HistoryManager historyManager;
private InactivityTimer inactivityTimer; private InactivityTimer inactivityTimer;
private BeepManager beepManager; private BeepManager beepManager;
private AmbientLightManager ambientLightManager;
ViewfinderView getViewfinderView() { ViewfinderView getViewfinderView() {
return viewfinderView; return viewfinderView;
@ -147,6 +148,7 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
historyManager.trimHistory(); historyManager.trimHistory();
inactivityTimer = new InactivityTimer(this); inactivityTimer = new InactivityTimer(this);
beepManager = new BeepManager(this); beepManager = new BeepManager(this);
ambientLightManager = new AmbientLightManager(this);
PreferenceManager.setDefaultValues(this, R.xml.preferences, false); PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
@ -187,6 +189,7 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
} }
beepManager.updatePrefs(); beepManager.updatePrefs();
// ambientLightManager.start(cameraManager);
inactivityTimer.onResume(); inactivityTimer.onResume();
@ -270,6 +273,7 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
handler = null; handler = null;
} }
inactivityTimer.onPause(); inactivityTimer.onPause();
// ambientLightManager.stop();
cameraManager.closeDriver(); cameraManager.closeDriver();
if (!hasSurface) { if (!hasSurface) {
SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view); SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);

View file

@ -130,6 +130,11 @@ final class CameraConfigurationManager {
return screenResolution; return screenResolution;
} }
boolean getTorchSetting() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
return prefs.getBoolean(PreferencesActivity.KEY_FRONT_LIGHT, false);
}
void setTorch(Camera camera, boolean newSetting) { void setTorch(Camera camera, boolean newSetting) {
Camera.Parameters parameters = camera.getParameters(); Camera.Parameters parameters = camera.getParameters();
doSetTorch(parameters, newSetting, false); doSetTorch(parameters, newSetting, false);

View file

@ -166,13 +166,15 @@ public final class CameraManager {
* Convenience method for {@link com.google.zxing.client.android.CaptureActivity} * Convenience method for {@link com.google.zxing.client.android.CaptureActivity}
*/ */
public synchronized void setTorch(boolean newSetting) { public synchronized void setTorch(boolean newSetting) {
if (camera != null) { if (newSetting != configManager.getTorchSetting()) {
if (autoFocusManager != null) { if (camera != null) {
autoFocusManager.stop(); if (autoFocusManager != null) {
} autoFocusManager.stop();
configManager.setTorch(camera, newSetting); }
if (autoFocusManager != null) { configManager.setTorch(camera, newSetting);
autoFocusManager.start(); if (autoFocusManager != null) {
autoFocusManager.start();
}
} }
} }
} }