Camera.getParameters() 在 Galaxy Tab 上返回 null

发布于 2024-12-17 06:37:01 字数 1035 浏览 0 评论 0原文

这是我的表面更改事件处理代码:

    public void surfaceChanged(SurfaceHolder holder,
                               int format, int width,
                               int height) {
    Camera.Parameters parameters = camera.getParameters();
    Camera.Size size = getBestPreviewSize(width, height,
                parameters);
       //...
    }


    private Camera.Size getBestPreviewSize(int width, int height,
                                       Camera.Parameters parameters) {
        Camera.Size result = null;

        // it fails with NullPointerExceptiopn here,
        // when accessing "getSupportedPreviewSizes" method:
        // that means "parameters" is null
        for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
            ///...
        }
    }

我像这样初始化相机:

    @Override
    public void onResume() {
        super.onResume();
        camera = Camera.open();
    }

这个问题不会发生在我的 Galaxy S Plus 上,也不会发生在 LG Optimus Black 手机上。有人认为这里出了什么问题吗?

Here is my surface-changed event handling code:

    public void surfaceChanged(SurfaceHolder holder,
                               int format, int width,
                               int height) {
    Camera.Parameters parameters = camera.getParameters();
    Camera.Size size = getBestPreviewSize(width, height,
                parameters);
       //...
    }


    private Camera.Size getBestPreviewSize(int width, int height,
                                       Camera.Parameters parameters) {
        Camera.Size result = null;

        // it fails with NullPointerExceptiopn here,
        // when accessing "getSupportedPreviewSizes" method:
        // that means "parameters" is null
        for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
            ///...
        }
    }

I initialize camera like this:

    @Override
    public void onResume() {
        super.onResume();
        camera = Camera.open();
    }

This problem doesn't occur on my Galaxy S Plus neither it happen on LG Optimus Black phone. Has anyone thoughts what's wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苍暮颜 2024-12-24 06:37:01

我已经解决了这个问题。

parameters.getSupportedPreviewSizes()

在 Galaxy Tab 上返回 NULL。因此,我只是检查它是否为空,并且在这种情况下不设置新的预览大小。我在研究了标准相机应用程序源之后得出了这个结论。

I've solved this.

parameters.getSupportedPreviewSizes()

Returns NULL on Galaxy Tab. So I just make a check if it is null and don't set new preview size in such case. To this conclusion I've came after looking into standard Camera application sources.

硬不硬你别怂 2024-12-24 06:37:01

看起来相机变量从未初始化,因此您正在对 null 调用 getParameters() 。首先尝试调用camera = Camera.open();

Looks like the camera variable was never initialized so you are calling getParameters() on null. Try calling camera = Camera.open(); first

撩起发的微风 2024-12-24 06:37:01

