如何在Android相机中交换预览大小的宽度和高度?

时间:2015-08-03 12:38:02

标签: android camera android-camera

我们可以通过指定预览大小打开Android摄像头,例如:

mParameter.setPreviewSize(640, 480);
mCamera.setParameter(mParameter);

上述比率为1.3333333333333333

有没有办法更换宽度和高度?也就是说这样的效果:

mParameter.setPreviewSize(480, 640);
mCamera.setParameter(mParameter);

该比率为0.75

1 个答案:

答案 0 :(得分:0)

并非所有设备都支持所有视频预览尺寸。您只能以编程方式执行此操作。 如果尺寸不适用,则抛出介质记录器停止-19错误

int width = 320;
            int height = 240;
            try {
                //get the available sizes of the video
                List<Size> tmpList = getSupportedVideoSizes();

                final List<Size> sizeList = new Vector<Size>();

                // compare the apsect ratio of the candidate sizes against the
                // real ratio
                Double aspectRatio = (Double.valueOf(getWindowManager()
                        .getDefaultDisplay().getHeight()) / getWindowManager()
                        .getDefaultDisplay().getWidth());
                for (int i = tmpList.size() - 1; i > 0; i--) {
                    Double tmpRatio = Double.valueOf(tmpList.get(i).height)
                            / tmpList.get(i).width;
                    if (EnableLog.LOG_TAG) {
                        Log.e("Width & height", tmpList.get(i).width + " x "
                                + tmpList.get(i).height);
                    }
                    if (Math.abs(aspectRatio - tmpRatio) < .15) {
                        width = tmpList.get(i).width;
                        height = tmpList.get(i).height;
                        sizeList.add(tmpList.get(i));
                    }
                }
                if (EnableLog.LOG_TAG) {
                    Log.e("tmpList", tmpList + "*");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }


            // set the size of video.
            // If the size is not applicable then throw the media recorder stop
            // -19 error
            mrec.setVideoSize(width, height);

/**
     * This method will return the size of the video
     * 
     * if the getSupportedVideoSizes() is return null
     * 
     * then it is possible to get the size of video from
     * getSupportedPreviewSizes()
     * 
     * @return list of size
     */
    public List<Size> getSupportedVideoSizes() {
            if (params.getSupportedVideoSizes() != null) {
                return params.getSupportedVideoSizes();
            } else {
                // Video sizes may be null, which indicates that all the supported
                // preview sizes are supported for video recording.
                return params.getSupportedPreviewSizes();
            }
        }

试试这段代码可能会有所帮助。

修改

根据您的评论,您希望设置最佳预览大小,因此您需要更改for循环。我将向您展示示例

将此更改为循环

for (int i = tmpList.size() - 1; i > 0; i--) {
}

for (int i = 0 ; i < tmpList.size()-1; i++) {
}

这将为您提供更好的预览尺寸。