/** * 处理图片的工具类. * */ public class ImageTool { /** * 读取路径中的图片,而后将其转化为缩放后的bitmap * * @param path */ public static void 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); } /** * 保存图片为PNG * * @param bitmap * @param name */ 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 * @return */ private Bitmap createBitmap(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(); // create the new blank bitmap Bitmap newb = Bitmap.createBitmap(w, h, Config.ARGB_8888);// 建立一个新的和SRC长度宽度同样的位图 Canvas cv = new Canvas(newb); // draw src into cv.drawBitmap(src, 0, 0, null);// 在 0,0坐标开始画入src // draw watermark into cv.drawBitmap(watermark, w - ww + 5, h - wh + 5, null);// 在src的右下角画入水印 // save all clip cv.save(Canvas.ALL_SAVE_FLAG);// 保存 // store cv.restore();// 存储 return newb; } // 将图片转换成byte[]以便能将其存到数据库或者上传网络 public static byte[] getByteFromBitmap(Bitmap bitmap) { ByteArrayOutputStream out = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); try { out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); // Log.e(TAG, "transform byte exception"); } return out.toByteArray(); } // 获得存储在数据库中的图片 // eg imageView.setImageBitmap(bitmapobj); public static Bitmap getBitmapFromByte(byte[] temp) { if (temp != null) { Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length); return bitmap; } else { // Bitmap bitmap=BitmapFactory.decodeResource(getResources(), // R.drawable.contact_add_icon); return null; } } //将手机中的文件转换为Bitmap类型 public static Bitmap getBitemapFromFile(File f) { if (!f.exists()) return null; try { return BitmapFactory.decodeFile(f.getAbsolutePath()); } catch (Exception ex) { return null; } } //将手机中的文件转换为Bitmap类型(重载+1) public static Bitmap getBitemapFromFile(String fileName) { try { return BitmapFactory.decodeFile(fileName); } catch (Exception ex) { return null; } } }