android 摄像头比例,java – Android全屏摄像头 – 同时保持摄像头选择的比例

我们正在尝试构建类似于Instagram Camera屏幕的东西.即允许用户拍摄方形照片.在执行此操作时,U.i必须能够让用户在fullScreen模式下看到相机.我们希望强制用户以纵向模式拍摄图像

获得相机可能的比例

我们正在计算相机的最佳比例

private Camera.Size getOptimalPreviewSize(List 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;

}

setCameraLayout来电者

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());

}

}

目标

手机全屏摄像头预览.

问题

现在我们正在预览,没有失真,这是好的!它与手机的高度相同,也很好!但!预览的宽度大于手机的宽度(当然),因此事实证明相机的中心不在手机的中心.我们考虑过的可能解决方案:

>将布局向左移动到负位置,使预览中心位于屏幕中央.

>裁剪布局并仅绘制电话屏幕应该可见的新预览的中心

任何想法如何克服这个问题?

非常感谢! 🙂

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值