相机 java.lang.RuntimeException:setParameters 失败

Posted

技术标签:

【中文标题】相机 java.lang.RuntimeException:setParameters 失败【英文标题】:Camera java.lang.RuntimeException: setParameters failed 【发布时间】:2016-02-08 08:59:50 【问题描述】:

我使用this 源代码创建了一个自定义相机应用程序,但在少数设备上(如高分辨率设备)我得到:

RuntimeException setParameters failed

我正面临这个异常,原因是:

params.setPictureSize(1200, 900);

我注意到,如果我使用 (1600, 1200) 而不是 (1200, 900),那么我就不会遇到此类问题

Logcat:

11-07 11:45:20.630: E/androidRuntime(3827): FATAL EXCEPTION: main
11-07 11:45:20.630: E/AndroidRuntime(3827): java.lang.RuntimeException: Unable to resume activity pl.gatti.dgcam/pl.gatti.dgcam.DgCamActivity: java.lang.RuntimeException: setParameters failed
11-07 11:45:20.630: E/AndroidRuntime(3827):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2825)
11-07 11:45:20.630: E/AndroidRuntime(3827):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2854)
11-07 11:45:20.630: E/AndroidRuntime(3827):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2318)
11-07 11:45:20.630: E/AndroidRuntime(3827):     at android.app.ActivityThread.access$600(ActivityThread.java:144)
11-07 11:45:20.630: E/AndroidRuntime(3827):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
11-07 11:45:20.630: E/AndroidRuntime(3827):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-07 11:45:20.630: E/AndroidRuntime(3827):     at android.os.Looper.loop(Looper.java:152)
11-07 11:45:20.630: E/AndroidRuntime(3827):     at android.app.ActivityThread.main(ActivityThread.java:5132)
11-07 11:45:20.630: E/AndroidRuntime(3827):     at java.lang.reflect.Method.invokeNative(Native Method)
11-07 11:45:20.630: E/AndroidRuntime(3827):     at java.lang.reflect.Method.invoke(Method.java:511)
11-07 11:45:20.630: E/AndroidRuntime(3827):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-07 11:45:20.630: E/AndroidRuntime(3827):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-07 11:45:20.630: E/AndroidRuntime(3827):     at dalvik.system.NativeStart.main(Native Method)
11-07 11:45:20.630: E/AndroidRuntime(3827): Caused by: java.lang.RuntimeException: setParameters failed
11-07 11:45:20.630: E/AndroidRuntime(3827):     at android.hardware.Camera.native_setParameters(Native Method)
11-07 11:45:20.630: E/AndroidRuntime(3827):     at android.hardware.Camera.setParameters(Camera.java:1490)
11-07 11:45:20.630: E/AndroidRuntime(3827):     at pl.gatti.dgcam.DgCamActivity.createCamera(DgCamActivity.java:124)
11-07 11:45:20.630: E/AndroidRuntime(3827):     at pl.gatti.dgcam.DgCamActivity.onResume(DgCamActivity.java:163)
11-07 11:45:20.630: E/AndroidRuntime(3827):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1185)
11-07 11:45:20.630: E/AndroidRuntime(3827):     at android.app.Activity.performResume(Activity.java:5182)
11-07 11:45:20.630: E/AndroidRuntime(3827):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2815)

代码:

private void createCamera() 
        // Create an instance of Camera
        mCamera = getCameraInstance();

        // Setting the right parameters in the camera
        Camera.Parameters params = mCamera.getParameters();
        params.setPictureSize(1200, 900);
        params.setPictureFormat(PixelFormat.JPEG);
        params.setJpegQuality(85);
        mCamera.setParameters(params);

        // Create our Preview view and set it as the content of our activity.
        mPreview = new CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);

        // Calculating the width of the preview so it is proportional.
        float widthFloat = (float) (deviceHeight) * 4 / 3;
        int width = Math.round(widthFloat);

        // Resizing the LinearLayout so we can make a proportional preview. This
        // approach is not 100% perfect because on devices with a really small
        // screen the the image will still be distorted - there is place for
        // improvment.
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, deviceHeight);
        preview.setLayoutParams(layoutParams);

        // Adding the camera preview after the FrameLayout and before the button
        // as a separated element.
        preview.addView(mPreview, 0);
    

您可能会看到complete CameraActivity 类代码here

这是我的CameraPreview 课程:

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback 
    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);
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        mHolder.setFixedSize(100, 100);
    

    public void surfaceCreated(SurfaceHolder holder) 
        // The Surface has been created, now tell the camera where to draw the
        // preview.
        try 
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
         catch (IOException e) 
            Log.d("DG_DEBUG", "Error setting camera preview: " + e.getMessage());
        

    

    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 
        // 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 (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
        

        // make any resize, rotate or reformatting changes here

        // start preview with new settings
        try 
            mCamera.setPreviewDisplay(mHolder);
            mCamera.setDisplayOrientation(90); // Portrait only
            mCamera.startPreview();

         catch (Exception e) 
            Log.d("DG_DEBUG", "Error starting camera preview: " + e.getMessage());
        
    

    public void surfaceDestroyed(SurfaceHolder holder) 
        // empty. Take care of releasing the Camera preview in your activity.
    


【问题讨论】:

这是行号124 ? 请定义第 124 行 @ρяσѕρєяK 第124行是mCamera.setParameters(params); 【参考方案1】:

任何相机应用都受到该设备支持的相机 HAL 的限制。所以在Camera HAL中我们定义我们将提供一个支持大小的列表,这些大小可以是预览大小、图片大小或视频大小。 所以我认为您遇到了这个问题,因为相机 HAL 或更低级别的代码不支持 1200*900。

【讨论】:

没错,我同意你的看法...感谢您提供的信息,但我该如何控制它? 其实你不能......你能做的是,获取支持的大小并相应地编写你的代码。【参考方案2】:

问题是由以下原因引起的:

params.setPictureSize(1200, 900);

因为相机不支持所需的尺寸。

使用getSupportedPictureSizes 获取所有可用的预览尺寸。

要检查相机可用的最大图片尺寸:

List<Size> allSizes = param.getSupportedPictureSizes();
Camera.Size size = allSizes.get(0); // get top size
for (int i = 0; i < allSizes.size(); i++) 
     if (allSizes.get(i).width > size.width)
       size = allSizes.get(i);
 
//set max Picture Size
 params.setPictureSize(size.width, size.height);

【讨论】:

好的,非常感谢,但是如果我想将 1200 X 900 大小的图像保存到 SD 卡中该怎么办 @Sonali:您可以通过在保存到 sd 卡之前更改从相机返回的图像的位图的高宽来做到这一点。但这可能会影响图像质量。试试让我知道 第一次在我的华为 P10 上不工作,但后来我意识到我正在设置“parameters.setFocusMode(Parameters.FOCUS_MODE_AUTO);”在我的前置摄像头没有对焦的前置摄像头上。

以上是关于相机 java.lang.RuntimeException:setParameters 失败的主要内容,如果未能解决你的问题,请参考以下文章

初始化 Visualizer 时的 Android 错误代码 -3

Cause: java.sql.SQLException: No operations allowed after statement closed.

Spark Standalone + Zeppelin + Docker:如何设置 SPARK_HOME

JBoss - 如何在 jboss-deployment-structure 中排除 javax.validation?

win系统执行spark-sql报错:java.io.IOException: (null) entry in command string: null ls -F C: mphive

win系统执行spark-sql报错:java.io.IOException: (null) entry in command string: null ls -F C: mphive