0

Followed this https://stackoverflow.com/a/44324039/1427037 https://inducesmile.com/android/android-camera2-api-example-tutorial/ but still camera2 API gives stretched Preview in Samsung device android 9.0 OS. Lots of stackoverflow solution already tried but no advantage. Any help ? Note: Cant use camera API with SurfaceHolder.

Erum
  • 790
  • 3
  • 14
  • 36
  • retested app in Android OS 6.0.1 its working fine. its only stretching in android 9 OS Any solution ? – Erum Jan 09 '20 at 12:24

1 Answers1

1

check this one

  @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);
    }

  }
Rahul Chokshi
  • 670
  • 4
  • 18