三星拍照后,图片竟然是旋转的,众多手机唯有三星拍照是旋转的,但是你又不能不处理。

首先你要先判断这张图片有木有旋转,图片都是自带这些信息的,方法如下:

 /*** 读取图片属性:旋转的角度** @param path 图片绝对路径* @return degree旋转的角度*/public static int readPictureDegree(String path) {int degree = 0;try {ExifInterface exifInterface = new ExifInterface(path);int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL);switch (orientation) {case ExifInterface.ORIENTATION_ROTATE_90:degree = 90;break;case ExifInterface.ORIENTATION_ROTATE_180:degree = 180;break;case ExifInterface.ORIENTATION_ROTATE_270:degree = 270;break;}} catch (IOException e) {e.printStackTrace();}return degree;}

当然三星手机拍照一般都是:

degree = 90;

如果degree != 0 ,就说明图片是旋转的。假设我们需要发送一张图片,不可能对原来路径下的图片进行修改,所以需要把要处理的图片在复制一份再进行处理;方法如下:

/*** 复制文件** @param oldPath* @param newPath*/public static void copyFile(String oldPath, String newPath) {try {int bytesum = 0;int byteread = 0;File oldfile = new File(oldPath);if (oldfile.exists()) { //文件存在时InputStream inStream = new FileInputStream(oldPath); //读入原文件FileOutputStream fs = new FileOutputStream(newPath);byte[] buffer = new byte[1444];int length;while ((byteread = inStream.read(buffer)) != -1) {bytesum += byteread; //字节数 文件大小System.out.println(bytesum);fs.write(buffer, 0, byteread);}inStream.close();}} catch (Exception e) {System.out.println("复制单个文件操作出错");e.printStackTrace();}}

最后再对相应的图片做旋转处理,先贴上图片旋转的处理方法:

 /*** 旋转图片** @param angle* @param bitmap* @return Bitmap*/public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {// 旋转图片 动作Matrix matrix = new Matrix();matrix.postRotate(angle);// 创建新的图片Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,bitmap.getWidth(), bitmap.getHeight(), matrix, true);return resizedBitmap;}

这里传入相应的角度和bitmap就行,最后贴上图片旋转处理的全部代码:

/*** 旋转的图片生成正的** @param path*/public static void rotaingDegreeImage(String path) {BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();//获取宽高度bitmapOptions.inJustDecodeBounds = true;BitmapFactory.decodeFile(path, bitmapOptions);/*** 计算sampleSize最优值*/bitmapOptions.inSampleSize = computeSampleSize(bitmapOptions, -1, 2048 * 1536);bitmapOptions.inJustDecodeBounds = false;int degree = readPictureDegree(path);Bitmap cameraBitmap = BitmapFactory.decodeFile(path, bitmapOptions);/*** 把图片旋转为正的方向*/cameraBitmap = rotaingImageView(degree, cameraBitmap);FileOutputStream out;try {out = new FileOutputStream(path);cameraBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}

这里还用到:

computeSampleSize()
 /*** 计算sampleSize最优值** @param options* @param minSideLength* @param maxNumOfPixels* @return*/public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);int roundedSize;if (initialSize <= 8) {roundedSize = 1;while (roundedSize < initialSize) {roundedSize <<= 1;}} else {roundedSize = (initialSize + 7) / 8 * 8;}return roundedSize;}

中间调用:

 private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {double w = options.outWidth;double h = options.outHeight;int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));if (upperBound < lowerBound) {// return the larger one when there is no overlapping zone.return lowerBound;}if ((maxNumOfPixels == -1) && (minSideLength == -1)) {return 1;} else if (minSideLength == -1) {return lowerBound;} else {return upperBound;}}

有了这几个方法。一切OK。

Android 三星手机拍照图片旋转处理相关推荐

  1. Android三星手机拍照自动旋转问题解决方案

    Android 小米8SE,三星手机拍照,从图库选择照片旋转问题完美解决 目前测试机有个小米8SE,发现获取照片会有旋转的问题,之前有三星手机有这个问题,现在添加上小米8SE,小米8据说也有这个问题, ...

  2. Android开发拍照图片旋转

    在做照相机图片相关操作的时候,由于android手机的适配原因,不同手机上出发的相机操作可能在细节上有很多不同,例如摄像头拍照的角度旋转了,使得获取到的图片也是旋转后的,再比如某些相机的图片像素太高了 ...

  3. Android 三星手机拍照,从图库选择照片旋转问题完美解决

    好久没有写博客了,最近解决了一个令我头疼好久的问题,就是三星手机拍照图片旋转的问题,项目中有上传图片的功能,那么涉及到拍照,从相册中选择图片,别的手机都ok没有问题,唯独三星的手机拍照之后,你会很清楚 ...

  4. Android 7.0 获取相机拍照图片,适配三星手机拍照,解决三星手机拍照屏幕旋转,判断设备是否有摄像头

    方法1 新建/res/xml/file_paths: <?xml version="1.0" encoding="utf-8"?> <path ...

  5. MVP模式的Android 调用系统拍照,相册,剪裁,适配到7.0,修复拍照图片旋转问题

    Android 调用系统拍照,相册,剪裁,适配到7.0,修复拍照图片旋转问题 直接上代码 首先添加拍照需要的权限 <uses-permission android:name="andr ...

  6. 解决ionic在手机上拍照图片旋转的问题

    解决ionic在手机上拍照图片旋转的问题 拍照或者从相册中选择图片,上传到服务器后,图片的方向是不对的 无论是拍照,还是从相册中选择图片都是可用的. correctOrientation:trueco ...

  7. android 三星手机拍照旋转90度,解决三星调用系统相机拍照显示图片旋转90度横着的问题...

    /** * 调用系统相机拍照工具类 * @author yao * */ public class CaremaUtil { private static String strImgPath = &q ...

  8. Android camera onPreviewFrame 图片旋转问题

     Android开发中调用到摄像头camera及截图.通过测试发现,调用前置摄像头的预览图和截图.调用后置摄像头的预览图和截图,参数是不一样的. 调用前置摄像头: camera = Camera. ...

  9. android 点击图片旋转90度,Android UI之ImageView实现图片旋转和缩放

    这一篇,给大家介绍一下ImageView控件的使用,ImageView主要是用来显示图片,可以对图片进行放大.缩小.旋转的功能. android:sacleType属性指定ImageVIew控件显示图 ...

最新文章

  1. 深入.NET DataTable
  2. 删除空值_空白单元格行,如何快速批量删除?简单方法,效率飞升
  3. erlang web socket参考。
  4. 互联网业务利润增长3倍,TCL电子走出第二增长曲线
  5. bootstrap思考一
  6. npm执行命令后无任何响应(windows下)
  7. unity基础知识笔记一(快捷方式、基础概念)
  8. 2017.9.23 循环格 思考记录
  9. CSV Converter Pro for Mac(CSV数据转换工具)
  10. Requirement-Driven Linux Shell Programming
  11. 行政区划信息抽取算法(区划抽取)
  12. VFP命令,DBF数据内部函数
  13. scp 命令简明介绍
  14. 从两张Excel表格中筛选相同的值
  15. 现代大学英语精读第二版(第四册)学习笔记(原文及全文翻译)——16B - Is Everybody Happy?(人人都幸福吗?)
  16. 淘宝店铺宝贝转化率该如何提升
  17. 京东区块链开源项目——JD Chain介绍及区块链白皮书发布
  18. 计算机二级考试地点没有容量,2017年计算机二级office考试点积累
  19. 硬件设备PS/2指的是什么?都有什么用途?
  20. 礼堂椅影院椅安装步骤方法

热门文章

  1. 格式化什么意思?格式化了数据还能恢复吗?
  2. 如何设计一个混沌工程实验?
  3. elastica安装
  4. uniapp实现瀑布流懒加载实现和无限上拉加载更多
  5. 如何评价一起嗨聊 APP?
  6. 服务器主板显示ff,主板诊断卡跑FF的原因及检查思路
  7. 免费PPT模板网站,模板精品好用,直接下载
  8. 药剂师揭露中药行业内幕:代煎多偷工减料
  9. 官网显示500内部服务器有错误代码,【500错误】http 500 - 内部服务器错误(错误代码500)解决方法...
  10. WPF翻盘连连看(二)