本文純屬個人見解,是對前面學習的總結,如有描述不正確的地方還請高手指正~
????有些場景中,須要照相并且上傳到服務,但是由于圖片的巨細太大,那么就
????上傳就
????會很慢(在有些網絡情況下),而且很耗流量,要想速度快,那么就須要減小圖片的巨細。減少圖片的巨細有兩種方法,1. 照小圖片; 2. 壓縮大圖片。 照相時獲取小圖片一般不太符合要求,因為,圖片的清晰度會很差,但是這類情況有個好處就是應用速度會快些; 壓縮圖片,就是把大圖片壓縮小,降低圖片的質量,在一定范圍內,降低圖片的巨細,并且滿意需求(圖片仍就清晰)。上面組要是分析圖片的壓縮:
????1. 照相請查看http://blog.csdn.net/luhuajcdd/article/details/8826587 ->
????想要保存圖片到制定目錄,啟動Camera應用時,須要指定文件
????final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, 480, 800); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; Bitmap bm = BitmapFactory.decodeFile(filePath, options);
? ? 2.2 處理圖片旋轉
? ?
????int degree = readPictureDegree(filePath); bm = rotateBitmap(bm,degree) ;
????private static int readPictureDegree(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; }
如果說友誼是一顆常青樹,那么,澆灌它的必定是出自心田的清泉;如果說友誼是一朵開不敗的鮮花,那么,照耀它的必定是從心中升起的太陽。 多少笑聲都是友誼喚起的,多少眼淚都是友誼揩干的。友誼的港灣溫情脈脈,友誼的清風灌滿征帆。友誼不是感情的投資,它不須要股息和分紅。(友誼可以換其他詞語)
????private static Bitmap rotateBitmap(Bitmap bitmap, int rotate){ if(bitmap == null) return null ; int w = bitmap.getWidth(); int h = bitmap.getHeight(); // Setting post rotate to 90 Matrix mtx = new Matrix(); mtx.postRotate(rotate); return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true); }
? ? 2.3壓縮圖片
? ? ? ??
????bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);//30 是壓縮率,表現壓縮70%; 如果不壓縮是100,表現壓縮率為0
????public static Bitmap getSmallBitmap(String filePath) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, 480, 800); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; Bitmap bm = BitmapFactory.decodeFile(filePath, options); if(bm == null){ return null; } int degree = readPictureDegree(filePath); bm = rotateBitmap(bm,degree) ; ByteArrayOutputStream baos = null ; try{ baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG, 30, baos); }finally{ try { if(baos != null) baos.close() ; } catch (IOException e) { e.printStackTrace(); } } return bm ; }
????private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { // Calculate ratios of height and width to requested height and // width final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); // Choose the smallest ratio as inSampleSize value, this will // guarantee // a final image with both dimensions larger than or equal to the // requested height and width. inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio; } return inSampleSize; }
文章結束給大家分享下程序員的一些笑話語錄: 3G普不普及現在已經不是看終端了,而是看應用,有好的,便宜實用的應用,花1000多買個能用的智能手機應該不是什么難事。反過來說,你200元拿一個智能手機,沒有好的應用,看個電影要幾十元,也是沒人用3G。
--------------------------------- 原創文章 By 圖片和壓縮 ---------------------------------