You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Camera2Basic示例相机启动报错求助:Surface尺寸无效及BufferQueue异常

Fixing Camera2Basic Preview Errors: Invalid Surface Size & Abandoned BufferQueue

Hey there, let's tackle these two errors you're seeing—they're directly linked, so fixing the first will resolve the second. Here's what's going on and how to fix it:

What's Causing the Issues?

  1. Invalid Surface Size Error:

    Surface with size (w=720, h=960) and format 0x1 is not valid, size not in valid set: [1440x1080, 1280x720, ...]

    Camera2 API requires your preview Surface (from the TextureView/SurfaceView) to use a size explicitly supported by the camera hardware. The 720x960 size your app is trying to use isn't in the camera's valid list—this usually happens when the sample code doesn't account for screen orientation (camera sensors are typically landscape, so portrait mode can swap width/height incorrectly).

  2. Abandoned BufferQueue:

    cancelBuffer: BufferQueue has been abandoned

    This is a secondary error caused by the invalid Surface. When the camera can't send frames to an unsupported Surface, it drops the BufferQueue connection entirely. Fix the Surface issue, and this error will disappear automatically.

Step-by-Step Fixes

1. Fix Preview Size Matching

The root issue is in how the sample selects the preview size. Open Camera2BasicFragment.java and modify the chooseOptimalSize method to account for screen orientation:

private static Size chooseOptimalSize(Size[] choices, int textureViewWidth, int textureViewHeight, int maxWidth, int maxHeight, Size aspectRatio) {
    List<Size> bigEnough = new ArrayList<>();
    List<Size> notBigEnough = new ArrayList<>();
    int targetRatioWidth = aspectRatio.getWidth();
    int targetRatioHeight = aspectRatio.getHeight();

    // Check if we're in portrait mode (swap width/height to match camera's landscape sensor)
    boolean isPortrait = textureViewHeight > textureViewWidth;

    for (Size option : choices) {
        int optionDisplayWidth = isPortrait ? option.getHeight() : option.getWidth();
        int optionDisplayHeight = isPortrait ? option.getWidth() : option.getHeight();

        // Only consider sizes that match the target aspect ratio and fit within max dimensions
        if (option.getHeight() * targetRatioWidth == option.getWidth() * targetRatioHeight
                && optionDisplayWidth <= maxWidth
                && optionDisplayHeight <= maxHeight) {
            if (optionDisplayWidth >= textureViewWidth && optionDisplayHeight >= textureViewHeight) {
                bigEnough.add(option);
            } else {
                notBigEnough.add(option);
            }
        }
    }

    // Pick the smallest size that's big enough, or largest that's too small
    if (!bigEnough.isEmpty()) {
        return Collections.min(bigEnough, new CompareSizesByArea());
    } else if (!notBigEnough.isEmpty()) {
        return Collections.max(notBigEnough, new CompareSizesByArea());
    } else {
        Log.e(TAG, "No valid preview sizes found! Falling back to first option.");
        return choices[0];
    }
}

This code:

  • Detects portrait mode and swaps the camera's size dimensions to match how it displays on screen.
  • Filters sizes to only those that match the camera's supported aspect ratio and fit within your device's screen limits.

2. Verify Surface Format

The format 0x1 in your error refers to PixelFormat.RGBA_8888—most cameras support this for preview Surfaces, but double-check these details:

  • Don't explicitly set an unsupported format on the TextureView.
  • When creating the preview request, use the Surface directly from the properly sized TextureView:
    mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
    mPreviewRequestBuilder.addTarget(mSurface); // mSurface comes from textureView.getSurfaceTexture()
    

3. Handle Camera State Cleanly

To prevent BufferQueue leaks:

  • In onPause(), ensure you close the camera device and stop the capture session properly.
  • In onResume(), reinitialize the TextureView and camera session only after the view is fully available (use TextureView.SurfaceTextureListener to wait for onSurfaceTextureAvailable).

Testing the Fix

After making these changes:

  1. Rebuild and run the app.
  2. Test in both portrait and landscape modes to ensure the preview works correctly.
  3. Check logcat—both errors should no longer appear.

内容的提问来源于stack exchange,提问作者Ahmad Hamzavi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:36:19