zoukankan      html  css  js  c++  java
  • android 照片旋转并保存

        /**
         * 读取图片属性:旋转的角度
         * @param path 图片绝对路径
         * @return degree旋转的角度
         */
        public 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;
        }
    
    
     /*
        * 旋转图片 
        * @param angle 
        * @param bitmap 
        * @return Bitmap 
        */ 
       public Bitmap rotaingImageView(int angle , Bitmap bitmap) {  
           //旋转图片 动作   
           Matrix matrix = new Matrix();
           matrix.postRotate(angle);
           // 创建新的图片   
           return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);  
       }
    
    
    /***
         * 将bitmap保存在SD jpeg格式
         * 
         * @param bitmap
         *            图片bitmap
         * @param filePath
         *            要保存图片路径
         * @param quality
         *            压缩质量值
         */
        public void saveImage(Bitmap bitmap, String filePath, int quality) {
            FileOutputStream fileOutputStream = null;
            try {
                fileOutputStream = new FileOutputStream(filePath);
                bitmap.compress(CompressFormat.JPEG, quality, fileOutputStream);
                fileOutputStream.flush();
                fileOutputStream.close();
                // 如果图片还没有回收,强制回收
                if (!bitmap.isRecycled()) {
                    bitmap.recycle();
                    System.gc();
                }
            } catch (Exception e) {
            }
        }
    
    
    bitmap=ImageCompress.decodeSampledBitmapFromResource(filename, 480, 800);
                        
                        //图片旋转
                        int Degree = imageUtils.readPictureDegree(filename);
                        bitmap = imageUtils.rotaingImageView(Degree, bitmap);
                        //保存图片到SD卡
                        //imageCompress.saveImage(bitmap, filename);
                        imageCompress.saveImage(bitmap, filename,90);
    View Code
    /***
         * 动态设置inSampleSize 倍数
         * 
         * @param pathName
         *            图片路径
         * @param reqWidth
         *            要压缩的宽度
         * @param reqHeight
         *            要压缩的高度
         * @return
         */
        public static Bitmap decodeSampledBitmapFromResource(String pathName,
                int reqWidth, int reqHeight) {
            // 首先设置 inJustDecodeBounds=true 来获取图片尺寸
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(pathName, options);
            // 计算 inSampleSize 的值
            options.inSampleSize = calculateInSampleSize(options, reqWidth,
                    reqHeight);
    
            // 根据计算出的 inSampleSize 来解码图片生成Bitmap
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeFile(pathName, options);
        }
    View Code
  • 相关阅读:
    route-over VS mesh-under
    IOS算法(三)之插入排序
    GitHub学习笔记
    Python-面向对象 (二 继承)
    POJ 3518 Prime Gap(素数题)
    struts2的总体回想(ACTION、拦截器、值栈、OGNL表达式、ModelDriven方案等)
    first move advantage_百度搜索
    【绿茶书情】:《SOHO小报》和《凤…
    潘石屹的SOHO小报猝死
    ASP.NET Hashtable输出JSON格式数据
  • 原文地址:https://www.cnblogs.com/freexiaoyu/p/4505184.html
Copyright © 2011-2022 走看看