图片 拍照 旋转 存储等

来源:互联网 发布:网络词蛤是啥意思 编辑:程序博客网 时间:2024/05/21 10:19

  1. package com.example.camerademo.utils;  
  2.   
  3. import java.io.BufferedOutputStream;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileNotFoundException;  
  7. import java.io.FileOutputStream;  
  8. import java.io.IOException;  
  9. import java.io.InputStream;  
  10. import java.nio.ByteBuffer;  
  11. import java.nio.channels.Channels;  
  12. import java.nio.channels.ReadableByteChannel;  
  13.   
  14. import android.content.Context;  
  15. import android.content.res.Resources;  
  16. import android.graphics.Bitmap;  
  17. import android.graphics.Bitmap.CompressFormat;  
  18. import android.graphics.BitmapFactory;  
  19. import android.graphics.BitmapFactory.Options;  
  20. import android.graphics.Matrix;  
  21. import android.media.ExifInterface;  
  22. import android.net.Uri;  
  23. //import android.media.tv.TvContract.Channels;  
  24. import android.os.Environment;  
  25. import android.util.Log;  
  26. import android.widget.Toast;  
  27.   
  28. /** 
  29.  * @action Bitmap 拍照时的图片处理的一些方法:旋转,发达缩小 
  30.  *  
  31.  * @author DongXiang 
  32.  * 
  33.  * @time 2016年7月27日下午4:24:23 
  34.  *  
  35.  * @version 1.0 
  36.  * 
  37.  * strFilePath绝对路径  : 图片可以不存在 
  38.  *                  外部存贮路径 String filePath = Environment.getExternalStorageDirectory() + File.separator + "test.jpg";  
  39.  *                  内部缓存 String dir = FileUtils.getCacheDir(context) + "Image" + File.separator+"test.jpg"; 
  40.  *  
  41.  */  
  42. public class BitmapHelper {  
  43.   
  44.     /** 
  45.      * 字符串  文件路径 转换成Uri 
  46.      * @param filePath 
  47.      * @return 
  48.      */  
  49.     public static Uri filePath2Uri(String filePath){  
  50.         return Uri.fromFile(new File(filePath));  
  51.     }  
  52.     /** 
  53.      * Uri 获取对应的字符串地址 
  54.      * @param uri 
  55.      * @return 
  56.      */  
  57.     public static String uri2FilePath(Uri uri){  
  58.         return  uri.getPath();  
  59.     }  
  60.       
  61.     /** 
  62.      * get the orientation of the bitmap {@link ExifInterface} 
  63.      * 
  64.      * @param path 
  65.      * @return 
  66.      */  
  67.     public static int getDegress(String path) {  
  68.         int degree = 0;  
  69.         try {  
  70.             ExifInterface exifInterface = new ExifInterface(path);  
  71.             int orientation = exifInterface.getAttributeInt(  
  72.                     ExifInterface.TAG_ORIENTATION,  
  73.                     ExifInterface.ORIENTATION_NORMAL);  
  74.             switch (orientation) {  
  75.             case ExifInterface.ORIENTATION_ROTATE_90:  
  76.                 degree = 90;  
  77.                 break;  
  78.             case ExifInterface.ORIENTATION_ROTATE_180:  
  79.                 degree = 180;  
  80.                 break;  
  81.             case ExifInterface.ORIENTATION_ROTATE_270:  
  82.                 degree = 270;  
  83.                 break;  
  84.             }  
  85.         } catch (IOException e) {  
  86.             e.printStackTrace();  
  87.         }  
  88.         return degree;  
  89.     }  
  90.   
  91.     /** 
  92.      * rotate the bitmap 
  93.      * 
  94.      * @param bitmap 
  95.      * @param degress 
  96.      * @return 
  97.      */  
  98.     public static Bitmap rotateBitmap(Bitmap bitmap, int degress) {  
  99.         if (bitmap != null) {  
  100.             Matrix m = new Matrix();  
  101.             m.postRotate(degress);  
  102.             bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),  
  103.                     bitmap.getHeight(), m, true);  
  104.             return bitmap;  
  105.         }  
  106.         return bitmap;  
  107.     }  
  108.   
  109.     /** 
  110.      * caculate the bitmap sampleSize 
  111.      *  
  112.      * 获取bitmap的压缩比例, 
  113.      *  
  114.      * @param options 
  115.      * @param rqsW   
  116.      * @param rqsH 
  117.      * @return rqsW或rqsH=0 ,则不压缩(返回值是1),获取宽 高压缩比例( >1 )===取最小值 
  118.      */  
  119.     public static int caculateInSampleSize(Options options, int rqsW, int rqsH) {  
  120.         int height = options.outHeight;  
  121.         int width = options.outWidth;  
  122.         // 主要修改这里,让最宽的对应设定的宽,高同理  
  123.         if (width > height) {//保证height 最大  
  124.             int temp = width;  
  125.             width = height;  
  126.             height = temp;  
  127.         }  
  128.         if (rqsW > rqsH) {//保证rqsh 最大  
  129.             int rqsT = rqsH;  
  130.             rqsH = rqsW;  
  131.             rqsW = rqsT;  
  132.         }  
  133.   
  134.         int inSampleSize = 1;  
  135.         if (rqsW == 0 || rqsH == 0)  
  136.             return 1;  
  137.         if (height > rqsH || width > rqsW) {  
  138.             int heightRatio = Math.round((float) height / (float) rqsH);  
  139.             int widthRatio = Math.round((float) width / (float) rqsW);  
  140.             inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;  
  141.         }  
  142.         return inSampleSize;  
  143.     }  
  144.   
  145.     /** 
  146.      * 根据需要压缩到某尺寸压缩指定路径的图片,并得到图片对象:控制生成图片的 像素风格 
  147.      *  
  148.      *  options.inPurgeable = true;      Bitmap 对象是否使用软引用机制 
  149.      *  options.inInputShareable = true;    组合使用的, 是控制是否复制 inputfile 对象的引用,// 可用可不用 
  150.      *               如果不复制, 那么要实现 inPurgeable 机制就需要复制一份 file 数据, 才能在系统需要 decode 的时候创建一个 bitmap 对象. 
  151.      * 
  152.      *  Bitmap bmp = BitmapFactory.decodeFile(path, options); 
  153.      * 
  154.      * @param path 
  155.      * @param rqsW 
  156.      * @param rqsH 
  157.      * @return 
  158.      */  
  159.     public static Bitmap compressBitmap(String path, int rqsW, int rqsH) {  
  160.         Options options = getBitmapOptions(path);  
  161.         options.inSampleSize = caculateInSampleSize(options, rqsW, rqsH);  
  162.         options.inPreferredConfig = Bitmap.Config.ARGB_4444; //生成Bitmap的 像素风格  
  163.           
  164.         return BitmapFactory.decodeFile(path, options);  
  165.     }  
  166.   
  167.     /** 
  168.      * 压缩指定路径图片,并将其保存在缓存目录中,通过isDelSrc判定是否删除源文件,并获取到缓存后的图片路径 
  169.      *  返回的图片是  JPEG,质量80  
  170.      * 
  171.      * @param context 
  172.      * @param srcPath 
  173.      * @param rqsW 
  174.      * @param rqsH 
  175.      * @param isDelSrc 
  176.      * @return 
  177.      */  
  178.     public static String compressBitmap(Context context, String srcPath,  
  179.             int rqsW, int rqsH, boolean isDelSrc) {  
  180.         int degree = getDegress(srcPath);  
  181.         Bitmap bitmap = compressBitmap(srcPath, rqsW, rqsH);// 根据长宽以及图片的长宽得到缩放图片  
  182.         File srcFile = new File(srcPath);  
  183.         String desPath = getImageCacheDir(context) + srcFile.getName();  
  184.         try {  
  185.             if (degree != 0)  
  186.                 bitmap = rotateBitmap(bitmap, degree);  
  187.             File file = new File(desPath);  
  188.             FileOutputStream fos = new FileOutputStream(file);  
  189.             bitmap.compress(CompressFormat.JPEG, 80, fos);// 80是图片质量  
  190.             fos.close();  
  191.             if (isDelSrc)  
  192.                 srcFile.deleteOnExit();  
  193.   
  194.         } catch (Exception e) {  
  195.         }  
  196.   
  197.         bitmap.recycle();  
  198.         System.gc();  
  199.   
  200.         return desPath;  
  201.     }  
  202.   
  203.     /** 
  204.      * 压缩某个输入流中的图片,可以解决网络输入流压缩问题,并得到图片对象 
  205.      * 
  206.      * @return Bitmap {@link Bitmap} 
  207.      */  
  208.     public static Bitmap compressBitmap(InputStream is, int reqsW, int reqsH) {  
  209.         try {  
  210.             ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  211.             ReadableByteChannel channel = Channels.newChannel(is);  
  212.             ByteBuffer buffer = ByteBuffer.allocate(1024);  
  213.             while (channel.read(buffer) != -1) {  
  214.                 buffer.flip();  
  215.                 while (buffer.hasRemaining())  
  216.                     baos.write(buffer.get());  
  217.                 buffer.clear();  
  218.             }  
  219.             byte[] bts = baos.toByteArray();  
  220.             Bitmap bitmap = compressBitmap(bts, reqsW, reqsH);  
  221.             is.close();  
  222.             channel.close();  
  223.             baos.close();  
  224.             return bitmap;  
  225.         } catch (Exception e) {  
  226.             // TODO: handle exception  
  227.             e.printStackTrace();  
  228.             return null;  
  229.         }  
  230.     }  
  231.   
  232.     /** 
  233.      * 压缩指定byte[]图片,并得到压缩后的图像 
  234.      * 
  235.      * @param bts 
  236.      * @param reqsW 
  237.      * @param reqsH 
  238.      * @return 
  239.      */  
  240.     public static Bitmap compressBitmap(byte[] bts, int reqsW, int reqsH) {  
  241.         Options options = new Options();  
  242.         options.inJustDecodeBounds = true;  
  243.         BitmapFactory.decodeByteArray(bts, 0, bts.length, options);  
  244.         options.inSampleSize = caculateInSampleSize(options, reqsW, reqsH);  
  245.         options.inJustDecodeBounds = false;  
  246.         return BitmapFactory.decodeByteArray(bts, 0, bts.length, options);  
  247.   
  248.     }  
  249.   
  250.     /** 
  251.      * 压缩已存在的图片对象,并返回压缩后的图片:   返回的图片格式是 PNG,质量100, 
  252.      * 
  253.      * @param bitmap 
  254.      * @param reqsW 
  255.      * @param reqsH 
  256.      * @return 
  257.      */  
  258.     public static Bitmap compressBitmap(Bitmap bitmap, int reqsW, int reqsH) {  
  259.         try {  
  260.             ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  261.               
  262.               
  263.             bitmap.compress(CompressFormat.PNG, 100, baos);  
  264.             byte[] bts = baos.toByteArray();  
  265.             Bitmap res = compressBitmap(bts, reqsW, reqsH);  
  266.             baos.close();  
  267.             return res;  
  268.         } catch (IOException e) {  
  269.             // TODO Auto-generated catch block  
  270.             e.printStackTrace();  
  271.             return bitmap;  
  272.         }  
  273.     }  
  274.   
  275.     /** 
  276.      * 压缩资源图片,并返回图片对象 
  277.      * 
  278.      * @param res 资源  context.getResources() 
  279.      *            {@link Resources} 
  280.      * @param resID  资源id 
  281.      * @param reqsW  
  282.      * @param reqsH 
  283.      * @return 
  284.      */  
  285.     public static Bitmap compressBitmap(Resources res, int resID, int reqsW,int reqsH) {  
  286.               
  287.         Options options = new Options();  
  288.         options.inJustDecodeBounds = true;  
  289.         BitmapFactory.decodeResource(res, resID, options);  
  290.         options.inSampleSize = caculateInSampleSize(options, reqsW, reqsH);  
  291.         options.inJustDecodeBounds = false;  
  292.         return BitmapFactory.decodeResource(res, resID, options);  
  293.     }  
  294.   
  295.     /** 
  296.      * 得到指定路径图片的options 
  297.      * 
  298.      * @param srcPath 
  299.      * @return Options {@link Options} 
  300.      */  
  301.     public static Options getBitmapOptions(String srcPath) {  
  302.         Options options = new Options();  
  303.         options.inJustDecodeBounds = true;  
  304.         BitmapFactory.decodeFile(srcPath, options);  
  305.         options.inJustDecodeBounds = false;  
  306.         return options;  
  307.     }  
  308.   
  309.     /** 
  310.      * 获取图片缓存路径 
  311.      * 
  312.      * @param context 
  313.      * @return 
  314.      */  
  315.     public static String getImageCacheDir(Context context) {  
  316.   
  317.         String dir = FileUtils.getCacheDir(context) + "Image" + File.separator;  
  318.         File file = new File(dir);  
  319.         if (!file.exists())  
  320.             file.mkdirs();  
  321.         return dir;  
  322.     }  
  323.   
  324.     /** 
  325.      * Bitmap 保存到指定的 strFilePath 
  326.      *  
  327.      * 生成图片的格式,生成图片的质量,都可以在这里修改,包括压缩图片 
  328.      *  
  329.      * @param strFilePath 
  330.      *            地址 FilePath= /storage/emulated/0/  
  331.      *            例如:外部存贮路径 String filePath = Environment.getExternalStorageDirectory() + File.separator ; //+ "test.jpg";  
  332.      *            
  333.      *                     内部缓存 String dir = FileUtils.getCacheDir(context) + "Image" + File.separator; //+"test.jpg"; 
  334.      * @param fileName 
  335.      *            文件的名字要带有后缀 "test.jpg" 
  336.      * @param bitmap 
  337.      *            都不能为空,路径为空的时候,会自己创建 
  338.      * @return true 存储成功 
  339.      */  
  340.     public static boolean saveBitmap2File(String strFilePath, String fileName,  
  341.             Bitmap bitmap) {  
  342.   
  343.         if (bitmap == null) {  
  344.             Log.e("图片地址不能为空""图片地址不能为空");  
  345.             return false;  
  346.         }  
  347.         if (!strFilePath.endsWith(File.separator)) {  
  348.             strFilePath += File.separator;  
  349.         }  
  350.   
  351.         // String path = getSDPath() +"/revoeye/";  
  352.         File dirFile = new File(strFilePath);  
  353.         if (!dirFile.exists()) {  
  354.             dirFile.mkdir();  
  355.         }  
  356. //      File bitmapFile = new File(strFilePath + fileName);  
  357.         saveBitmap2FilePath(strFilePath + fileName, bitmap);  
  358. //      BufferedOutputStream bos;  
  359. //      try {  
  360. //          bos = new BufferedOutputStream(new FileOutputStream(bitmapFile));  
  361. //          bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos); // 生成图片的格式,图片质量,要要保存到哪个流当中  
  362. //          bos.flush();  
  363. //          bos.close();  
  364. //  
  365. //          return true;  
  366. //      } catch (FileNotFoundException e) {  
  367. //          // TODO Auto-generated catch block  
  368. //          e.printStackTrace();  
  369. //      } catch (IOException e) {  
  370. //          // TODO Auto-generated catch block  
  371. //          e.printStackTrace();  
  372. //      }  
  373.         return false;  
  374.     }  
  375.       
  376.     /** 
  377.      * 生成图片的格式,生成图片的质量,都可以在这里修改,包括压缩图片 
  378.      *  
  379.      * 存贮旋转的 图片,有旋转和没旋转的图片都可以从这里走 
  380.      * 自动判断图片是否旋转,并存储没有旋转角度的bitmap到该文件中 
  381.      * @param FilePath 绝对路径   
  382.      *                  外部存贮路径 String filePath = Environment.getExternalStorageDirectory() + File.separator + "test.jpg";  
  383.      *                  内部缓存 String dir = FileUtils.getCacheDir(context) + "Image" + File.separator+"test.jpg"; 
  384.      * @return  
  385.      */  
  386.     public static boolean saveBitmap2RotateFilePath(String FilePath) {  
  387.         File file = new File(FilePath);  
  388.         if (file.exists()) {  
  389.             Log.e("filePath", file.getAbsolutePath() + " == FilePath= "  
  390.                     + FilePath);  
  391.   
  392.             Bitmap bitmapRotate = BitmapHelper.rotateBitmap(  
  393.                     BitmapFactory.decodeFile(FilePath),  
  394.                     BitmapHelper.getDegress(FilePath));  
  395.   
  396.             Log.e("bitmapDegress=""== " + BitmapHelper.getDegress(FilePath));  
  397.               
  398.             saveBitmap2FilePath(FilePath, bitmapRotate);  
  399.             return true;  
  400.         }  
  401.         return false;  
  402.   
  403.     }  
  404.     /** 
  405.      * bitmap 存到指定的 绝对路径中 
  406.      *  
  407.      * 生成图片的格式,生成图片的质量,都可以在这里修改,包括压缩图片 
  408.      *  
  409.      *  
  410.      * @param strFilePath绝对路径  : 图片可以不存在 
  411.      *                  外部存贮路径 String filePath = Environment.getExternalStorageDirectory() + File.separator + "test.jpg";  
  412.      *                  内部缓存 String dir = FileUtils.getCacheDir(context) + "Image" + File.separator+"test.jpg"; 
  413.      * @param bitmap  
  414.      * @return 
  415.      */  
  416.     public static boolean saveBitmap2FilePath(String strFilePath, Bitmap bitmap) {  
  417.   
  418.         if (bitmap == null) {  
  419.             Log.e("图片地址不能为空""图片地址不能为空");  
  420.             return false;  
  421.         }  
  422.           
  423.         File bitmapFile = new File(strFilePath);  
  424.         BufferedOutputStream bos;  
  425.         try {  
  426.             bos = new BufferedOutputStream(new FileOutputStream(bitmapFile));  
  427.             bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos); // 生成图片的格式,图片质量,要要保存到哪个流当中  
  428.             bos.flush();  
  429.             bos.close();  
  430.   
  431.             return true;  
  432.         } catch (FileNotFoundException e) {  
  433.             // TODO Auto-generated catch block  
  434.             e.printStackTrace();  
  435.         } catch (IOException e) {  
  436.             // TODO Auto-generated catch block  
  437.             e.printStackTrace();  
  438.         }  
  439.   
  440.         return false;  
  441.     }  
  442.       
  443. }  
  444.   
  445. class FileUtils {  
  446.     /** 
  447.      * 获取app 的缓存目录 
  448.      *  
  449.      * @param context 
  450.      * @return 
  451.      */  
  452.     public static String getCacheDir(Context context) {  
  453.   
  454.         File cacheDir = context.getCacheDir();// 文件所在目录为getFilesDir();  
  455.         String cachePath = cacheDir.getPath();  
  456.         return cachePath;  
  457.     }  
  458. }  
原创粉丝点击