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
Morgan Courbet
Nikolaos Ftylitakis
Nikolaus Huber
Olli
Pablo Orduña (University of Deusto)
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 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);

View file

@ -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);

View file

@ -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();
}
}
}
}