Camera zoom not working

2019-01-23 10:11发布

问题:

I am using the API 2.1 and my debug shows a max zoom value of 15. The code here does not make the camera zoom. How do I get the camera to zoom?

camera = Camera.open();
Camera.Parameters parameters = camera.getParameters();
parameters.set("orientation", "portrait");
parameters.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_TWILIGHT);
int zoom = parameters.getMaxZoom();
Log.d(TAG, "Zoom " + zoom);
parameters.setZoom(15);
camera.setParameters(parameters);

回答1:

This functionality depends on your Android version. The method parameters.getMaxZoom() has been appeared since: API Level 8 (Android 2.2), so if you use Android 2.1 this code won't work. And another thing. You should use your max zoom something like that parameters.setZoom(zoom); instead using constant parameters.setZoom(15);

Also you need to check that user's device support zooming. You can do it like this (only for Android 2.2 and higher)

   Camera.Parameters parameters = camera.getParameters();
   int maxZoom = parameters.getMaxZoom(); 
   if (parameters.isZoomSupported()) {
      if (zoom >=0 && zoom < maxZoom) {
        parameters.setZoom(zoom);
      } else {
        // zoom parameter is incorrect
      }
   }

But as I know there are no standard API for setting zoom in Android 2.1 I am still investigating this question.



回答2:

Your samples about zooming is work, thanks a lot.

This is my code :

zooming = (ZoomControls) findViewById(R.id.zooming);
zooming.setOnZoomInClickListener(new OnClickListener() {
    public void onClick(View v) {
        Camera.Parameters p = mCamera.getParameters();
        int maxZoom = p.getMaxZoom();

        if (p.isZoomSupported()) {
           zoom += 10;
           if (zoom > maxZoom) {
               zoom -= 10;
           }
           p.setZoom(zoom);
        }

        mCamera.setParameters(p);

        try {
            mCamera.setPreviewDisplay(mSurfaceHolder);
        } catch (Exception e) { }

         mCamera.startPreview();
    }
});

I hope your application is working too.



回答3:

if you want to used seekbar to change camera preview zoom in-out used following code its perfectly running :

    seekbarZoom = (SeekBar) findViewById(R.id.seekBarZoom);
    seekbarZoom.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            // TODO Auto-generated method stub
            Log.d(TAG, "progress:"+progress);

            if(mCamera.getParameters().isZoomSupported()){

                Parameters params = mCamera.getParameters();
                params.setZoom(progress);
                mCamera.setParameters(params);
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
            Log.d(TAG, "onStartTrackingTouch");
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
            Log.d(TAG, "onStartTrackingTouch");
        }

    });


回答4:

In case you are trying to set the parameters while the preview is on, then you must first stop the preview, set the parameters and then re-start the preview. This was the issue which was faced by my prior to 4.1, and also mentioned in the Android Official Website



回答5:

            zoomIncrementer = parameters.getMaxZoom() / 2;
            zoomInOut.setOnZoomInClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    zoomIncrementer += parameters.getMaxZoom() / 5;

                    if (zoomIncrementer > parameters.getMaxZoom()) {

                        zoomIncrementer = parameters.getMaxZoom();

                        Log.d(TAG, String.valueOf(zoomIncrementer));

                    }

                    cameraParameters();
                    cameraReloader();

                }
            });

            zoomInOut.setOnZoomOutClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    zoomIncrementer -= parameters.getMaxZoom() / 5;

                    if(zoomIncrementer < 0){

                        zoomIncrementer = 0;

                    }

                    cameraParameters();
                    cameraReloader();

                }
            });

void cameraReloader() {

    try {


      camera.stopPreview();

    } catch (Exception e) {

    }

    try {

        camera.setPreviewDisplay(cameraHolder);
        camera.startPreview();

    } catch (Exception e) {


    }

}

void cameraParameters() {

    Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
    parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
    parameters.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
    parameters.setZoom(zoomIncrementer);
    parameters.setFocusMode(Parameters.FOCUS_MODE_MACRO);
    parameters.setWhiteBalance(Parameters.WHITE_BALANCE_AUTO);


    Log.d(TAG, String.valueOf(parameters.getMaxZoom()));


    if (display.getRotation() == Surface.ROTATION_0) {

        camera.setDisplayOrientation(90);

    }

    if (display.getRotation() == Surface.ROTATION_270) {

        camera.setDisplayOrientation(180);

    }

    camera.setParameters(parameters);

}