android.view.TextureView.getWidth()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(12.8k)|赞(0)|评价(0)|浏览(192)

本文整理了Java中android.view.TextureView.getWidth()方法的一些代码示例,展示了TextureView.getWidth()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。TextureView.getWidth()方法的具体详情如下:
包路径:android.view.TextureView
类名称:TextureView
方法名:getWidth

TextureView.getWidth介绍

暂无

代码示例

代码示例来源:origin: Bilibili/DanmakuFlameMaster

@Override
public int getViewWidth() {
  return super.getWidth();
}

代码示例来源:origin: google/ExoPlayer

/** Applies a texture rotation to a {@link TextureView}. */
private static void applyTextureViewRotation(TextureView textureView, int textureViewRotation) {
 float textureViewWidth = textureView.getWidth();
 float textureViewHeight = textureView.getHeight();
 if (textureViewWidth == 0 || textureViewHeight == 0 || textureViewRotation == 0) {
  textureView.setTransform(null);
 } else {
  Matrix transformMatrix = new Matrix();
  float pivotX = textureViewWidth / 2;
  float pivotY = textureViewHeight / 2;
  transformMatrix.postRotate(textureViewRotation, pivotX, pivotY);
  // After rotation, scale the rotated texture to fit the TextureView size.
  RectF originalTextureRect = new RectF(0, 0, textureViewWidth, textureViewHeight);
  RectF rotatedTextureRect = new RectF();
  transformMatrix.mapRect(rotatedTextureRect, originalTextureRect);
  transformMatrix.postScale(
    textureViewWidth / rotatedTextureRect.width(),
    textureViewHeight / rotatedTextureRect.height(),
    pivotX,
    pivotY);
  textureView.setTransform(transformMatrix);
 }
}

代码示例来源:origin: journeyapps/zxing-android-embedded

private void startPreviewIfReady() {
  if (currentSurfaceSize != null && previewSize != null && surfaceRect != null) {
    if (surfaceView != null && currentSurfaceSize.equals(new Size(surfaceRect.width(), surfaceRect.height()))) {
      startCameraPreview(new CameraSurface(surfaceView.getHolder()));
    } else if(textureView != null && textureView.getSurfaceTexture() != null) {
      if(previewSize != null) {
        Matrix transform = calculateTextureTransform(new Size(textureView.getWidth(), textureView.getHeight()), previewSize);
        textureView.setTransform(transform);
      }
      startCameraPreview(new CameraSurface(textureView.getSurfaceTexture()));
    } else {
      // Surface is not the correct size yet
    }
  }
}

代码示例来源:origin: journeyapps/zxing-android-embedded

/**
 * Start the camera preview and decoding. Typically this should be called from the Activity's
 * onResume() method.
 *
 * Call from UI thread only.
 */
public void resume() {
  // This must be safe to call multiple times
  Util.validateMainThread();
  Log.d(TAG, "resume()");
  // initCamera() does nothing if called twice, but does log a warning
  initCamera();
  if (currentSurfaceSize != null) {
    // The activity was paused but not stopped, so the surface still exists. Therefore
    // surfaceCreated() won't be called, so init the camera here.
    startPreviewIfReady();
  } else if(surfaceView != null) {
    // Install the callback and wait for surfaceCreated() to init the camera.
    surfaceView.getHolder().addCallback(surfaceCallback);
  } else if(textureView != null) {
    if(textureView.isAvailable()) {
      surfaceTextureListener().onSurfaceTextureAvailable(textureView.getSurfaceTexture(), textureView.getWidth(), textureView.getHeight());
    } else {
      textureView.setSurfaceTextureListener(surfaceTextureListener());
    }
  }
  // To trigger surfaceSized again
  requestLayout();
  rotationListener.listen(getContext(), rotationCallback);
}

代码示例来源:origin: google/ExoPlayer

