开发者

Issue with setting the parameters to camera

开发者 https://www.devze.com 2023-03-23 13:11 出处:网络
I\'m have an android app which works with the camera. So after a few atempts I started building my own camera by creating a class that implements SurfaceHolder.Callback. The big problem in all this is

I'm have an android app which works with the camera. So after a few atempts I started building my own camera by creating a class that implements SurfaceHolder.Callback. The big problem in all this is that in my method surfaceChanged() when I'm trying to set the parameters to the camera I get: FORCE CLOSE.

Here is how my method looks like:

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        Log.e(TAG, "surfaceChanged");
        if (mPreviewRunning) {
            mCamera.stopPreview();
        }

        Camera.Parameters p = mCamera.getParameters();
   开发者_如何学JAVA     List<Size> sizes = p.getSupportedPictureSizes();

        System.out.println("Lista de parametrii este urmatoarea:"+sizes);
        Size   size = sizes.get(0);
        p.setPictureSize(size.width, size.height);

        p.setPreviewSize(w, h);
        mCamera.setParameters(p);
        try {
            mCamera.setPreviewDisplay(holder);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mCamera.startPreview();
        mPreviewRunning = true;
    }

If I try the above way:

Camera.Parameters p = mCamera.getParameters();
        List<Size> sizes = p.getSupportedPictureSizes();
        Size   size = sizes.get(0);
        p.setPictureSize(size.width, size.height);

Then size is null and I get error at this line:

Size size = sizes.get(0);

If I do this way:

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
            Log.e(TAG, "surfaceChanged");
            if (mPreviewRunning) {
                mCamera.stopPreview();
            }

            Camera.Parameters p = mCamera.getParameters();
            p.setPreviewSize(w, h);
            mCamera.setParameters(p);
            try {
                mCamera.setPreviewDisplay(holder);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mCamera.startPreview();
            mPreviewRunning = true;
        }

I get the following error:

FATAL EXCEPTION: main
java.lang.RuntimeException: setParameters failed
at android.hardware.Camera.native_setParameters(Native Method)
at android.hardware.Camera.setParameters(Camera.java:914)
at com.SplashScreen.EditPhoto.surfaceChanged(EditPhoto.java:535)
at android.view.SurfaceView.updateWindow(SurfaceView.java:554)
at android.view.SurfaceView.dispatchDraw(SurfaceView.java:353)
at android.view.ViewGroup.drawChild(ViewGroup.java:1719)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1448)
at android.view.ViewGroup.drawChild(ViewGroup.java:1719)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1448)
at android.view.View.draw(View.java:6971)
at android.widget.FrameLayout.draw(FrameLayout.java:361)

So what's the proper way to set parameters?Thank you!

EDIT: This is how I instantiate my camera:

public void surfaceCreated(SurfaceHolder holder) {
    Log.e(TAG, "surfaceCreated");
    mCamera = Camera.open();

}


You're doing it the proper way, I have a similar method, exactly like yours, that works perfectly. The only reason I could imagine for your method to fail are the width and height values. Check them out. Check also if the mCamera object is properly instantiated.

I see that you call getSupportedPictureSize(), have you tried getSupportedPreviewSizes() ?

Cheers

0

精彩评论

暂无评论...
验证码 换一张
取 消