android camera颠倒,android开发步步为营之112:关于Camera镜像上下左右颠倒问题的解决办法...
发布日期:2021-06-24 11:27:39 浏览次数:2 分类:技术文章

本文共 2486 字,大约阅读时间需要 8 分钟。

我们在做自定义相机开发的过程中,一般会使用SurfaceView或者GlSurfaceView做预览,预览或拍照的时候经常会碰到镜像的左右颠倒或者上下颠倒?这个该怎么处理,这里给出解决方案。

一、拍照照片镜像左右颠倒,使用Matrix来设置

Matrix m = new Matrix();

m.postScale(-1, 1); // 镜像水平翻转

bmpPreview = Bitmap.createBitmap(bmpPreview, 0, 0, bmpPreview.getWidth(), bmpPreview.getHeight(), m, true);

二、镜像上下颠倒,需要设置相机预览的显示方向

public static void setCameraDisplayOrientation(Activity activity,

int cameraId, android.hardware.Camera camera) {

try {

android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();

android.hardware.Camera.getCameraInfo(cameraId, info);

int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();

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

} catch (Exception e) {

CommonUtil.printStackTrace(e);

}

}

拍完照后还得旋转图片

Matrix matrix = new Matrix();

matrix.postRotate(CamParaUtil.getCameraDisplayOrientation(StickerMakeActivity.this, mCameraId, mCamera));

matrix.postScale(scale, isCameraFront() ? -scale : scale);

Bitmap cropRotateScaled = Bitmap.createBitmap(origin, (int) resultRect.left, (int) resultRect.top, (int) resultRect.width(), (int) resultRect.height(), matrix, true);

public static int getCameraDisplayOrientation(Activity activity,

int cameraId, android.hardware.Camera camera) {

int result = 90;

try {

android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();

android.hardware.Camera.getCameraInfo(cameraId, info);

int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();

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;

}

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;

}

} catch (Exception e) {

CommonUtil.printStackTrace(e);

}

return result;

}

转载地址:https://blog.csdn.net/weixin_32497403/article/details/117698577 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:雨滴桌面显示html,如何用雨滴桌面设置美观的桌面天气插件
下一篇:linux sed 正则转义,转义字符Escape character在正则中的用法

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月06日 08时21分11秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

【leetcode之旅】 数组 - 169.求众数 2019-04-28
【leetcode之旅】 数组 - 219. 存在重复元素II 2019-04-28
【spring】校验器@NotNull、@NotEmpty和@NotBlank的区别 2019-04-28
【Hibernate】hibernate-validation验证框架常见注解 2019-04-28
【leetcode之旅】 数组 - 268.缺少数字 2019-04-28
【leetcode之旅】 数组 - 283.移动零 2019-04-28
【leetcode之旅】 数组 - 414.第三大的数 2019-04-28
【leetcode之旅】 数组 - 448.找出所有数组消失的数 2019-04-28
【leetcode之旅】 数组 - 485.最大连续1的个数 2019-04-28
【leetcode之旅】 数组 - 561.数组拆分I 2019-04-28
Android面试必问!我的移动开发春季历程,大厂内部资料 2019-04-28
Android面试送分题:来看看移动端小程序技术的前世今生!附赠课程+题库 2019-04-28
Android面试题整理,46道面试题带你了解中高级Android面试,顺利通过阿里Android岗面试 2019-04-28
GitHub重磅官宣!Android程序员面试必备的知识点,经典好文 2019-04-28
上海大厂Android面试经历:Android多线程实现方式及并发与同步,年薪超过80万! 2019-04-28
从入门到精通!已成功拿下字节、腾讯、脉脉offer,看看这篇文章吧! 2019-04-28
金九银十Android热点知识!如何快速的开发一个完整的直播app,内含福利 2019-04-28
金九银十Android热点知识!字节跳动移动架构师学习笔记,面试真题解析 2019-04-28
阿里P7亲自教你!34岁安卓开发大叔感慨,Android面试题及解析 2019-04-28
阿里P7大佬手把手教你!系统盘点Android开发者必须掌握的知识点,系列篇 2019-04-28