camera startPreview failed

后端 未结 5 1171
猫巷女王i
猫巷女王i 2021-01-02 09:46

I am creating a camera app but I have problems on startPreview, it sends me:

java.lang.RuntimeException: startPreview failed

h

相关标签:
5条回答
  • 2021-01-02 09:59

    I had this error, and it's because I didn't call releaseCamera on the onPause the first time I start my app.
    After a reboot, everything works fine ^^

    0 讨论(0)
  • 2021-01-02 10:01
    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.getParameters().getSupportedPreviewSizes();
            mCamera.startPreview();
            Log.d(TAG, "Camera preview started.");
        } catch (IOException e) {
            Log.d(TAG, "Error setting camera preview: " + e.getMessage());
        }
    }
    
    0 讨论(0)
  • 2021-01-02 10:07

    Its late, but if someones looking for the answer

    The variables w and h are not the optimal preview sizes . You can get optimal preview sizes using the function

    public static Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
            final double ASPECT_TOLERANCE = 0.1;
            double targetRatio=(double)h / w;
            if (sizes == null) return null;
    
            Camera.Size optimalSize = null;
            double minDiff = Double.MAX_VALUE;
    
            int targetHeight = h;
    
            for (Camera.Size size : sizes) {
                double ratio = (double) size.width / size.height;
                if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
    
            if (optimalSize == null) {
                minDiff = Double.MAX_VALUE;
                for (Camera.Size size : sizes) {
                    if (Math.abs(size.height - targetHeight) < minDiff) {
                        optimalSize = size;
                        minDiff = Math.abs(size.height - targetHeight);
                    }
                }
            }
            return optimalSize;
        }
    

    and you can call the function using

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    ..
    size=getOptimalPreviewSize(parameters.getSupportedPreviewSizes(), w, h);
     parameters.setPreviewSize(size.getWidth(), size.getHeight());
    ..
    
    }
    
    0 讨论(0)
  • 2021-01-02 10:15

    Is (w, h) a valid preview size for your camera?

    You can use mCamera.getParameters().getSupportedPreviewSizes() to get all of the valid preview sizes.

    0 讨论(0)
  • 2021-01-02 10:17

    I have solved deleting some lines in surfaceChanged

     public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
            // If your preview can change or rotate, take care of those events here.
            // Make sure to stop the preview before resizing or reformatting it.
            Log.d("Function", "surfaceChanged iniciado");
            if (mHolder.getSurface() == null){
              // preview surface does not exist
              return;
            }
    
            // stop preview before making changes
            try {
                mCamera.stopPreview();
            } catch (Exception e){
              // ignore: tried to stop a non-existent preview
            }
    
            // set preview size and make any resize, rotate or
            // reformatting changes here
    
    
            // start preview with new settings
            try {
                mCamera.setPreviewDisplay(mHolder);
                mCamera.startPreview();
    
            } catch (Exception e){
                Log.d(TAG, "Error starting camera preview: " + e.getMessage());
            }
        }
    

    So the error must be in i one of these lines:

    Parameters parameters= mCamera.getParameters();
        parameters.setPreviewSize(w, h);
        mCamera.setParameters(parameters);
    

    Someone could explain me what was wrong in those lines?

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