@Override
public void setVideoTextureView(TextureView textureView) {
 verifyApplicationThread();
 removeSurfaceCallbacks();
 this.textureView = textureView;
 if (textureView == null) {
  setVideoSurfaceInternal(null, true);
  maybeNotifySurfaceSizeChanged(/* width= */ 0, /* height= */ 0);
 } else {
  if (textureView.getSurfaceTextureListener() != null) {
   Log.w(TAG, "Replacing existing SurfaceTextureListener.");
  }
  textureView.setSurfaceTextureListener(componentListener);
  SurfaceTexture surfaceTexture = textureView.isAvailable() ? textureView.getSurfaceTexture()
    : null;
  if (surfaceTexture == null) {
   setVideoSurfaceInternal(/* surface= */ null, /* ownsSurface= */ true);
   maybeNotifySurfaceSizeChanged(/* width= */ 0, /* height= */ 0);
  } else {
   setVideoSurfaceInternal(new Surface(surfaceTexture), /* ownsSurface= */ true);
   maybeNotifySurfaceSizeChanged(textureView.getWidth(), textureView.getHeight());
  }
 }
}

代码示例来源:origin: QuickBlox/ChatMessagesAdapter-android

private void setCorrectRotation() {
  int viewWidth = textureView.getWidth();
  int viewHeight = textureView.getHeight();
  Matrix matrix = new Matrix();
  matrix.reset();
  int px = viewWidth / 2;
  int py = viewHeight / 2;
  matrix.postRotate(rotationDegree, px, py);
  float ratio = (float) viewHeight / viewWidth;
  matrix.postScale(1 / ratio, ratio, px, py);
  textureView.setTransform(matrix);
}

代码示例来源:origin: googlecreativelab/shadercam

@Override
public void onOpened(CameraDevice cameraDevice) {
  mCameraOpenCloseLock.release();
  mCameraDevice = cameraDevice;
  mCameraIsOpen = true;
  startPreview();
  //overkill?
  if (mTextureView != null) {
    configureTransform(mTextureView.getWidth(), mTextureView.getHeight());
  }
}

代码示例来源:origin: fanbaoying/FBYIDCardRecognition-Android

private Camera.Size getOptimalSize(List<Camera.Size> sizes) {
  int width = previewView.textureView.getWidth();
  int height = previewView.textureView.getHeight();
  Camera.Size pictureSize = sizes.get(0);
  List<Camera.Size> candidates = new ArrayList<>();
  for (Camera.Size size : sizes) {
    if (size.width >= width && size.height >= height && size.width * height == size.height * width) {
      // 比例相同
      candidates.add(size);
    } else if (size.height >= width && size.width >= height && size.width * width == size.height * height) {
      // 反比例
      candidates.add(size);
    }
  }
  if (!candidates.isEmpty()) {
    return Collections.min(candidates, sizeComparator);
  }
  for (Camera.Size size : sizes) {
    if (size.width > width && size.height > height) {
      return size;
    }
  }
  return pictureSize;
}

代码示例来源:origin: vbier/habpanelviewer

private void configureTransform(Point previewSize, int rotation) {
  if (null == mPreviewView || null == mActivity) {
    return;
  }
  Matrix matrix = new Matrix();
  RectF viewRect = new RectF(0, 0, mPreviewView.getWidth(), mPreviewView.getHeight());
  RectF bufferRect = new RectF(0, 0, previewSize.y, previewSize.x);
  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) mPreviewView.getHeight() / previewSize.y,
        (float) mPreviewView.getWidth() / previewSize.x);
    matrix.postScale(scale, scale, centerX, centerY);
  }
  matrix.postRotate(-90 * rotation, centerX, centerY);
  mPreviewView.setTransform(matrix);
}

代码示例来源:origin: fanbaoying/FBYIDCardRecognition-Android

@Override
public void refreshPermission() {
  openCamera(textureView.getWidth(), textureView.getHeight());
}

代码示例来源:origin: yangjie10930/OpenGL4Android

