我读到你需要获取参数.getSupportedPreviewSizes()和parameters.getSupportedPictureSizes()所以我这样做了
Camera.Parameters pic = mCamera.getParameters();
List<Size> pic2 = pic.getSupportedPictureSizes();
Camera.Parameters p = mCamera.getParameters();
List<Size> previews = p.getSupportedPreviewSizes();
Size preview = getOptimalPreviewSize(previews, pic2.get(0).width,pic2.get(0).height);
用此来获得最佳预览
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.05;
double targetRatio = (double) w / h;
if (sizes == null) return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
// Try to find an size match aspect ratio and size
for (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);
}
}
// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
然后
mFrameLayout.getLayoutParams().height = preview.height;
mFrameLayout.getLayoutParams().width = preview.width;
mCamera.setParameters(parameters);
mCamera.setDisplayOrientation(90);
mFrameLayout.addView(mPreview);
我希望得到与Camera Intent类似的预览,其自定义视图类似于instagram,但这显示了一个展平为平面的矩形。有没有办法获得默认相机的预览和图片大小,并将其设置为自定义相机?
出于某种原因,当我拍照时,左侧和右侧保存的图片中的内容比您在预览版中看到的内容更多,它看起来像是缩放但是检查了缩放,它是0。任何提示如何解决这个问题?
还有一个问题,您可以在布局上使用相机意图而不是打开默认相机吗?