图片拍照图片 处理工具 旋转 存贮等等

来源:互联网 发布:wpf编程宝典2013 pdf 编辑:程序博客网 时间:2024/05/11 18:43
package com.example.camerademo.utils;import java.io.BufferedOutputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.nio.ByteBuffer;import java.nio.channels.Channels;import java.nio.channels.ReadableByteChannel;import android.content.Context;import android.content.res.Resources;import android.graphics.Bitmap;import android.graphics.Bitmap.CompressFormat;import android.graphics.BitmapFactory;import android.graphics.BitmapFactory.Options;import android.graphics.Matrix;import android.media.ExifInterface;import android.net.Uri;//import android.media.tv.TvContract.Channels;import android.os.Environment;import android.util.Log;import android.widget.Toast;/** * @action Bitmap 拍照时的图片处理的一些方法:旋转,发达缩小 *  * @author DongXiang * * @time 2016年7月27日下午4:24:23 *  * @version 1.0 * * strFilePath绝对路径  : 图片可以不存在 * 外部存贮路径 String filePath = Environment.getExternalStorageDirectory() + File.separator + "test.jpg";  * 内部缓存 String dir = FileUtils.getCacheDir(context) + "Image" + File.separator+"test.jpg"; *  */public class BitmapHelper {/** * 字符串  文件路径 转换成Uri * @param filePath * @return */public static Uri filePath2Uri(String filePath){return Uri.fromFile(new File(filePath));}/** * Uri 获取对应的字符串地址 * @param uri * @return */public static String uri2FilePath(Uri uri){return  uri.getPath();}/** * get the orientation of the bitmap {@link ExifInterface} * * @param path * @return */public static int getDegress(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;}/** * rotate the bitmap * * @param bitmap * @param degress * @return */public static Bitmap rotateBitmap(Bitmap bitmap, int degress) {if (bitmap != null) {Matrix m = new Matrix();m.postRotate(degress);bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),bitmap.getHeight(), m, true);return bitmap;}return bitmap;}/** * caculate the bitmap sampleSize *  * 获取bitmap的压缩比例, *  * @param options * @param rqsW   * @param rqsH * @return rqsW或rqsH=0 ,则不压缩(返回值是1),获取宽 高压缩比例( >1 )===取最小值 */public static int caculateInSampleSize(Options options, int rqsW, int rqsH) {int height = options.outHeight;int width = options.outWidth;// 主要修改这里,让最宽的对应设定的宽,高同理if (width > height) {//保证height 最大int temp = width;width = height;height = temp;}if (rqsW > rqsH) {//保证rqsh 最大int rqsT = rqsH;rqsH = rqsW;rqsW = rqsT;}int inSampleSize = 1;if (rqsW == 0 || rqsH == 0)return 1;if (height > rqsH || width > rqsW) {int heightRatio = Math.round((float) height / (float) rqsH);int widthRatio = Math.round((float) width / (float) rqsW);inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;}return inSampleSize;}/** * 根据需要压缩到某尺寸压缩指定路径的图片,并得到图片对象:控制生成图片的 像素风格 *  *  options.inPurgeable = true;      Bitmap 对象是否使用软引用机制 *  options.inInputShareable = true;组合使用的, 是控制是否复制 inputfile 对象的引用,// 可用可不用 *   如果不复制, 那么要实现 inPurgeable 机制就需要复制一份 file 数据, 才能在系统需要 decode 的时候创建一个 bitmap 对象. * *Bitmap bmp = BitmapFactory.decodeFile(path, options); * * @param path * @param rqsW * @param rqsH * @return */public static Bitmap compressBitmap(String path, int rqsW, int rqsH) {Options options = getBitmapOptions(path);options.inSampleSize = caculateInSampleSize(options, rqsW, rqsH);options.inPreferredConfig = Bitmap.Config.ARGB_4444; //生成Bitmap的 像素风格return BitmapFactory.decodeFile(path, options);}/** * 压缩指定路径图片,并将其保存在缓存目录中,通过isDelSrc判定是否删除源文件,并获取到缓存后的图片路径 *  返回的图片是  JPEG,质量80  * * @param context * @param srcPath * @param rqsW * @param rqsH * @param isDelSrc * @return */public static String compressBitmap(Context context, String srcPath,int rqsW, int rqsH, boolean isDelSrc) {int degree = getDegress(srcPath);Bitmap bitmap = compressBitmap(srcPath, rqsW, rqsH);// 根据长宽以及图片的长宽得到缩放图片File srcFile = new File(srcPath);String desPath = getImageCacheDir(context) + srcFile.getName();try {if (degree != 0)bitmap = rotateBitmap(bitmap, degree);File file = new File(desPath);FileOutputStream fos = new FileOutputStream(file);bitmap.compress(CompressFormat.JPEG, 80, fos);// 80是图片质量fos.close();if (isDelSrc)srcFile.deleteOnExit();} catch (Exception e) {}bitmap.recycle();System.gc();return desPath;}/** * 压缩某个输入流中的图片,可以解决网络输入流压缩问题,并得到图片对象 * * @return Bitmap {@link Bitmap} */public static Bitmap compressBitmap(InputStream is, int reqsW, int reqsH) {try {ByteArrayOutputStream baos = new ByteArrayOutputStream();ReadableByteChannel channel = Channels.newChannel(is);ByteBuffer buffer = ByteBuffer.allocate(1024);while (channel.read(buffer) != -1) {buffer.flip();while (buffer.hasRemaining())baos.write(buffer.get());buffer.clear();}byte[] bts = baos.toByteArray();Bitmap bitmap = compressBitmap(bts, reqsW, reqsH);is.close();channel.close();baos.close();return bitmap;} catch (Exception e) {// TODO: handle exceptione.printStackTrace();return null;}}/** * 压缩指定byte[]图片,并得到压缩后的图像 * * @param bts * @param reqsW * @param reqsH * @return */public static Bitmap compressBitmap(byte[] bts, int reqsW, int reqsH) {Options options = new Options();options.inJustDecodeBounds = true;BitmapFactory.decodeByteArray(bts, 0, bts.length, options);options.inSampleSize = caculateInSampleSize(options, reqsW, reqsH);options.inJustDecodeBounds = false;return BitmapFactory.decodeByteArray(bts, 0, bts.length, options);}/** * 压缩已存在的图片对象,并返回压缩后的图片:   返回的图片格式是 PNG,质量100, * * @param bitmap * @param reqsW * @param reqsH * @return */public static Bitmap compressBitmap(Bitmap bitmap, int reqsW, int reqsH) {try {ByteArrayOutputStream baos = new ByteArrayOutputStream();bitmap.compress(CompressFormat.PNG, 100, baos);byte[] bts = baos.toByteArray();Bitmap res = compressBitmap(bts, reqsW, reqsH);baos.close();return res;} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();return bitmap;}}/** * 压缩资源图片,并返回图片对象 * * @param res 资源  context.getResources() *            {@link Resources} * @param resID  资源id * @param reqsW  * @param reqsH * @return */public static Bitmap compressBitmap(Resources res, int resID, int reqsW,int reqsH) {Options options = new Options();options.inJustDecodeBounds = true;BitmapFactory.decodeResource(res, resID, options);options.inSampleSize = caculateInSampleSize(options, reqsW, reqsH);options.inJustDecodeBounds = false;return BitmapFactory.decodeResource(res, resID, options);}/** * 得到指定路径图片的options * * @param srcPath * @return Options {@link Options} */public static Options getBitmapOptions(String srcPath) {Options options = new Options();options.inJustDecodeBounds = true;BitmapFactory.decodeFile(srcPath, options);options.inJustDecodeBounds = false;return options;}/** * 获取图片缓存路径 * * @param context * @return */public static String getImageCacheDir(Context context) {String dir = FileUtils.getCacheDir(context) + "Image" + File.separator;File file = new File(dir);if (!file.exists())file.mkdirs();return dir;}/** * Bitmap 保存到指定的 strFilePath *  * 生成图片的格式,生成图片的质量,都可以在这里修改,包括压缩图片 *  * @param strFilePath *            地址 FilePath= /storage/emulated/0/  *            例如:外部存贮路径 String filePath = Environment.getExternalStorageDirectory() + File.separator ; //+ "test.jpg";  *            *                  内部缓存 String dir = FileUtils.getCacheDir(context) + "Image" + File.separator; //+"test.jpg"; * @param fileName *            文件的名字要带有后缀 "test.jpg" * @param bitmap *            都不能为空,路径为空的时候,会自己创建 * @return true 存储成功 */public static boolean saveBitmap2File(String strFilePath, String fileName,Bitmap bitmap) {if (bitmap == null) {Log.e("图片地址不能为空", "图片地址不能为空");return false;}if (!strFilePath.endsWith(File.separator)) {strFilePath += File.separator;}// String path = getSDPath() +"/revoeye/";File dirFile = new File(strFilePath);if (!dirFile.exists()) {dirFile.mkdir();}//File bitmapFile = new File(strFilePath + fileName);saveBitmap2FilePath(strFilePath + fileName, bitmap);//BufferedOutputStream bos;//try {//bos = new BufferedOutputStream(new FileOutputStream(bitmapFile));//bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos); // 生成图片的格式,图片质量,要要保存到哪个流当中//bos.flush();//bos.close();////return true;//} catch (FileNotFoundException e) {//// TODO Auto-generated catch block//e.printStackTrace();//} catch (IOException e) {//// TODO Auto-generated catch block//e.printStackTrace();//}return false;}/** * 生成图片的格式,生成图片的质量,都可以在这里修改,包括压缩图片 *  * 存贮旋转的 图片,有旋转和没旋转的图片都可以从这里走 * 自动判断图片是否旋转,并存储没有旋转角度的bitmap到该文件中 * @param FilePath 绝对路径   * 外部存贮路径 String filePath = Environment.getExternalStorageDirectory() + File.separator + "test.jpg";  * 内部缓存 String dir = FileUtils.getCacheDir(context) + "Image" + File.separator+"test.jpg"; * @return  */public static boolean saveBitmap2RotateFilePath(String FilePath) {File file = new File(FilePath);if (file.exists()) {Log.e("filePath", file.getAbsolutePath() + " == FilePath= "+ FilePath);Bitmap bitmapRotate = BitmapHelper.rotateBitmap(BitmapFactory.decodeFile(FilePath),BitmapHelper.getDegress(FilePath));Log.e("bitmapDegress=", "== " + BitmapHelper.getDegress(FilePath));saveBitmap2FilePath(FilePath, bitmapRotate);return true;}return false;}/** * bitmap 存到指定的 绝对路径中 *  * 生成图片的格式,生成图片的质量,都可以在这里修改,包括压缩图片 *  *  * @param strFilePath绝对路径  : 图片可以不存在 * 外部存贮路径 String filePath = Environment.getExternalStorageDirectory() + File.separator + "test.jpg";  * 内部缓存 String dir = FileUtils.getCacheDir(context) + "Image" + File.separator+"test.jpg"; * @param bitmap  * @return */public static boolean saveBitmap2FilePath(String strFilePath, Bitmap bitmap) {if (bitmap == null) {Log.e("图片地址不能为空", "图片地址不能为空");return false;}File bitmapFile = new File(strFilePath);BufferedOutputStream bos;try {bos = new BufferedOutputStream(new FileOutputStream(bitmapFile));bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos); // 生成图片的格式,图片质量,要要保存到哪个流当中bos.flush();bos.close();return true;} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return false;}}class FileUtils {/** * 获取app 的缓存目录 *  * @param context * @return */public static String getCacheDir(Context context) {File cacheDir = context.getCacheDir();// 文件所在目录为getFilesDir();String cachePath = cacheDir.getPath();return cachePath;}}


0 0
原创粉丝点击