/**
 * 设置显示比例
 */
private void changeScale(boolean one){
  if(one){
    stopPreview();
    int mTextureViewWidth = mTextureView.getWidth();
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(mTextureViewWidth,mTextureViewWidth);
    layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT,RelativeLayout.TRUE);
    mTextureView.setLayoutParams(layoutParams);
    mCameraRecord.setOutputSize(mOneSize);
    mCamera = Camera.open(mUserCamera);
    mCameraRecord.startPreview();
  }
}

代码示例来源:origin: Manuiq/ZoomableTextureView

/**
 * Applies a texture rotation to a {@link TextureView}.
 */
private static void applyTextureViewRotation(TextureView textureView, int textureViewRotation) {
  float textureViewWidth = textureView.getWidth();
  float textureViewHeight = textureView.getHeight();
  if (textureViewWidth == 0 || textureViewHeight == 0 || textureViewRotation == 0) {
    textureView.setTransform(null);
  } else {
    Matrix transformMatrix = new Matrix();
    float pivotX = textureViewWidth / 2;
    float pivotY = textureViewHeight / 2;
    transformMatrix.postRotate(textureViewRotation, pivotX, pivotY);
    // After rotation, scale the rotated texture to fit the TextureView size.
    RectF originalTextureRect = new RectF(0, 0, textureViewWidth, textureViewHeight);
    RectF rotatedTextureRect = new RectF();
    transformMatrix.mapRect(rotatedTextureRect, originalTextureRect);
    transformMatrix.postScale(
        textureViewWidth / rotatedTextureRect.width(),
        textureViewHeight / rotatedTextureRect.height(),
        pivotX,
        pivotY);
    textureView.setTransform(transformMatrix);
  }
}

代码示例来源:origin: square1-io/rich-text-android

public static void adjustAspectRatio(TextureView textureView, int videoWidth, int videoHeight) {
  int viewWidth = textureView.getWidth();
  int viewHeight = textureView.getHeight();
  double aspectRatio = (double) videoHeight / videoWidth;
  int newWidth, newHeight;
  if (viewHeight > (int) (viewWidth * aspectRatio)) {
    // limited by narrow width; restrict height
    newWidth = viewWidth;
    newHeight = (int) (viewWidth * aspectRatio);
  } else {
    // limited by short height; restrict width
    newWidth = (int) (viewHeight / aspectRatio);
    newHeight = viewHeight;
  }
  int xoff = (viewWidth - newWidth) / 2;
  int yoff = (viewHeight - newHeight) / 2;
  Matrix txform = new Matrix();
  textureView.getTransform(txform);
  txform.setScale((float) newWidth / viewWidth, (float) newHeight / viewHeight);
  txform.postTranslate(xoff, yoff);
  textureView.setTransform(txform);
}

代码示例来源:origin: yixia/VitamioBundleStudio

/**
 * Sets the TextureView transform to preserve the aspect ratio of the video.
 */
private void adjustAspectRatio(int videoWidth, int videoHeight) {
 int viewWidth = mTextureView.getWidth();
 int viewHeight = mTextureView.getHeight();
 double aspectRatio = (double) videoHeight / videoWidth;
 int newWidth, newHeight;
 if (viewHeight > (int) (viewWidth * aspectRatio)) {
  // limited by narrow width; restrict height
  newWidth = viewWidth;
  newHeight = (int) (viewWidth * aspectRatio);
 } else {
  // limited by short height; restrict width
  newWidth = (int) (viewHeight / aspectRatio);
  newHeight = viewHeight;
 }
 int xoff = (viewWidth - newWidth) / 2;
 int yoff = (viewHeight - newHeight) / 2;
 Log.v(TAG, "video=" + videoWidth + "x" + videoHeight + " view=" + viewWidth + "x" + viewHeight
   + " newView=" + newWidth + "x" + newHeight + " off=" + xoff + "," + yoff);
 Matrix txform = new Matrix();
 mTextureView.getTransform(txform);
 txform.setScale((float) newWidth / viewWidth, (float) newHeight / viewHeight);
 //txform.postRotate(10);          // just for fun
 txform.postTranslate(xoff, yoff);
 mTextureView.setTransform(txform);
}