相机初始化很大程度上取决于特定设备。例如,特定的三星设备 GT5500 报告空(宽度 = 0,高度 = 0)作为预览的有效分辨率,但如果您尝试使用它,则会导致整个手机崩溃(“硬”重启)。我们使用 mixare 增强现实引擎(http://www.mixare.org)体验了它,并且需要 PITA 进行调试(因为我们没有手机,无法在任何其他硬件上重现该错误)。

然而,关于获得“正确的”预览尺寸,您可以查看我们在 github 上的代码(它是一个免费的开源应用程序)。在文件中: https://github.com/mixare /mixare/blob/master/src/org/mixare/MixView.java(第 871 行及以后)

        List<Camera.Size> supportedSizes = null;
        //On older devices (<1.6) the following will fail
        //the camera will work nevertheless
        supportedSizes = Compatibility.getSupportedPreviewSizes(parameters);

        //preview form factor
        float ff = (float)w/h;
        Log.d("Mixare", "Screen res: w:"+ w + " h:" + h + " aspect ratio:" + ff);

        //holder for the best form factor and size
        float bff = 0;
        int bestw = 0;
        int besth = 0;
        Iterator<Camera.Size> itr = supportedSizes.iterator();

        //we look for the best preview size, it has to be the closest to the
        //screen form factor, and be less wide than the screen itself
        //the latter requirement is because the HTC Hero with update 2.1 will
        //report camera preview sizes larger than the screen, and it will fail
        //to initialize the camera
        //other devices could work with previews larger than the screen though
        while(itr.hasNext()) {
            Camera.Size element = itr.next();
            //current form factor
            float cff = (float)element.width/element.height;
            //check if the current element is a candidate to replace the best match so far
            //current form factor should be closer to the bff
            //preview width should be less than screen width
            //preview width should be more than current bestw
            //this combination will ensure that the highest resolution will win
            Log.d("Mixare", "Candidate camera element: w:"+ element.width + " h:" + element.height + " aspect ratio:" + cff);
            if ((ff-cff <= ff-bff) && (element.width <= w) && (element.width >= bestw)) {
                bff=cff;
                bestw = element.width;
                besth = element.height;
            }
        } 
        Log.d("Mixare", "Chosen camera element: w:"+ bestw + " h:" + besth + " aspect ratio:" + bff);
        //Some Samsung phones will end up with bestw and besth = 0 because their minimum preview size is bigger then the screen size.
        //In this case, we use the default values: 480x320
        if ((bestw == 0) || (besth == 0)){
            Log.d("Mixare", "Using default camera parameters!");
            bestw = 480;
            besth = 320;
        }
        parameters.setPreviewSize(bestw, besth);

如您所见,我们没有直接使用对 Camera 类的 getSupportedPreviewSizes 的调用,而是添加了一个兼容性层(代码在这里: https ://github.com/mixare/mixare/blob/master/src/org/mixare/Compatibility.java )因为我们需要与旧手机兼容。如果您不想支持较旧的 Android 版本,您可以直接使用 Camera 类的方法。

华泰
丹尼尔

camera initialization depends a lot on the specific device. For instance a specific Samsung device GT5500 is reporting null (width = 0, height = 0) as a valid resolution for preview, but crashes the whole phone ("hard" reboot) if you try to use it. We experienced it with mixare augmented reality engine (http://www.mixare.org) and it was PITA to debug (since we didn't have the phone and could not reproduce the bug on any other hardware).

However, about getting the "right" preview size you can take a look at our code (it's a free and open source app) on github. In the file: https://github.com/mixare/mixare/blob/master/src/org/mixare/MixView.java (row 871 and onwards)

        List<Camera.Size> supportedSizes = null;
        //On older devices (<1.6) the following will fail
        //the camera will work nevertheless
        supportedSizes = Compatibility.getSupportedPreviewSizes(parameters);

        //preview form factor
        float ff = (float)w/h;
        Log.d("Mixare", "Screen res: w:"+ w + " h:" + h + " aspect ratio:" + ff);

        //holder for the best form factor and size
        float bff = 0;
        int bestw = 0;
        int besth = 0;
        Iterator<Camera.Size> itr = supportedSizes.iterator();

        //we look for the best preview size, it has to be the closest to the
        //screen form factor, and be less wide than the screen itself
        //the latter requirement is because the HTC Hero with update 2.1 will
        //report camera preview sizes larger than the screen, and it will fail
        //to initialize the camera
        //other devices could work with previews larger than the screen though
        while(itr.hasNext()) {
            Camera.Size element = itr.next();
            //current form factor
            float cff = (float)element.width/element.height;
            //check if the current element is a candidate to replace the best match so far
            //current form factor should be closer to the bff
            //preview width should be less than screen width
            //preview width should be more than current bestw
            //this combination will ensure that the highest resolution will win
            Log.d("Mixare", "Candidate camera element: w:"+ element.width + " h:" + element.height + " aspect ratio:" + cff);
            if ((ff-cff <= ff-bff) && (element.width <= w) && (element.width >= bestw)) {
                bff=cff;
                bestw = element.width;
                besth = element.height;
            }
        } 
        Log.d("Mixare", "Chosen camera element: w:"+ bestw + " h:" + besth + " aspect ratio:" + bff);
        //Some Samsung phones will end up with bestw and besth = 0 because their minimum preview size is bigger then the screen size.
        //In this case, we use the default values: 480x320
        if ((bestw == 0) || (besth == 0)){
            Log.d("Mixare", "Using default camera parameters!");
            bestw = 480;
            besth = 320;
        }
        parameters.setPreviewSize(bestw, besth);

As you see we're not using directly the call to getSupportedPreviewSizes of the Camera class, but instead added a compatibility layer (the code is here: https://github.com/mixare/mixare/blob/master/src/org/mixare/Compatibility.java ) because we needed compatibility with older phones. If you don't want to support older android releases you can use the method of the Camera class directly.

HTH
Daniele

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文