带有TextureView / AutoFitTextureView预览的Camera2 API在纵向模式下被拉伸

问题描述 投票:0回答:1

跟随此https://stackoverflow.com/a/44324039/1427037https://inducesmile.com/android/android-camera2-api-example-tutorial/但是camera2 API仍然可以在Samsung设备android 9.0 OS中提供扩展的Preview。许多stackoverflow解决方案已经尝试过,但是没有优势。有什么帮助吗?注意:无法将相机API与SurfaceHolder一起使用。

android-camera2 android-textureview
1个回答
0
投票

选中此一项

  @Override
  public void setVideoURI(Uri uri) {
    super.setVideoURI(uri);
    MediaMetadataRetriever retr = new MediaMetadataRetriever();
    retr.setDataSource(uri.getPath());
    String height = retr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
    String width = retr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
    try {
      videoRealH = Integer.parseInt(height);
      videoRealW = Integer.parseInt(width);
    } catch (NumberFormatException e) {
      e.printStackTrace();
    }
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = getDefaultSize(0, widthMeasureSpec);
    int height = getDefaultSize(0, heightMeasureSpec);
    if (height > width) {

      if (videoRealH > videoRealW) {

        mVideoHeight = height;
        mVideoWidth = width;
      } else {
        mVideoWidth = width;
        float r = videoRealH / (float) videoRealW;
        mVideoHeight = (int) (mVideoWidth * r);
      }
    } else {
      if (videoRealH > videoRealW) {
        mVideoHeight = height;
        float r = videoRealW / (float) videoRealH;
        mVideoWidth = (int) (mVideoHeight * r);
      } else {
        mVideoHeight = height;
        mVideoWidth = width;
      }
    }
    if (videoRealH == videoRealW && videoRealH == 1) {
      super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    } else {
      setMeasuredDimension(mVideoWidth, mVideoHeight);
    }

  }
© www.soinside.com 2019 - 2024. All rights reserved.