android 预览和拍照成像方向不一致,android 拍照 预览图与 照片分辨率(可视区域)不一致...

问题来源是来自项目自定义相机模块,问题出现在拍摄下的照片与预览的照片课时范围不一致,在测试手机上出现的具体情况是拍摄的照片可视宽度大于预览的画面。

问题出在 该自定义相机采用了全屏预览画面,以及虚拟按键占用了部分屏幕高度,预览分辨率的选择没有与相机匹配,导致最终拍照与预览不一致。

简单解决方式类似手机系统相机,将预览画面与相机拍摄画面维持同一分辨率比例,比如16:9,预览TextureView宽度拉满,预览高度自适应。

高适配的话需要根据屏幕分辨率,匹配最合适的预览分辨率/拍照分辨率。

寻找与当前屏幕分辨率最接近的 分辨比率

Set ratios = mCameraView.getSupportedAspectRatios();

float screenRatio = (float) screenheight / (float) screenWidth;

AspectRatio lastRatio = null;

for (AspectRatio ratio : ratios) {

if(lastRatio == null){

lastRatio = ratio;

continue;

}

float ratioXY = ratio.toFloat();

if (Math.abs(ratioXY-screenRatio)

lastRatio = ratio;

}

}

if (lastRatio != null) {

mCameraView.setAspectRatio(lastRatio);

}

获取支持的预览分辨率(排除与可拍照不同的size)

Set getSupportedAspectRatios() {

SizeMap idealAsp

android调用camera时,可以自己写一个activity,赋上相关参数,打开前camera就可以了; 需要申请的permission,在AndroidManifest.xml中添加: 主要功能,打开前camera private Camera openFrontFacingCameraGingerbread() { int cameraCount = 0; Camera cam = null; Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); cameraCount = Camera.getNumberOfCameras(); for (int camIdx = 0; camIdx < cameraCount; camIdx++) { Camera.getCameraInfo(camIdx, cameraInfo); if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { try { cam = Camera.open(camIdx); mCurrentCamIndex = camIdx; } catch (RuntimeException e) { Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage()); } } } return cam; } 根据打开时的横竖屏方向来调整preview角度 //根据横竖屏自动调节preview方向,Starting from API level 14, this method can be called when preview is active. private static void setCameraDisplayOrientation(Activity activity,int cameraId, Camera camera) { Camera.CameraInfo info = new Camera.CameraInfo(); Camera.getCameraInfo(cameraId, info); int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); //degrees the angle that the picture will be rotated clockwise. Valid values are 0, 90, 180, and 270. //The starting position is 0 (landscape). int degrees = 0; switch (rotation) { case Surface.ROTATION_0: degrees = 0; break; case Surface.ROTATION_90: degrees = 90; break; case Surface.ROTATION_180: degrees = 180; break; case Surface.ROTATION_270: degrees = 270; break; } int result; if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { result = (info.orientation + degrees) % 360; result = (360 - result) % 360; // compensate the mirror } else { // back-facing result = (info.orientation - degrees + 360) % 360; } camera.setDisplayOrientation(result); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值