android camera focal length nd focal distances do not change

别说谁变了你拦得住时间么 提交于 2019-12-10 23:57:06

问题


I am trying to detect a value that changes when the camera auto focuses, i expected the focal length and focal distances to change. I have ran my app with FOCUS_MODE_CONTINUOUS_PICTURE. the auto focus works, i called getFocalLength() every second to see the changes in values, i always got 4.6, no matter how much i move the camera. I also tried getFocusDistances() , it returns infinity for all 3 outputs all the time.

the values these functions return should change when the focus changes, but they don't, is there a reason for that ? and what other parameters can change when the focus changes ? because i want to detect whatever change that happens when the camera tries to auto focus.

here is the content of my activity:

private Camera mCamera;
private CameraPreview mPreview;
private ArrayList<Float> flengths = new ArrayList<Float>();
private ArrayList<String> times = new ArrayList<String>();
FileOutputStream outputStream;
Camera.Parameters params;
float[] distances = new float[3];
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (checkCameraHardware(this)==false)
        this.finish();
    else if((mCamera=getCameraInstance())==null)
        this.finish();
    else
    {

        mPreview = new CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mPreview);


       params = mCamera.getParameters();
        if (params.getMaxNumMeteringAreas() > 0){ // check that metering areas are supported
            List<Camera.Area> meteringAreas = new ArrayList<Camera.Area>();

            Rect areaRect1 = new Rect(-100, -100, 100, 100);    // specify an area in center of image
            meteringAreas.add(new Camera.Area(areaRect1, 600)); // set weight to 60%
            Rect areaRect2 = new Rect(800, -1000, 1000, -800);  // specify an area in upper right of image
            meteringAreas.add(new Camera.Area(areaRect2, 400)); // set weight to 40%
            params.setMeteringAreas(meteringAreas);
        }

        mCamera.setParameters(params);

        new Thread() {
            public void run() {
                while (true) {
                    runOnUiThread(new Runnable() {
                         @Override
                         public void run() {


                             mCamera.getParameters().getFocusDistances(distances);


                             Log.d("focus distance 0", Float.toString(distances[0]));
                            Log.d("focus distance 1", Float.toString(distances[1]));
                            Log.d("focus distance 2", Float.toString(distances[2]));


                         }
                    });
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } 
            }
        }.start();



    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


/** Check if this device has a camera */
private boolean checkCameraHardware(Context context) {
    if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
        // this device has a camera
        return true;
    } else {
        // no camera on this device
        return false;
    }
}




/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open(); // attempt to get a Camera instance
    }
    catch (Exception e){
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}

and the contents of my cameraPreviw:

private SurfaceHolder mHolder;
private Camera mCamera;

public CameraPreview(Context context, Camera camera) {
    super(context);
    mCamera = camera;

    // Install a SurfaceHolder.Callback so we get notified when the
    // underlying surface is created and destroyed.
    mHolder = getHolder();
    mHolder.addCallback(this);

    Log.d(TAG,"in camera preview constructor");

}

public void surfaceCreated(SurfaceHolder holder) {
    // The Surface has been created, now tell the camera where to draw the preview.
    try {

        Log.d(TAG,"in surface created");

        mCamera.setPreviewDisplay(holder);
        Camera.Parameters params = mCamera.getParameters();
        params.setFocusMode("continuous-picture");
        mCamera.setParameters(params);
        mCamera.startPreview();


    } catch (IOException e) {
        Log.d(TAG, "Error setting camera preview: " + e.getMessage());
    }
}

来源:https://stackoverflow.com/questions/21515214/android-camera-focal-length-nd-focal-distances-do-not-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!