方法一:
/** * 通过对比得到与宽高比最接近的尺寸(如果有相同尺寸,优先选择) * * @param surfaceWidth * 需要被进行对比的原宽 * @param surfaceHeight * 需要被进行对比的原高 * @param preSizeList * 需要对比的预览尺寸列表 * @return 得到与原宽高比例最接近的尺寸 */ protected Camera.Size getCloselyPreSize(int surfaceWidth, int surfaceHeight, List<Camera.Size> preSizeList) { int ReqTmpWidth; int ReqTmpHeight; // 当屏幕为垂直的时候需要把宽高值进行调换,保证宽大于高 // if (mIsPortrait) { // ReqTmpWidth = surfaceHeight; // ReqTmpHeight = surfaceWidth; // } else { ReqTmpWidth = surfaceWidth; ReqTmpHeight = surfaceHeight; // } //先查找preview中是否存在与surfaceview相同宽高的尺寸 for(Camera.Size size : preSizeList){ if((size.width == ReqTmpWidth) && (size.height == ReqTmpHeight)){ return size; } } // 得到与传入的宽高比最接近的size float reqRatio = ((float) ReqTmpWidth) / ReqTmpHeight; float curRatio, deltaRatio; float deltaRatioMin = Float.MAX_VALUE; Camera.Size retSize = null; for (Camera.Size size : preSizeList) { curRatio = ((float) size.width) / size.height; deltaRatio = Math.abs(reqRatio - curRatio); if (deltaRatio < deltaRatioMin) { deltaRatioMin = deltaRatio; retSize = size; } } return retSize; }调用:
List<Camera.Size> mSupportedPreviewSizes = mCamera.getParameters().getSupportedPictureSizes(); Camera.Size bestSize = getCloselyPreSize(1000,740,mSupportedPreviewSizes); mWidth = bestSize.width; mHeight = bestSize.height;
获取到的尺寸虽然不怎么变形,但很模糊。
方法二:
private int getPictureSize(List<Camera.Size> sizes) { // 屏幕的宽度 int screenWidth = getResources().getDisplayMetrics().widthPixels; LogUtil.d(TAG, "screenWidth=" + screenWidth); int index = -1; for (int i = 0; i < sizes.size(); i++) { if (screenWidth == sizes.get(i).width) { index = i; } } // 当未找到与手机分辨率相等的数值,取列表中间的分辨率 if (index == -1) { index = sizes.size() / 2; } return index; }放在宽度为1000,高度为740的surfaceView里,会变形。