/** * 处理图片的工具类. */public class ImageTools {/** * 图片去色,返回灰度图片 *  * @param bmpOriginal传入的图片 * @return 去色后的图片 */public static Bitmap toGrayscale(Bitmap bmpOriginal) {int width, height;// 获取宽高height = bmpOriginal.getHeight();width = bmpOriginal.getWidth();// 按照一定画质新建bitmapBitmap bmpGrayscale = Bitmap.createBitmap(width, height,Bitmap.Config.RGB_565);// 建画布Canvas c = new Canvas(bmpGrayscale);// 建画笔Paint paint = new Paint();// 颜色矩阵ColorMatrix cm = new ColorMatrix();// 设置灰阶cm.setSaturation(0);// 颜色矩阵颜色过滤对象,传入设置的颜色矩阵ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);// 设置画笔的颜色过滤,传入颜色矩阵颜色过滤对象paint.setColorFilter(f);// 在画布用画笔画传入的图片c.drawBitmap(bmpOriginal, 0, 0, paint);return bmpGrayscale;}/** * 去色同时加圆角 *  * @param bmpOriginal 原图 * @param pixels 圆角弧度 * @return 修改后的图片 */public static Bitmap toGrayscale(Bitmap bmpOriginal, int pixels) {return toRoundCorner(toGrayscale(bmpOriginal), pixels);}/** * 把图片变成圆角 * @param bitmap 需要修改的图片 * @param pixels 圆角的弧度 * @return 圆角图片 */public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), Config.ARGB_8888);Canvas canvas = new Canvas(output);final int color = 0xff424242;final Paint paint = new Paint();final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());final RectF rectF = new RectF(rect);final float roundPx = pixels;// 圆角的弧度paint.setAntiAlias(true);canvas.drawARGB(0, 0, 0, 0);paint.setColor(color);canvas.drawRoundRect(rectF, roundPx, roundPx, paint);paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));canvas.drawBitmap(bitmap, rect, rect, paint);return output;}/** *//** * 使圆角功能支持BitampDrawable *  * @param bitmapDrawable   * @param pixels * @return */public static BitmapDrawable toRoundCorner(BitmapDrawable bitmapDrawable,int pixels) {Bitmap bitmap = bitmapDrawable.getBitmap();bitmapDrawable = new BitmapDrawable(toRoundCorner(bitmap, pixels));return bitmapDrawable;}/** * 读取路径中的图片,然后将其转化为缩放后的bitmap * @param path */public static Bitmap saveBefore(String path) {BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;// 获取这个图片的宽和高Bitmap bitmap = BitmapFactory.decodeFile(path, options); // 此时返回bm为空options.inJustDecodeBounds = false;// 计算缩放比int be = (int) (options.outHeight / (float) 200);if (be <= 0) {be = 1;}options.inSampleSize = 2; // 图片长宽各缩小二分之一// 重新读入图片,注意这次要把options.inJustDecodeBounds 设为 false哦bitmap = BitmapFactory.decodeFile(path, options);int w = bitmap.getWidth();int h = bitmap.getHeight();System.out.println(w + "    " + h);// savePNG_After(bitmap,path);saveJPGE_After(bitmap, path);return bitmap;}/** * 保存图片为PNG *  * @param bitmap bitmap对象 * @param name  path */public static void savePNG_After(Bitmap bitmap, String name) {File file = new File(name);try {FileOutputStream out = new FileOutputStream(file);if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)) {out.flush();out.close();}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}/** * 保存图片为JPEG *  * @param bitmap * @param path   */public static void saveJPGE_After(Bitmap bitmap, String path) {File file = new File(path);try {FileOutputStream out = new FileOutputStream(file);if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) {out.flush();out.close();}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}/** * 水印 *  * @param bitmap 图片 * @param bitmap 水印 * @return bitmap 处理后的图片 */public static Bitmap createBitmapForWatermark(Bitmap src, Bitmap watermark) {if (src == null) {return null;}//位图的宽高int w = src.getWidth();int h = src.getHeight();//水印的宽高int ww = watermark.getWidth();int wh = watermark.getHeight();// 创建一个新的和SRC长度宽度一样的位图Bitmap newb = Bitmap.createBitmap(w, h, Config.ARGB_8888);Canvas cv = new Canvas(newb);// 在 0,0坐标开始画入srccv.drawBitmap(src, 0, 0, null);// 在src的右下角画入水印(距离底部和右边距离为5)cv.drawBitmap(watermark, w - ww + 5, h - wh + 5, null);// 保存cv.save(Canvas.ALL_SAVE_FLAG);// 存储cv.restore();return newb;}/** * 将Bitmap转换成指定大小 *  * @param bitmap * @param width * @param height * @return */public static Bitmap createBitmapBySize(Bitmap bitmap, int width, int height) {return Bitmap.createScaledBitmap(bitmap, width, height, true);}/** * Drawable 转 Bitmap *  * @param drawable * @return */public static Bitmap drawableToBitmapByBD(Drawable drawable) {BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;return bitmapDrawable.getBitmap();}/** * Bitmap 转 Drawable *  * @param bitmap * @return */public static Drawable bitmapToDrawableByBD(Bitmap bitmap) {Drawable drawable = new BitmapDrawable(bitmap);return drawable;}/** * byte[] 转 bitmap *  * @param b * @return */public static Bitmap bytesToBimap(byte[] b) {if (b.length != 0) {return BitmapFactory.decodeByteArray(b, 0, b.length);} else {return null;}}/** * bitmap 转 byte[] *  * @param bm * @return */public static byte[] bitmapToBytes(Bitmap bm) {ByteArrayOutputStream baos = new ByteArrayOutputStream();bm.compress(Bitmap.CompressFormat.PNG, 100, baos);return baos.toByteArray();}/**       * 图片转成string       *        * @param bitmap       * @return       */      public static String convertIconToString(Bitmap bitmap)      {          ByteArrayOutputStream baos = new ByteArrayOutputStream();// outputstream          bitmap.compress(CompressFormat.PNG, 100, baos);          byte[] appicon = baos.toByteArray();// 转为byte数组          return Base64.encodeToString(appicon, Base64.DEFAULT);      }    /**       * string转成bitmap       *        * @param st       */      public static Bitmap convertStringToIcon(String st)      {          // OutputStream out;          Bitmap bitmap = null;          try          {              // out = new FileOutputStream("/sdcard/aa.jpg");              byte[] bitmapArray;              bitmapArray = Base64.decode(st, Base64.DEFAULT);              bitmap =                      BitmapFactory.decodeByteArray(bitmapArray, 0,                              bitmapArray.length);              // bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);              return bitmap;          }          catch (Exception e)          {              return null;          }      }/** * Check the SD card 检测是否有SD卡 *  * @return   */public static boolean checkSDCardAvailable() {return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);}/** * Save image to the SD card 保存图片到sd卡 *  * @param photoBitmap 位图 * @param photoName  图片名 * @param path 存放路径 *             */public static void savePhotoToSDCard(Bitmap photoBitmap, String path,String photoName) {// 判断是否存在sd卡if (checkSDCardAvailable()) {File dir = new File(path);// 如果dir 文件不存在,新建if (!dir.exists()) {dir.mkdirs();}// 建图片文件路径,在文件路径下File photoFile = new File(path, photoName + ".png");// 新建文件输出流定为空FileOutputStream fileOutputStream = null;try {fileOutputStream = new FileOutputStream(photoFile);if (photoBitmap != null) {if (photoBitmap.compress(Bitmap.CompressFormat.PNG, 100,fileOutputStream)) {fileOutputStream.flush(); // 这里才是输出数据fileOutputStream.close();}}} catch (FileNotFoundException e) {photoFile.delete();e.printStackTrace();} catch (IOException e) {photoFile.delete();e.printStackTrace();} finally {try {fileOutputStream.close();} catch (IOException e) {e.printStackTrace();}}}}}
上传的这个工具类 是 网上找到的一些方法进行的一个总结,直接可以用的。 我现在都在用。代码中注释比较完整 就不用我再赘述了。

更多相关文章

  1. 【Android性能优化】Android图片加载方案--Bitmap的内存管理和优
  2. android上传图片过大处理
  3. android防新闻循环轮播图效果
  4. Android(安卓)时间选择器 PickerView,的详细使用
  5. Android(安卓)Volley 详解 Google发布的一套用于网络通信的工具
  6. Android(安卓)自定义View 使用Matrix(矩阵)操作图片
  7. Android(安卓)仿微信朋友圈发动态功能(相册图片多选)
  8. android加载gif图片的动画库
  9. Android开发 之 图片浏览

随机推荐

  1. Android(安卓)如何 隐藏导航栏
  2. Android(安卓)App: 按键识别
  3. iOS、Android获取文件头信息
  4. Android(安卓)创建图像倒影
  5. android百度地图:ItemizedOverlay
  6. Android(安卓)RecyclerView多个Item布局
  7. android 调试 报错
  8. 在Tab里面使用Android(安卓)TTS引擎的问
  9. Android(安卓)网页无法打开 net:ERR_UNKN
  10. android屏幕旋转在framework中的修改。