我读到您需要获取 parameters.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);
我希望获得与具有类似于 instagram 的自定义视图的相机意图类似的预览,但这显示了一个扁平化为预览的矩形。有没有办法获取默认相机的预览和图片大小并将其设置为自定义相机?
出于某种原因,当我拍照时,左侧和右侧保存的图片中的内容比您在预览布局中看到的内容更多,它看起来像已缩放但检查了缩放并且它为 0。有关如何修复的任何提示这个?
只是另一个问题,您可以在布局上使用相机意图而不是打开默认相机吗?