美文网首页
彻底解决 Android Camera 预览拉伸的问题

彻底解决 Android Camera 预览拉伸的问题

作者: 世界是一个圆_ | 来源:发表于2019-05-07 16:09 被阅读0次

步骤1:
根据 Camera 支持的尺寸和当前屏幕的尺寸选择一个合适的预览尺寸,大概的代码如下:

private static Camera.Size getOptimalSize(int w, int h) {
    Camera.Parameters cameraParameter = camera.getParameters();
    List<Camera.Size> sizes = cameraParameter.getSupportedPreviewSizes()
    final double ASPECT_TOLERANCE = 0.1;
    // 竖屏是 h/w, 横屏是 w/h
    double targetRatio = (double) h / w;
    Camera.Size optimalSize = null;
    double minDiff = Double.MAX_VALUE;

    int targetHeight = h;

    for (Camera.Size size : sizes) {
      double ratio = (double) size.width / size.height;
      if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
      if (Math.abs(size.height - targetHeight) < minDiff) {
        optimalSize = size;
        minDiff = Math.abs(size.height - targetHeight);
      }
    }

    if (optimalSize == null) {
      minDiff = Double.MAX_VALUE;
      for (Camera.Size size : sizes) {
        if (Math.abs(size.height - targetHeight) < minDiff) {
          optimalSize = size;
          minDiff = Math.abs(size.height - targetHeight);
        }
      }
    }

    return optimalSize;
  }

步骤2:
获取了合适的 Camera.Size 之后会发现部分机型,比如小米Mix3支持的相机尺寸如下:

width:1920,height:1440
width:1920,height:1080
width:1600,height:1200
width:1280,height:960
width:1280,height:720
width:1280,height:640
width:800,height:600
width:720,height:480
width:640,height:480
width:640,height:360
width:352,height:288
width:320,height:240

但是它的屏幕尺寸是:2340 * 1080,这样获取的最合适的 Camera.Size 是 1920 * 1080,这是就会发现预览界面被拉长了,想象一下 1920 的高度放到 2340 的高度上肯定会被拉长,这个时候就需要对显示预览界面的 View 做一下缩放偏移 操作了,大概的代码如下:

public Matrix calculateSurfaceHolderTransform() {
        // 预览 View 的大小,比如 SurfaceView
        int viewHeight = configManager.getScreenResolution().y;
        int viewWidth = configManager.getScreenResolution().x;
        // 相机选择的预览尺寸
        int cameraHeight = configManager.getCameraResolution().x;
        int cameraWidth = configManager.getCameraResolution().y;
        // 计算出将相机的尺寸 => View 的尺寸需要的缩放倍数
        float ratioPreview = (float) cameraWidth / cameraHeight;
        float ratioView = (float) viewWidth / viewHeight;
        float scaleX, scaleY;
        if (ratioView < ratioPreview) {
            scaleX = ratioPreview / ratioView;
            scaleY = 1;
        } else {
            scaleX = 1;
            scaleY = ratioView / ratioPreview;
        }
        // 计算出 View 的偏移量
        float scaledWidth = viewWidth * scaleX;
        float scaledHeight = viewHeight * scaleY;
        float dx = (viewWidth - scaledWidth) / 2;
        float dy = (viewHeight - scaledHeight) / 2;
        
        Matrix matrix = new Matrix();
        matrix.postScale(scaleX, scaleY);
        matrix.postTranslate(dx, dy);

        return matrix;
    }    

如果你的预览 View 是 SurfaceView:

 Matrix matrix = CameraManager.get().calculateSurfaceHolderTransform();
            float[] values = new float[9];
            matrix.getValues(values);
            surfaceView.setTranslationX(values[Matrix.MTRANS_X]);
            surfaceView.setTranslationY(values[Matrix.MTRANS_Y]);
            surfaceView.setScaleX(values[Matrix.MSCALE_X]);
            surfaceView.setScaleY(values[Matrix.MSCALE_Y]);
            surfaceView.invalidate();

如果你的预览 View 是 TextureView 更加简单:

Matrix matrix = CameraManager.get().calculateSurfaceHolderTransform();
textureView.setTransform(matrix);

参考:
ZxingView

相关文章

网友评论

      本文标题:彻底解决 Android Camera 预览拉伸的问题

      本文链接:https://www.haomeiwen.com/subject/eqqyoqtx.html