代码示例来源:origin: googlecreativelab/shadercam

@Override
  public void run() {
    if (mRestartCamera) {
      setReady(mTextureView.getSurfaceTexture(), mTextureView.getWidth(), mTextureView.getHeight());
      mRestartCamera = false;
    }
  }
});

代码示例来源:origin: mobapptuts/android_camera2_api_video_app

@Override
protected void onResume() {
  super.onResume();
  startBackgroundThread();
  if(mTextureView.isAvailable()) {
    setupCamera(mTextureView.getWidth(), mTextureView.getHeight());
    connectCamera();
  } else {
    mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
  }
}

代码示例来源:origin: fanbaoying/FBYIDCardRecognition-Android

@Override
public void start() {
  startBackgroundThread();
  if (textureView.isAvailable()) {
    openCamera(textureView.getWidth(), textureView.getHeight());
    textureView.setSurfaceTextureListener(surfaceTextureListener);
  } else {
    textureView.setSurfaceTextureListener(surfaceTextureListener);
  }
}

代码示例来源:origin: square1-io/rich-text-android

public void handover(RichVideoView destination){
  destination.mMediaPlayer = mMediaPlayer;
  if(destination.mTextureView.isAvailable() == true){
    destination.mSurfaceTextureListener.onSurfaceTextureAvailable(destination.mTextureView.getSurfaceTexture(),
        destination.mTextureView.getWidth(),
        destination.mTextureView.getHeight());
  }
  destination.initMediaPlayer();
  destination.mMediaPlayer.syncMediaState();
}

代码示例来源:origin: wangli135/BlogDemo

private void openCamera() {
  if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},
        1);
    return;
  }
  //获取相机对象
  CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
  try {
    for (String id : cameraManager.getCameraIdList()) {
      CameraCharacteristics cameraCharacteristics = cameraManager.getCameraCharacteristics(id);
      Integer facing = cameraCharacteristics.get(CameraCharacteristics.LENS_FACING);
      if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) {
        continue;
      }
      cameraId = id;
      imageReader = ImageReader.newInstance(textureView.getWidth(), textureView.getHeight(), ImageFormat.JPEG, 2);
      imageReader.setOnImageAvailableListener(mOnImageAvailableListener, mBackgroundHandler);
      break;
    }
  } catch (CameraAccessException e) {
    e.printStackTrace();
  }
  try {
    cameraManager.openCamera(cameraId, mStateCallback, mBackgroundHandler);
  } catch (CameraAccessException e) {
    e.printStackTrace();
  }
}

代码示例来源:origin: googlecreativelab/shadercam

@Override
protected void onResume() {
  super.onResume();
  Log.d(TAG, "onResume()");
  ShaderUtils.goFullscreen(this.getWindow());
  /**
   * if we're on M and not satisfied, check for permissions needed
   * {@link PermissionsHelper#checkPermissions()} will also instantly return true if we've
   * checked prior and we have all the correct permissions, allowing us to continue, but if its
   * false, we want to {@code return} here so that the popup will trigger without {@link #setReady(SurfaceTexture, int, int)}
   * being called prematurely
   */
  //
  if(PermissionsHelper.isMorHigher() && !mPermissionsSatisfied) {
    if(!mPermissionsHelper.checkPermissions())
      return;
    else
      mPermissionsSatisfied = true; //extra helper as callback sometimes isnt quick enough for future results
  }
  if(!mTextureView.isAvailable())
    mTextureView.setSurfaceTextureListener(mTextureListener); //set listener to handle when its ready
  else
    setReady(mTextureView.getSurfaceTexture(), mTextureView.getWidth(), mTextureView.getHeight());
}

相关文章