简体   繁体   中英

Light sensor on different devices

I tried to create a small application that calculates the light through the light sensor, but different devices give me completely different values. I used the max value to calculate a percentage, but my xperia and my samsung are in the same place (near the wall outside my house, under the roof) and the first device gives me 90% (90k/102k) and the second one gives me 12% (8k/64k). Is there a way to avoid this problem?

    public final void onSensorChanged(SensorEvent event) 
    {
        if( event.sensor.getType() == Sensor.TYPE_LIGHT)
        {
            currentLux=(int) event.values[0];
            float perc=(currentLux*100)/max;
            lightTextBox.setText(lightTextBox.getText()+String.valueOf(perc)+" * ");    
        }   
    }

Since every sensor have different values for the same light level, so what I did is to record the minimum and the maximum values that will be captured during the application lifetime, and I stored these values as a Min and Max , after a few days of using the application, all the devices will have almost the same value that I can reliable on. as the initial state, I considered the min value is 1000 and the max value is 0 . (I stored it in the SharedPreferences). then I will update these values by considering the current Lux value of the sensor. and then I will calculate the percentage of the lighting. this is the code that I wrote:

float luxValue = o.getAsFloat("double_light_lux");

//here I update the value in sharedPreferences
updateMaxAndMinValue(context, luxValue);

float percentageValue = scale(LightMetadata.getMinValue(context),
                LightMetadata.getMaxValue(context),
                0,
                100,
                luxValue);

the updating function:

public static void updateMaxAndMinValue(Context context, float luxValue) {
    float currentMax = LightMetadata.getMaxValue(context); //0 as init.
    float currentMin = LightMetadata.getMinValue(context); //1000 as init.

    if (luxValue > currentMax)
        LightMetadata.setMaxValue(context, luxValue);
    if (luxValue < currentMin)
        LightMetadata.setMinValue(context, luxValue);
}

and I calculate the percentage by scaling the value from min -> max to 0 -> 100 this is the function:

/**
 * @param min   old min value
 * @param max   old max value
 * @param a     new min value
 * @param b     new max value
 * @param value the value that want to scale
 * @return new scaled value
 */
public static float scale(float min, float max, float a, float b, float value) {

    //       (b-a)(x - min)
    //f(x) = --------------  + a
    //          max - min

    float scaled;
    float numerator = (b - a) * (value - min);
    float denominator = max - min;

    scaled = (numerator / denominator) + a;
    return scaled;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM