2

我已经在我的应用程序中实现了 camera2,它正在 Nexus 6P 和 Nexus 5 上运行。现在我正在尝试在其他设备上对其进行测试,而我尝试的第一个设备立即失败了。这是我在运行 Lollipop 的 HTC M7 上遇到的错误:

Surface with size (w=1920, h=1080) and format 0x1 is not valid, 
size not in valid set: [1920x1088, 1440x1088, 1456x832, 1088x1088,
1280x720, 960x720, 960x544, 720x720, 800x480, 768x464, 720x480, 
768x432, 640x480, 544x544, 576x432, 640x384, 640x368, 480x480, 
480x320, 384x288, 352x288, 320x240, 240x160, 176x144]

有什么建议在这种情况下我应该怎么做?我已经尝试计算最接近我的分辨率TextureView(即 1280x720)并TextureView相应地调整大小,但这看起来并不特别好 - 未使用的空间太多......在使用旧相机的设备上没有看到这个问题和SurfaceView

编辑:

问题似乎在我的TextureView. 这是我的代码:

在控制器内部我有:

 TextureView.SurfaceTextureListener mSurfaceTextureListener = new TextureView.SurfaceTextureListener() {
    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture,
            int width, int height) {
        startLollipopPreview(width, height);
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture,
            int width, int height) {
        configureTransform(width,height);
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
    }
};


 private void startLollipopPreview(int width, int height) {
    CameraProxy camera = getCurrentCamera();
    try {
        if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
            throw new RuntimeException("Time out waiting to lock camera opening.");
        }
        mPreviewSize = camera
                .getOptimalPreviewSize((Activity) mContext,
                        camera.getSupportedPreviewSizes(
                                width, height), (double) width / height);
        mPreviewTexture.setAspectRatio(mPreviewSize.width, mPreviewSize.height);
        mPreviewTexture.getSurfaceTexture().setDefaultBufferSize(mPreviewSize.width,
                mPreviewSize.height);
        configureTransform(mPreviewSize.width, mPreviewSize.height);
        camera.setStateAndHandler(getCameraStateCallback(), mBackgroundHandler);
        camera.open();
    } catch (CameraHardwareException e) {
        Log.e(TAG, ".surfaceCreated() - Error opening camera", e);
        ((Activity) mContext).finish();
    } catch (InterruptedException e) {
        Log.e(TAG, ".surfaceCreated() - InterruptedException opening camera", e);
    }
}

configureTransform()看起来与 Camera2 google 示例完全一样,所以我认为问题不存在。

在我的里面我TextureView有以下内容:

 public void setAspectRatio(int width, int height) {
    if (width < 0 || height < 0) {
        throw new IllegalArgumentException("Size cannot be negative.");
    }
    mRatioWidth = width;
    mRatioHeight = height;
    requestLayout();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);
    if (0 == mRatioWidth || 0 == mRatioHeight) {
        setMeasuredDimension(width, height);
    } else {
        if (width < height * mRatioWidth / mRatioHeight) {
            FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) getLayoutParams();
            params.gravity = Gravity.CENTER_VERTICAL;
            setLayoutParams(params);
            setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
        } else {
            setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
        }
    }
}
4

2 回答 2

1

您面临的问题会根据您测试应用程序的模拟器/设备而有所不同。

当屏幕尺寸的设备兼容尺寸和 MediaRecorder 设置不匹配时,会发生此错误

当相机打开时(方法),它通过使用方法openCamera(int w, int h)获取支持的分辨率的大小。chooseOptimalSize(.... .....)但在此之前,它会设置videoSize().

尝试 Logcat 以获取支持的大小,并根据该设置mMediaRecorder.setVideoSize(w,h);

于 2017-11-29T05:39:10.503 回答
1

我相信您需要将缓冲区大小设置为支持的预览大小:

textureView.getSurfaceTexture().setDefaultBufferSize(1280,720);

然后您可以缩放 TextureView 以使其适合您的屏幕,即使预览尺寸较小。Camera2Video示例有一个示例。具体看configureTransformCamera2VideoFragment

/**
 * Configures the necessary {@link android.graphics.Matrix} transformation to `mTextureView`.
 * This method should not to be called until the camera preview size is determined in
 * openCamera, or until the size of `mTextureView` is fixed.
 *
 * @param viewWidth  The width of `mTextureView`
 * @param viewHeight The height of `mTextureView`
 */
private void configureTransform(int viewWidth, int viewHeight) {
    Activity activity = getActivity();
    if (null == mTextureView || null == mPreviewSize || null == activity) {
        return;
    }
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    Matrix matrix = new Matrix();
    RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
    RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth());
    float centerX = viewRect.centerX();
    float centerY = viewRect.centerY();
    if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
        bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
        matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
        float scale = Math.max(
                (float) viewHeight / mPreviewSize.getHeight(),
                (float) viewWidth / mPreviewSize.getWidth());
        matrix.postScale(scale, scale, centerX, centerY);
        matrix.postRotate(90 * (rotation - 2), centerX, centerY);
    }
    mTextureView.setTransform(matrix);
}
于 2015-12-17T14:39:20.500 回答