RuntimeException on Camera.setParameters() on nexus one

前端 未结 7 1616
遥遥无期
遥遥无期 2020-12-13 02:45

I copied the code from the answer here and I still am getting a RuntimeException: setParameters failed error on my nexus one. My manifest file has camera and wake_lock permi

相关标签:
7条回答
  • 2020-12-13 03:00

    None of the above solved this for me. Adding this code before setting the parameters did though.

    // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e){
            // ignore: tried to stop a non-existent preview
        }
    
    //now set your parameters
    
    0 讨论(0)
  • 2020-12-13 03:05

    I corrected this by doing what Roman said, with the code:

       Camera.Parameters parameters = camera.getParameters();  
       List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();  
       Camera.Size cs = sizes.get(0);  
       parameters.setPreviewSize(cs.width, cs.height);  
       camera.setParameters(parameters);
    
    0 讨论(0)
  • 2020-12-13 03:05

    For what it's worth, the source of my issue ended up being that I was trying to call parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); without first verifying that flash modes were supported by checking that parameters.getFlashMode() != null.

    There's more than one cause for this poorly documented exception, so check all of your parameters and not just that you're using a supportedPreviewSize.

    0 讨论(0)
  • For me this would happen after taking a photo and the preview would freeze, until I updated my call for parameters to be the following. It is always important with this error to make sure you check all of the parameters that the camera is asking to set to make sure that every parameter you are asking the camera to set itself to is possible for the camera.

    Camera.Parameters parameters = myCamera.getParameters();
    

    With the preview size:

    if (myCamera.getParameters().getSupportedPreviewSizes() != null){
         Camera.Size previewSize = getOptimalPreviewSize(myCamera.getParameters().getSupportedPreviewSizes(), width, height);;
         parameters.setPreviewSize(previewSize.width, previewSize.height);
    }
    

    With the flash/focus modes:

    if(parameters.getSupportedFocusModes() != null && parameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)){
         parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
    }
    
    if (parameters.getSupportedFlashModes() != null && parameters.getSupportedFlashModes().contains(Camera.Parameters.FLASH_MODE_AUTO)){
        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
    
    }
    
    myCamera.setParameters(parameters);
    

    etc. All of this wrapped in a nice try{}catch(){} works great. Good luck.

    Here is the getOptimalPreview Size from this great tutorial:

    private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int width, int height)
        {
            // Source: http://stackoverflow.com/questions/7942378/android-camera-will-not-work-startpreview-fails
            Camera.Size optimalSize = null;
    
            final double ASPECT_TOLERANCE = 0.1;
            double targetRatio = (double) height / width;
    
            // Try to find a size match which suits the whole screen minus the menu on the left.
            for (Camera.Size size : sizes){
    
                if (size.height != width) continue;
                double ratio = (double) size.width / size.height;
                if (ratio <= targetRatio + ASPECT_TOLERANCE && ratio >= targetRatio - ASPECT_TOLERANCE){
                    optimalSize = size;
                }
            }
    
            // If we cannot find the one that matches the aspect ratio, ignore the requirement.
            if (optimalSize == null) {
                // TODO : Backup in case we don't get a size.
            }
    
            return optimalSize;
        }
    
    0 讨论(0)
  • 2020-12-13 03:07

    the solution from Sam is correct but the output image is still zoomed a little bit on several tablet devices. One of the best practices that I found on Internet, we should set in Camera host so that the properties will be re-used each time the camera is resumed. Here is implemented method in CameraHost:

    @Override
            public Parameters adjustPreviewParameters(Parameters parameters) {
                List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
                Camera.Size cs = sizes.get(0);
                parameters.setPreviewSize(cs.width, cs.height);
                return super.adjustPreviewParameters(parameters);
            }
    
    0 讨论(0)
  • 2020-12-13 03:08

    You're most likely requsting an invalid preview size. If you check the results of adb logcat you'll probably see something like this:

    E/QualcommCameraHardware(22732): Invalid preview size requested: 480x724
    

    The solution is to request the closest available preview size to the one you'd like; you can get a list of available preview sizes by calling getSupportedPreviewSizes in the Camera.Parameters object returned by Camera.getParameters.

    0 讨论(0)
提交回复
热议问题