/** * 處理圖片的工具類. * */ 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; } } }