Android 三星手机拍照图片旋转处理
来源:互联网 发布:通联数据股份公司 深圳 编辑:程序博客网 时间:2024/04/19 16:54
三星拍照后,图片竟然是旋转的,众多手机唯有三星拍照是旋转的,但是你又不能不处理。
首先你要先判断这张图片有木有旋转,图片都是自带这些信息的,方法如下:
/** * 读取图片属性:旋转的角度 * * @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 block e.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。
阅读全文
0 0
- Android 三星手机拍照图片旋转处理
- 解决三星手机拍照后,图片旋转。
- Android解决三星手机拍照图片调用时逆时针旋转90度问题
- 三星手机拍照,图片旋转问题的解决
- 三星手机拍照旋转问题
- 关于三星手机拍照 获取的照片旋转处理
- Android解决三星手机图片旋转问题
- android 三星手机拍照裁剪后会旋转问题
- Android三星手机拍照自动旋转问题解决方案
- android三星等手机拍照自动旋转问题与解决
- android关于手机拍照图片旋转问题
- android解决三星手机拍照获取图片的问题
- Android开发中三星手机拍照异常处理
- android开发三星手机拍照
- Android:Camera的使用,并处理手机拍照后上传图片被旋转的问题
- 三星手机拍照,上传照片或者下载导致图片旋转90°的解决办法。
- 三星手机拍照后显示图片角度旋转问题的解决方法
- 关于三星手机调用系统相机拍照旋转屏导致图片数据丢失问题
- UDP协议发送数据:UDP协议接收数据:UDP协议收发数据注意事项
- 浏览器报Uncaught ReferenceError: require is not defined
- ImportantError:/usr/local/lib/python2.7/dist-packages/gnuradio/uhd/_uhd_swig.so:undefined symbol:_ZN
- switch语句
- MEF第三代网络白皮书摘要
- Android 三星手机拍照图片旋转处理
- git学习
- EXCEL导入 导出
- 处理uri %乱码
- Oracle教程____oracle给予rownum分页的sql语句
- Integer.valueOf(String) 方法之惑
- 自定义锁管理器
- 改变返回值
- TCP协议发送数据:TCP 协议接收端(服务端)数据,