从方法surfaceChanged() 调用Camera.setParameters() 时出现RuntimeException 错误。我看到了关于同一问题的其他帖子,但没有找到明确的解决方案:
Issue with setting the parameters to camera

我的问题是:

  • 为什么应该在surfaceCreated() 之后立即调用surfaceChanged()? (至少对我而言)在surfaceCreated()中启动预览,只是停止预览并在surfaceChanged()中重新启动预览是一种反常的操作。
  • 第一次调用surfaceChanged()时,传入的宽高由什么决定?正如上述帖子中所建议的,并在我的实验中进行了验证,Camera.setParameters() 不一定支持这些值。请参阅下面的代码 fragment 和日志信息。
  • 在调用surfaceChanged()之前,有没有办法保证Camera.setParameters()支持宽高值?可以在 surfaceChanged() 中添加代码来确保这一点。例如,可以将 w 和 h 与所有支持的预览尺寸进行比较,并更改为最接近的支持值。但是,这样做会影响性能。

  • 下面的列表是我的代码 fragment 和 LogCat 输出。日志记录信息表明surfaceChanged()中的Camera.setParameters()导致了错误。如果我注释掉调用 setParameters() 的行,我就看不到错误消息了。

    [ 编辑 :我正在 Amazon Kindle Fire HD 上进行测试。 Android版本是4.0.3,API 15]

    代码 fragment :
    public void surfaceCreated(SurfaceHolder holder) {
        // Log & debug
        Log.d(TAG, "surfaceCreated() is called");
    
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();
        } catch (IOException e) {
            Log.d(TAG, "surfaceCreated(): " + e.getMessage());
        }
    
        if (mHolder.getSurface() == null) {
            Log.d(TAG, "surfaceCreated(): preview surface does not exist yet!");
        }
    }
    
    public void surfaceDestroyed(SurfaceHolder holder) {
        // Log & debug
        Log.d(TAG, "surfaceDestroyed() is called");
    
        // Stop preview and release camera
        if (holder.getSurface() != null) {
            mCamera.stopPreview();
            //mCamera.release();  //should we release camera here?
            //mCamera = null;
        }
        else {
            Log.d(TAG, "surfaceDestroyed(): preview surface does not exist");
        }
    }
    
    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // Log and debug
        Log.d(TAG, "surfaceChanged() is called with width = " + w);
        Log.d(TAG, "surfaceChanged() is called with height = " + 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.
        if (holder.getSurface() == null) {
          Log.d(TAG, "surfaceChanged(): preview surface does not exist");
          return;
        }
    
        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e) {
            Log.d(TAG, "surfaceChanged(): tried to stop a non-existent preview " + e.getMessage());
        }
    
        // set preview size and make any resize, rotate or
        // reformatting changes here
        Camera.Parameters params = mCamera.getParameters();
        List<Size> mSizes = params.getSupportedPreviewSizes();
    
        // list all 25 supported preview sizes
        // i==25 will cause an "IndexOutofBoundsException"
        for (int i=0; i<25; i++) {
            Size mSize  = mSizes.get(i);
            if (mSize == null) break;
            Log.d(TAG, "surfaceChanged() i = " + i);
            Log.d(TAG, "surfaceChanged() preview width = " + mSize.width);
            Log.d(TAG, "surfaceChanged() preview height = " + mSize.height);
        }
        Size       mSize  = mSizes.get(0);
        if(mSize != null) { //make sure we don't pull a NullException.
            if (w > mSize.width) w  = mSize.width;
            if (h > mSize.height) h = mSize.height;
            params.setPreviewSize(w, h); //set the size, since we know it.
        }
        mCamera.setParameters(params); //set the parameters now.
    
        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (Exception e){
            Log.d(TAG, "surfaceChanged() restart preview " + e.getMessage());
        }
    }
    

    LogCat 输出:
    02-13 10:46:08.113: D/CameraPreview(2886): surfaceCreated() is called
    02-13 10:46:08.496: D/CameraPreview(2886): surfaceChanged() is called with width = 1072
    02-13 10:46:08.496: D/CameraPreview(2886): surfaceChanged() is called with height = 705
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() i = 0
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview width = 1280
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview height = 720
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() i = 1
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview width = 960
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview height = 720
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() i = 2
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview width = 800
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview height = 480
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() i = 3
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview width = 720
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview height = 576
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() i = 4
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview width = 720
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview height = 480
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() i = 5
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview width = 768
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview height = 576
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() i = 6
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview width = 640
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview height = 480
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() i = 7
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview width = 320
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview height = 240
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() i = 8
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview width = 352
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview height = 288
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() i = 9
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview width = 240
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview height = 160
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() i = 10
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview width = 176
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview height = 144
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() i = 11
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview width = 160
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview height = 120
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() i = 12
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview width = 128
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview height = 96
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() i = 13
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview width = 720
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview height = 1280
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() i = 14
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview width = 480
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview height = 800
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() i = 15
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview width = 576
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview height = 720
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() i = 16
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview width = 576
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview height = 768
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() i = 17
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview width = 480
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview height = 720
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() i = 18
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview width = 480
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview height = 640
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() i = 19
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview width = 288
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview height = 352
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() i = 20
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview width = 240
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview height = 320
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() i = 21
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview width = 240
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview height = 160
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() i = 22
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview width = 144
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview height = 176
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() i = 23
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview width = 120
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview height = 160
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() i = 24
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview width = 96
    02-13 10:46:08.941: D/CameraPreview(2886): surfaceChanged() preview height = 128
    02-13 10:46:08.957: D/AndroidRuntime(2886): Shutting down VM
    02-13 10:46:08.957: W/dalvikvm(2886): threadid=1: thread exiting with uncaught exception (group=0x40a6c1f8)
    02-13 10:46:08.965: E/AndroidRuntime(2886): FATAL EXCEPTION: main
    02-13 10:46:08.965: E/AndroidRuntime(2886): java.lang.RuntimeException: setParameters failed
    02-13 10:46:08.965: E/AndroidRuntime(2886):     at android.hardware.Camera.native_setParameters(Native Method)
    02-13 10:46:08.965: E/AndroidRuntime(2886):     at android.hardware.Camera.setParameters(Camera.java:1295)
    02-13 10:46:08.965: E/AndroidRuntime(2886):     at com.xyz.camera.CameraPreview.surfaceChanged(CameraPreview.java:109)
    02-13 10:46:08.965: E/AndroidRuntime(2886):     at android.view.SurfaceView.updateWindow(SurfaceView.java:544)
    02-13 10:46:08.965: E/AndroidRuntime(2886):     at android.view.SurfaceView.access$000(SurfaceView.java:81)
    02-13 10:46:08.965: E/AndroidRuntime(2886):     at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:169)
    02-13 10:46:08.965: E/AndroidRuntime(2886):     at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:590)
    02-13 10:46:08.965: E/AndroidRuntime(2886):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1630)
    02-13 10:46:08.965: E/AndroidRuntime(2886):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2462)
    02-13 10:46:08.965: E/AndroidRuntime(2886):     at android.os.Handler.dispatchMessage(Handler.java:99)
    02-13 10:46:08.965: E/AndroidRuntime(2886):     at android.os.Looper.loop(Looper.java:137)
    02-13 10:46:08.965: E/AndroidRuntime(2886):     at android.app.ActivityThread.main(ActivityThread.java:4486)
    02-13 10:46:08.965: E/AndroidRuntime(2886):     at java.lang.reflect.Method.invokeNative(Native Method)
    02-13 10:46:08.965: E/AndroidRuntime(2886):     at java.lang.reflect.Method.invoke(Method.java:511)
    02-13 10:46:08.965: E/AndroidRuntime(2886):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    02-13 10:46:08.965: E/AndroidRuntime(2886):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    02-13 10:46:08.965: E/AndroidRuntime(2886):     at dalvik.system.NativeStart.main(Native Method)
    

    最佳答案

    好吧,您可能会遇到的问题之一是,由于您使用的是 api 15,那么您应该使用 getSupportedVideoSizes(),因为它来自 api 11。我在某些设备上设置参数时遇到了类似的问题(那些正在运行的设备)蜂窝及以上),因为预览大小和支持的视频大小并不总是相同的。

    编辑::关于您的评论:
    我会为您的测试设备输出所有支持的预览尺寸。你已经有了。然后我会拿一个(例如:你得到的第二个)并静态设置它以用于测试目的

    例如:

    for int (i = 0; i < mSizes.size(); i ++)
        {
            Log each size;
        }
    

    从中取一个随机大小:例如 480 x 800 并设置它
        params.setPreviewSize(480, 800);
    

    这将告诉您是否设置预览尺寸使显示正确的尺寸。

    编辑2:
    我会再次更新我的答案以澄清我的评论

    里面 onSurfaceChanged
    有两个变量
    int width = 0;
    int height = 0;
    

    然后在你的 if 语句中
    if (w > mSize.width) width = mSize.width;
    if (h > mSize.height) height = mSize.height;
    

    另外你正在做的是得到相当于

    mSizes.get(mSize.size()-1)。因为你 w 和 h 总是会大于 mSize.width 和 mSize.height

    编辑 3:
    好的,根据您的意见。
    你想在 onSufaceCreated() 中初始化相机
    并将 setPreviewSize(w,h) 更改为 setPreviewSize(mSizes.get(mSizes.size() -1).width, mSizes.get(mSizes.size() -1).height);

    然后添加一个将SurfaceView设置为所需大小的函数()
    surfaceParams = surfaceView.getLayoutParameters();
    surfaceParams.setWidth(somewidthyouwant);
    surfaceParams.setHeight(someHeightYouWant);
    surfaceView.setLayoutParams(surfaceParams);
    

    这会将您的表面 View 设置为您想要的任何大小。

    关于android - setParameters() 不支持在surfaceChanged() 中传递的宽度和高度?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14861576/

    10-11 15:05