解决三星手机拍照后,图片旋转。
来源:互联网 发布:淘宝服装模特名字 编辑:程序博客网 时间:2024/04/26 13:02
<span style="font-size:18px;">最近项目中遇到用三星手机拍照,图片会自动旋转,应该是三星内部系统的功能,然后需要是不让他旋转,找到了方法。原理就是,获取到图片,判断它的旋转角度,然后相应的旋转回来。在拍照的返回结果中,获取到图片的路径。即activityforresult方法中。 path = filePath + fileName; //path 为拍照返回的路径 File file = new File(path); int degree = readPictureDegree(file.getAbsolutePath()); Bitmap smallBitmap=FileUtils.compressSize(path, 800, 800); smallBitmap = rotaingImageView(degree, smallBitmap); saveAllPath = FileUtils.saveBitmap(smallBitmap, System.currentTimeMillis() +(Math.random() * 10000) + ".png");//质量压缩并存储得到pathImageItem takePhoto = new ImageItem();takePhoto.setBitmap(smallBitmap);takePhoto.setImagePath(saveAllPath);Bimp.tempSelectBitmap.add(takePhoto);/** * 尺寸压缩 * @param path 图片绝对路径 * @return degree旋转的角度 */ public static Bitmap compressSize(final String path,final int w,final int h){ //Looper.prepare(); // TODO Auto-generated method stub BitmapFactory.Options opts = new BitmapFactory.Options(); // 设置为ture只获取图片大小 opts.inJustDecodeBounds = true;//只读边,不读内容 opts.inPreferredConfig = Bitmap.Config.ARGB_8888; // 返回为空 BitmapFactory.decodeFile(path, opts); int width = opts.outWidth; int height = opts.outHeight; // 判断后缀名 String suffix = ""; CompressFormat format = null; if (path.endsWith(".jpg")) { suffix = ".jpg"; format = CompressFormat.JPEG; } else if (path.endsWith(".jpeg")) { suffix = ".jpeg"; format = CompressFormat.JPEG; } else if (path.endsWith(".png")) { suffix = ".png"; format = CompressFormat.PNG; } else { suffix = ".jpg"; format = CompressFormat.JPEG; } float scaleWidth = 0.f, scaleHeight = 0.f; if (width > w || height > h) {//如果宽度大于 传入的宽度 或者 高度大于 传入的高度大于 // 缩放 scaleWidth = ((float) width) / w; scaleHeight = ((float) height) / h; } opts.inJustDecodeBounds = false; //缩放后的高度和宽度取最大值 float scale = Math.max(scaleWidth, scaleHeight); opts.inSampleSize = (int) scale;//此处是最后的宽高值 Bitmap bMapRotate = BitmapFactory.decodeFile(path, opts); if (bMapRotate!=null) {return bMapRotate;} return null; } /** * 读取图片属性:旋转的角度 * @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; } /* * * 旋转图片 * */ public static Bitmap rotaingImageView(int angle , Bitmap bitmap) { //旋转图片 动作 Matrix matrix = new Matrix(); ; matrix.postRotate(angle); System.out.println("angle2=" + angle); // 创建新的图片 Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); return resizedBitmap; } /** * 存储图片,返回路径 * @param bm * @param picName * @return */ public static String saveBitmap(Bitmap bm, String picName) { try { if (!isFileExist(picName)) { File tempf = createSDDir(""); } File f = new File(SDPATH, picName); if (f.exists()) { f.delete(); } FileOutputStream out = new FileOutputStream(f); bm.compress(Bitmap.CompressFormat.PNG, 100, out); out.flush(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return SDPATH + picName; }</span>
0 0
- 解决三星手机拍照后,图片旋转。
- 三星手机拍照,图片旋转问题的解决
- 解决三星手机拍照后裁剪图片时横屏问题
- Android 三星手机拍照图片旋转处理
- 三星手机拍照后 图片翻转
- 三星手机拍照后显示图片角度旋转问题的解决方法
- Android解决三星手机拍照图片调用时逆时针旋转90度问题
- android 三星手机拍照裁剪后会旋转问题
- Android解决三星手机图片旋转问题
- 三星手机拍照旋转问题
- android三星等手机拍照自动旋转问题与解决
- 解决三星手机拍照旋转导致崩溃的问题
- android拍照后图片路径的获取(解决不同手机存在拍照后旋转的问题)
- android解决三星手机拍照获取图片的问题
- 解决android有的手机拍照后上传图片被旋转的问题
- 解决android有的手机拍照后上传图片被旋转的问题 .
- 解决android有的手机拍照后上传图片被旋转的问题
- android学习笔记----解决android有的手机拍照后上传图片被旋转的问题
- Hive
- Codeforces Round #350 (Div. 2)(D)优先队列
- metaq实例
- Spring MVC拦截器
- HDU 2199 Can you solve this equation?
- 解决三星手机拍照后,图片旋转。
- shell切换用户
- Access SQL语句 格式化日期
- python版本、文件信息及时间获取函数实例
- C++输出流cout的执行顺序问题
- android 获取手机硬件imei信息
- java List转List Lists.transform
- VisualSVN Server提交前必须写信息(备注)
- hdu 4936 Rainbow Island (状压dp+高斯消元)