Androidcamera2API在Google示例代码中图片旋转错误的问题?
创始人
2024-10-06 15:21:33
0

在使用camera2 API拍照后,生成的图片可能会由于旋转而出现不正确的方向。这通常是因为设备的方向不同于预期的方向所导致的。为了解决这个问题,可以按照以下步骤更改Google示例代码:

  1. 在onImageAvailable()方法中,获取图片的方向信息并保存:

int rotation = getActivity().getWindowManager().getDefaultDisplay().getRotation(); mImageFile.mOrientation = ORIENTATIONS.get(rotation);

  1. 将保存方向信息的变量传入ImageSaver类中:

private static class ImageSaver implements Runnable { ... private final String mImageFile; private final int mOrientation;

public ImageSaver(Bitmap bitmap, String imageFile, int orientation) { .... mImageFile = imageFile; mOrientation = orientation; }

  1. 在保存图片之前,使用Matrix类将图片旋转回正确的方向:

if(mOrientation != 0) { Matrix matrix = new Matrix(); matrix.postRotate(mOrientation);

Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); out = new FileOutputStream(mImageFile); rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); } else { out = new FileOutputStream(mImageFile); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); }

  1. 这样,即可确保图片保存到本地的方向正确。

相关内容

热门资讯

推荐攻略!微扑克系统发牌规律,... 推荐攻略!微扑克系统发牌规律,微扑克伙牌技巧,必胜教程(有挂透视);小薇(透视辅助)致您一封信;亲爱...
玩家必用!wepoke辅助,w... 玩家必用!wepoke辅助,wepoke app,曝光教程(有挂技巧);亲真的是有正版授权,小编(透...
透视辅助!aapoker辅助器... 透视辅助!aapoker辅助器怎么用,wepoker破解器(详细辅助透牌教程)1、aapoker辅助...
查到实测!gg扑克软件(wep... 查到实测!gg扑克软件(wepoke智能ai)好像存在有挂(详细辅助插件教程)是一款可以让一直输的玩...
透视教程!德普之星辅助器app... 透视教程!德普之星辅助器app,德州局怎么透视(详细辅助科技教程)1、金币登录送、破产送、升级送、活...
三分钟了解!微扑克辅助测试(w... 1、三分钟了解!微扑克辅助测试(wepoke辅助德之星)真是有挂(详细辅助2025新版技巧);详细教...
透视辅助!德普之星透视辅助插件... 透视辅助!德普之星透视辅助插件,德扑圈透视挂(详细辅助黑科技教程)1、下载好德普之星透视辅助插件辅助...
透视辅助!wepoker辅助器... 透视辅助!wepoker辅助器怎么用,wepoker买脚本靠谱吗(详细辅助wpk教程)1、该软件可以...
玩家爆料!wpk用什么辅助器(... 《玩家爆料!wpk用什么辅助器(aapoker辅助)都是有挂(详细辅助教你攻略)》 wpk用什么辅助...
透视安装!wpk透视表,new... 透视安装!wpk透视表,newpoker怎么安装脚本(详细辅助规律教程)1、wpk透视表透视辅助简单...