如何通过加速度计检测到设备中的运动时调用自动对焦?

时间:2016-10-06 10:13:13

标签: android android-camera accelerometer autofocus

这是我现在正在使用的代码:

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ZoomControls;

import  android.hardware.Camera.AutoFocusCallback;

import java.io.IOException;
import java.util.List;


public class CameraActivity extends AppCompatActivity implements SurfaceHolder.Callback,SensorEventListener {

    Camera camera;
    Camera.Parameters params;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    int currentZoomLevel = 0, maxZoomLevel = 0;
    private ImageView flashButton;
    AutoFocusCallback mAutoFocusCallback;
    // Tracker tracker;
    private boolean isFlashOn;
    private boolean hasFlash;
    private ImageView drawerToggle;
    boolean flashOffFirst;
    private SensorManager sensorMan;
    private Sensor accelerometer;
    public boolean var;
    private float[] mGravity;
    private float mAccel;
    private float mAccelCurrent;
    private float mAccelLast;
    public int con = 0;




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        sensorMan = (SensorManager)getSystemService(SENSOR_SERVICE);
        accelerometer = sensorMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mAccel = 0.00f;
        mAccelCurrent = SensorManager.GRAVITY_EARTH;
        mAccelLast = SensorManager.GRAVITY_EARTH;
        //  var=safeCameraOpen(0);



    @Override
    public void onSensorChanged(SensorEvent event) {

        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            mGravity = event.values.clone();
            // Shake detection
            float x = mGravity[0];
            float y = mGravity[1];
            float z = mGravity[2];
            mAccelLast = mAccelCurrent;
            mAccelCurrent = (float) Math.sqrt(x * x + y * y + z * z);
            float delta = mAccelCurrent - mAccelLast;
            mAccel = mAccel * 0.9f + delta;
            // Make this higher or lower according to how much
            // motion you want to detect
            if (mAccel > 0.5 && params!=null) {
           /*    if (params.getSupportedFocusModes().contains(
                        Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {


                    params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
                }
                camera.setParameters(params);

                camera.autoFocus(new AutoFocusCallback() {
                    @Override
                    public void onAutoFocus(boolean success, Camera camera) {
                        if (success) {

                            Toast.makeText(getApplicationContext(), "AutoFocus on sensor changed", Toast.LENGTH_SHORT).show();//  camera.takePicture(shutterCallback, rawCallback, jpegCallback);
                        }
                    }
                });*/
            }
            if(con==1 && params!=null)
            {
               /*if (params.getSupportedFocusModes().contains(
                        Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {

                  //  params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
                    params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
                }
                camera.setParameters(params);*/

                camera.autoFocus(new AutoFocusCallback() {
                    @Override
                    public void onAutoFocus(boolean success, Camera camera) {
                        if (success) {

                            Toast.makeText(getApplicationContext(), "AutoFocus on accuracy changed", Toast.LENGTH_SHORT).show();//  camera.takePicture(shutterCallback, rawCallback, jpegCallback);
                        }
                    }
                });
            }
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        switch (accuracy) {
            case 0:
                System.out.println("Unreliable");
                con = 0;
                break;
            case 1:
                System.out.println("Low Accuracy");
                con = 0;
                break;
            case 2:
                System.out.println("Medium Accuracy");
                con = 0;

                break;
            case 3:
                System.out.println("High Accuracy");
                con = 1;
                break;
        }
    }
}

实际上传感器正在搞乱自动对焦,所以当我在SensorEventlistener的onSensorchanged方法中调用自动对焦时,onSurfaceChanged,onSurfaceCreate中的自动对焦会停止工作。任何人都可以建议任何解决方案或任何其他方式。

提前致谢。

0 个答案:

没有答案