首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android Camera2 API扩展预览

Android Camera2 API扩展预览
EN

Stack Overflow用户
提问于 2017-01-20 06:15:15
回答 1查看 2.1K关注 0票数 1

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

代码语言:javascript
复制
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类上物理改变宽高比,这会使它全屏显示,但会导致它拉伸。

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

EN

回答 1

Stack Overflow用户

发布于 2017-06-02 16:17:11

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

previously--->

代码语言:javascript
复制
   Size largest = Collections.max(
                        Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)),
                        new CompareSizesByArea());

已修改->

代码语言:javascript
复制
largest =getFullScreenPreview(map.getOutputSizes(ImageFormat.JPEG),width,height);

previously--->

代码语言:javascript
复制
mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class),
                    rotatedPreviewWidth, rotatedPreviewHeight, maxPreviewWidth,
                    maxPreviewHeight, largest);

modified---->

代码语言:javascript
复制
  mPreviewSize = getFullScreenPreview(map.getOutputSizes(SurfaceTexture.class),
                    width, height);

获得全屏预览的方法如下:

代码语言:javascript
复制
 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;
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41752733

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档