我们正在尝试构建类似于Instagram
相机屏幕的内容。即允许用户拍摄square
张照片。在执行此操作时,U.i必须能够让用户在fullScreen
模式下看到相机。我们希望强制用户以portrait
模式拍摄图像
我们正在计算best
提供的camera
比例
private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) h / w;
if (sizes == null) {
return null;
}
Camera.Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
for (Camera.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);
}
}
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Camera.Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
public FrameLayout setCameraLayout(int width, int height) {
float newProportion = (float) width / (float) height;
// Get the width of the screen
int screenWidth = this.customCameraActivity.getWindowManager().getDefaultDisplay()
.getWidth();
int screenHeight = this.customCameraActivity.getWindowManager().getDefaultDisplay()
.getHeight();
float screenProportion = (float) screenWidth / (float) screenHeight;
// Get the SurfaceView layout parameters
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams)preview.getLayoutParams();
if (newProportion > screenProportion) {
lp.width = screenWidth;
lp.height = (int) ((float) screenWidth / newProportion);
} else {
lp.width = (int) (newProportion * (float) screenHeight);
lp.height = screenHeight;
}
//calculate the amount that takes to make it full screen (in the `height` parameter)
float propHeight = screenHeight / lp.height;
//make it full screen(
lp.width = (int)(lp.width * propHeight);
lp.height = (int)(lp.height * propHeight);
frameLayout.setLayoutParams(lp);
return frameLayout;
}
OnCreate
及之后Activity
的 surfaceChanged
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
if (getHolder().getSurface() == null) {
return;
}
try {
cameraManager.getCamera().stopPreview();
} catch (Exception e) {
// tried to stop a non-existent preview
}
try {
Camera.Parameters cameraSettings = cameraManager.getCamera().getParameters();
cameraSettings.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
this.cameraView.setCameraLayout(mPreviewSize.width, mPreviewSize.height);
cameraManager.getCamera().setParameters(cameraSettings);
cameraManager.getCamera().setPreviewDisplay(holder);
cameraManager.getCamera().startPreview();
} catch (Exception e) {
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
手机上的全屏相机预览。
现在我们正在进行没有失真的预览 GOOD!,它与height
具有相同的phone
,这也是 GOOD! 即可。 但是! preview
的宽度大于phone width
(当然),所以事实证明相机的中心不在手机的中心。我们考虑过的可能解决方案:
move
布局左侧为负位置,使预览中心位于屏幕中央。crop
布局并仅绘制new preview
phone screens
的中心
醇>
非常感谢! :)
答案 0 :(得分:6)
我通过设置SurfaceView的重力来成功地使预览居中:
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(wid, hei);
lp.gravity = Gravity.CENTER;
mSv.setLayoutParams(lp);
代码段取自here,如果您有耐心解密它,欢迎您使用该代码。我想我正在解决类似的问题(预览旋转,扭曲,装入/填充)
祝你好运。
答案 1 :(得分:1)
如果我从代码片段中理解,您每次用户更改方向或旋转相机时都会重新实例化相机。有什么理由这样做吗?
为什么不将预览限制为仅限纵向并且具有旋转的重叠视图?我很确定这是其他人(Instagram,Snapchat等)所做的。如果我能找到它,我会用一个例子进行编辑。