• Android根据加速度和地磁场传感器实现自动对焦


    在相机预览开始后新建AutoFocusManage对象即可,传入context和camera。

    注意,在停止预览或者关闭相机时需调用方法中unregisterListener方法。

    目前实现是当前方向偏离原方向8°后进行自动对焦(并无根据,可自行调整)。

    AutoFocusManage.class

    package com.skytech.demo.camera;
    
    import android.content.Context;
    import android.hardware.Camera;
    import android.hardware.Sensor;
    import android.hardware.SensorEvent;
    import android.hardware.SensorEventListener;
    import android.hardware.SensorManager;
    
    public class AutoFocusManage {
    
        private SensorManager sensorManager;
        private Camera camera;
        private float[] accelerometerValues;
        private float[] magneticFieldValues;
        private float[] orientationValues = new float[3];
        private SensorEventListener accelerometerSensorEventListener = new SensorEventListener() {
            @Override
            public void onSensorChanged(SensorEvent event) {
                accelerometerValues = event.values;
                if (magneticFieldValues != null) {
                    calculateOrientation();
                }
            }
    
            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
            }
        };
        private SensorEventListener magneticFieldSensorEventListener = new SensorEventListener() {
            @Override
            public void onSensorChanged(SensorEvent event) {
                magneticFieldValues = event.values;
                if (accelerometerValues != null) {
                    calculateOrientation();
                }
            }
    
            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
            }
        };
    
        public AutoFocusManage(Context context, Camera camera) {
            this.camera = camera;
            sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
            // 初始化加速度传感器
            Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            // 初始化地磁场传感器
            Sensor magneticField = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
            sensorManager.registerListener(accelerometerSensorEventListener, accelerometer, SensorManager.SENSOR_DELAY_UI);
            sensorManager.registerListener(magneticFieldSensorEventListener, magneticField, SensorManager.SENSOR_DELAY_UI);
        }
    
        public void unregisterListener() {
            sensorManager.unregisterListener(accelerometerSensorEventListener);
            sensorManager.unregisterListener(magneticFieldSensorEventListener);
        }
    
        /**
         * 根据加速度和地磁场计算方向
         */
        private synchronized void calculateOrientation() {
            float[] values = new float[3];
            float[] R = new float[9];
            SensorManager.getRotationMatrix(R, null, accelerometerValues, magneticFieldValues);
            SensorManager.getOrientation(R, values);
            float[] orientation = new float[3];
            orientation[0] = (float) Math.toDegrees(values[0]);
            orientation[1] = (float) Math.toDegrees(values[1]);
            orientation[2] = (float) Math.toDegrees(values[2]);
            if (Math.abs(orientation[0] - orientationValues[0]) >= 8 || Math.abs(orientation[1] - orientationValues[1]) >= 8 || Math.abs(orientation[2] - orientationValues[2]) >= 8) {
                orientationValues = orientation;
                camera.autoFocus(new Camera.AutoFocusCallback() {
                    @Override
                    public void onAutoFocus(boolean success, Camera camera) {
                    }
                });
            }
        }
    
    }
  • 相关阅读:
    UVA 10827 Maximum sum on a torus 最大矩阵和
    UVA 11100 The Trip, 2007 水题一枚
    编程之美2015测试赛 题目1 : 同构
    UVA 10801 Lift Hopping Floyd
    UVA 11389 The Bus Driver Problem 贪心水题
    UVA 11039 Building designing 贪心
    UVA 11636 Hello World! 水题
    poj 3070 Fibonacci 矩阵快速幂
    hdu 1757 A Simple Math Problem 矩阵快速幂
    了解常见的 Azure 灾难
  • 原文地址:https://www.cnblogs.com/hackyo/p/11773042.html
Copyright © 2020-2023  润新知