android相机焦距和焦距不变

时间:2014-02-02 19:35:30

标签: android parameters camera autofocus

我正在尝试检测相机自动对焦时发生变化的值,我预计焦距和焦距会发生变化。我用FOCUS_MODE_CONTINUOUS_PICTURE运行了我的应用程序。自动对焦工作,我每秒都调用getFocalLength()来查看值的变化,无论我移动相机多少,我总是得到4.6。我也试过getFocusDistances(),它一直为所有3个输出返回无穷大。

当焦点改变时,这些函数返回的值应该改变,但它们没有,是否有原因?焦点变化时可以改变哪些其他参数?因为我想检测相机试图自动对焦时发生的任何变化。

这是我活动的内容:

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
}

以及我的相机内容:

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

0 个答案:

没有答案