java - Android Camera2 API 拉伸(stretch)预览

标签 java android android-camera2

我正在使用 Google 示例项目,但我似乎无法在不拉伸(stretch)的情况下让预览工作。

public void setAspectRatio(int width, int height) {
     if (width < 0 || height < 0) 
     {
        throw new IllegalArgumentException("Size cannot be negative.");
     }
     mRatioWidth =  width;
     mRatioHeight = height;
     requestLayout();
}

我已经尝试通过物理方式更改 AutoFitTextureView 类的纵横比,这会使其全屏显示,但会使其拉伸(stretch)。

有没有人想出一个成功的实现方法?

最佳答案

需要修改setUpCameraOutputs方法。修改以下行

以前--->

   Size largest = Collections.max(
                        Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)),
                        new CompareSizesByArea());

已修改--->

largest =getFullScreenPreview(map.getOutputSizes(ImageFormat.JPEG),width,height);

以前--->

mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class),
                    rotatedPreviewWidth, rotatedPreviewHeight, maxPreviewWidth,
                    maxPreviewHeight, largest);

已修改---->

  mPreviewSize = getFullScreenPreview(map.getOutputSizes(SurfaceTexture.class),
                    width, height);

获取全屏预览的方法如下-

 private Size getFullScreenPreview(Size[] outputSizes, int width, int height) {

        List<Size> outputSizeList = Arrays.asList(outputSizes);
        outputSizeList = sortListInDescendingOrder(outputSizeList); //because in some phones available list is in ascending order
        Size fullScreenSize = outputSizeList.get(0);
        for (int i = 0; i < outputSizeList.size(); i++) {
            int orginalWidth = outputSizeList.get(i).getWidth();
            int orginalHeight = outputSizeList.get(i).getHeight();
            float orginalRatio = (float) orginalWidth / (float) orginalHeight;
            float requiredRatio;
            if (width > height) {
                requiredRatio = ((float) width / height); //for landscape mode
                if ((outputSizeList.get(i).getWidth() > width && outputSizeList.get(i).getHeight() > height)) {
                    // because if we select preview size hire than device display resolution it may fail to create capture request
                    continue;
                }
            } else {
                requiredRatio = 1 / ((float) width / height); //for portrait mode
                if ((outputSizeList.get(i).getWidth() > height && outputSizeList.get(i).getHeight() > width)) {
                    // because if we select preview size hire than device display resolution it may fail to create capture request
                    continue;
                }
            }
            if (orginalRatio == requiredRatio) {
                fullScreenSize = outputSizeList.get(i);
                break;
            }
        }
        return fullScreenSize;
    }

关于java - Android Camera2 API 拉伸(stretch)预览,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41752733/

相关文章:

javascript - 如何添加到Android移动设备的 'Add to homescreen'弹出窗口

android - 如何检查另一个应用程序是否在 android 中最小化或打开?

android - session 已关闭;进一步的改变是非法的

java - 如何使 Jersey @RolesAllowed 与 @Stateless 一起工作?

java - 在这种情况下使用方法引用有什么好处?

android - NotificationListenerService 获取通知图标?

android - 拍摄的 Camera2 API 图片不会显示在图库应用程序中

android - 使用 android camera2 在相机预览中显示矩形并在其中裁剪图像

java - Double 不能取消引用 [Arraylist]

java - 如何清理重复的 if 语句