设置相机预览缓冲区大小

时间:2011-12-19 18:54:57

标签: android camera

我正在使用HTC Wildfire和Android 2.2.1测试我的应用。 默认的相机预览尺寸为768x432。我从设备日志中看到它:

12-19 18:41:34.088: D/QualcommCameraHardware(72): initPreview E: preview size=768x432
12-19 18:41:34.088: D/QualcommCameraHardware(72): initPreview E: preview size=768x432 videosize = 768 x 432
12-19 18:41:34.088: D/QualcommCameraHardware(72): mBufferSize=497664, mAlignedBufferSize=524288

好的,它有效。但是,如果我尝试将预览大小更改为某些不同的值,如320x240或800x480,屏幕上只有一堆像素。这里最有趣的是,如果我使用预览缓冲区大小小于默认缓冲区大小(497664)我在日志中出现此错误:

12-19 18:52:49.288: E/Camera-JNI(5776): Manually set buffer was too small! Expected 497664 bytes, but got 144000!

很明显,实际的缓冲区大小没有改变。有什么方法可以解决这个问题吗?

我还应该注意到,使用Android 3.2.1的华硕Eee Pad Transformer没有这样的问题

3 个答案:

答案 0 :(得分:4)

我发现了问题所在。 看来在Android 2.2.1上你必须在调用之前设置相机参数

camera.startPreview()

然而,在Android 3.2.1等后续系统中,这不再是一个问题(但它可能是供应商特定的)。

答案 1 :(得分:0)

Android上的摄像头是一种伏都教。我建议看看实际支持哪些预览尺寸(好吧,至少是广告):

        Camera.Parameters cameraParameters = camera.getParameters();
        // print out parameters
        Log.d(LOG_TAG, "flash modes:" + cameraParameters.getSupportedFlashModes());
        Log.d(LOG_TAG, "autofocus:" + cameraParameters.getSupportedFocusModes());
        Log.d(LOG_TAG, "preview formats:" + cameraParameters.getSupportedPreviewFormats());
        Log.d(LOG_TAG, "scene modes:" + cameraParameters.getSupportedSceneModes());
        Log.d(LOG_TAG, "white balance modes:" + cameraParameters.getSupportedWhiteBalance());

不幸的是,并非所有预览尺寸都可以使用,即使它们在此列表中也是如此。 至少不是对摩托罗拉的挑战或htc英雄。所以我做了以下事情:

            // as we know,  that big preview size can produce RE on HTC Hero,
        // we just iterate through allowed preview sizes until we find something proper
        //  go for maximal allowed preview size

        previewSize = cameraParameters.getPreviewSize();

        if (previewSize.width <= MIN_PREVIEW_WIDTH) {
            Log.d(LOG_TAG, "preview size is too small:" + previewSize.width + "x" + previewSize.height);
            final List<Camera.Size> sizes = cameraParameters.getSupportedPreviewSizes();
            Collections.sort(sizes, new Comparator<Camera.Size>() {
                public int compare(Camera.Size o1, Camera.Size o2) {
                    return new Integer(o2.width).compareTo(o1.width);
                }
            });

            for (Camera.Size size : sizes) {
                cameraParameters.setPreviewSize(size.width, size.height);
                camera.setParameters(cameraParameters);
                Log.d(LOG_TAG, "attempt preview size:" + size.width + "x" + size.height);
                try {
                    camera.startPreview();
                    Log.d(LOG_TAG, "...accepted - go along");
                    //  ok, camera accepted out settings.  since we know,
                    // that some implementations may choose different preview format,
                    // we retrieve parameters again. just to be sure
                    cameraParameters = camera.getParameters();
                    break;
                } catch (RuntimeException rx) {
                    // ups, camera did not like this size
                    Log.d(LOG_TAG, "...barfed, try next");
                }

            }
        } else {
            Log.d(LOG_TAG, " accepted default preview size on the spot:" + previewSize.width + "x" + previewSize.height);
            camera.startPreview();
        }

答案 2 :(得分:0)

我的2.2版解决方案:

Camera.PictureCallback photoCallback=new Camera.PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
mCamera.addCallbackBuffer(data);
mCamera.startPreview();

http://developer.android.com/reference/android/hardware/Camera.html#addCallbackBuffer%28byte[]%29
        }
                };