使用Android camera2进行全屏预览

前端之家收集整理的这篇文章主要介绍了使用Android camera2进行全屏预览前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用新的camera2 API构建自定义相机.我的代码基于Google here提供的代码示例.

我无法找到一种方法来全屏显示相机.在代码示例中,他们使用比率优化来适应所有屏幕,但它只占屏幕高度的3/4左右.

这是我的AutoFitTextureView代码

  1. public class AutoFitTextureView extends TextureView {
  2.  
  3. private int mRatioWidth = 0;
  4. private int mRatioHeight = 0;
  5.  
  6. public AutoFitTextureView(Context context) {
  7. this(context,null);
  8. }
  9.  
  10. public AutoFitTextureView(Context context,AttributeSet attrs) {
  11. this(context,attrs,0);
  12. }
  13.  
  14. public AutoFitTextureView(Context context,AttributeSet attrs,int defStyle) {
  15. super(context,defStyle);
  16. }
  17.  
  18. /**
  19. * Sets the aspect ratio for this view. The size of the view will be measured based on the ratio
  20. * calculated from the parameters. Note that the actual sizes of parameters don't matter,that
  21. * is,calling setAspectRatio(2,3) and setAspectRatio(4,6) make the same result.
  22. *
  23. * @param width Relative horizontal size
  24. * @param height Relative vertical size
  25. */
  26. public void setAspectRatio(int width,int height) {
  27. if (width < 0 || height < 0) {
  28. throw new IllegalArgumentException("Size cannot be negative.");
  29. }
  30. mRatioWidth = width;
  31. mRatioHeight = height;
  32. requestLayout();
  33. }
  34.  
  35. @Override
  36. protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec) {
  37. super.onMeasure(widthMeasureSpec,heightMeasureSpec);
  38. int width = MeasureSpec.getSize(widthMeasureSpec);
  39. int height = MeasureSpec.getSize(heightMeasureSpec);
  40.  
  41. if (0 == mRatioWidth || 0 == mRatioHeight) {
  42. setMeasuredDimension(width,height);
  43. } else {
  44. if (width < height * mRatioWidth / mRatioHeight) {
  45. setMeasuredDimension(width,width * mRatioHeight / mRatioWidth);
  46. } else {
  47. setMeasuredDimension(height * mRatioWidth / mRatioHeight,height);
  48. }
  49. }
  50. }

}

非常感谢您的帮助.

解决方法

这是您的问题的解决方案.在此 line中,纵横比设置为3/4.我更改了chooseVideSize方法,为MediaRecorder选择具有高清分辨率的视频大小.
  1. private static Size chooseVideoSize(Size[] choices) {
  2. for (Size size : choices) {
  3. // Note that it will pick only HD video size,you should create more robust solution depending on screen size and available video sizes
  4. if (1920 == size.getWidth() && 1080 == size.getHeight()) {
  5. return size;
  6. }
  7. }
  8. Log.e(TAG,"Couldn't find any suitable video size");
  9. return choices[choices.length - 1];
  10. }

然后我更正了this method以根据视频大小纵横比选择预览尺寸,结果如下.

  1. private static Size chooSEOptimalSize(Size[] choices,int width,int height,Size aspectRatio) {
  2. // Collect the supported resolutions that are at least as big as the preview Surface
  3. List<Size> bigEnough = new ArrayList<Size>();
  4. int w = aspectRatio.getWidth();
  5. int h = aspectRatio.getHeight();
  6. double ratio = (double) h / w;
  7. for (Size option : choices) {
  8. double optionRatio = (double) option.getHeight() / option.getWidth();
  9. if (ratio == optionRatio) {
  10. bigEnough.add(option);
  11. }
  12. }
  13.  
  14. // Pick the smallest of those,assuming we found any
  15. if (bigEnough.size() > 0) {
  16. return Collections.min(bigEnough,new CompareSizesByArea());
  17. } else {
  18. Log.e(TAG,"Couldn't find any suitable preview size");
  19. return choices[1];
  20. }
  21. }

我希望它会对你有所帮助!

猜你在找的Android相关文章