小時候都知道每天寫日記是個好習慣,慢慢發現自己忘記了這些習慣,好久沒有給這干枯的博客添加一點新意了,這回也冒出個小芽來刷新一下博客。
今天寫的一點也不復雜,就是回顧一些老的知識而已,也算是記一個筆記,好了閑話不多說了,開始今天的主題吧。
關於拍照,這里不是自己實現拍照,是調用系統拍照,很簡單的,可是有些時候我也遇到一個問題,就是我沒有主動壓縮,系統卻自動幫我壓縮了,可是我需要這些高清的圖片,解決方式網上也有說,但是我做的是自己的筆記,所以也不在乎贅余,最起碼我是經過驗證后,才寫我筆記的。
下面是系統默認壓縮圖片的調用方式
1 private static final int TAKE_PICTURE = 0x000001; 2 public void photo() { 3 Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 4 startActivityForResult(openCameraIntent, TAKE_PICTURE); 5 }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { switch (requestCode) { case TAKE_PICTURE: if (&& resultCode == RESULT_OK) { String fileName = String.valueOf(System.currentTimeMillis()); Bitmap bm = null; ContentResolver resolver = getContentResolver(); if (originalUri == null) { bm = (Bitmap) intent.getExtras().get("data");//小米5測試發現好像走這里,華為G521 android4.3老機器也走這里 } else { try { bm = BitmapFactory.decodeStream(resolver.openInputStream(originalUri)); LogUtil.e(TAG, "originalBitmap photo width=" + bm.getWidth()); bm = comp(bm);//這里是壓縮至於壓縮方法,就放下面提供了 } catch (FileNotFoundException e) { e.printStackTrace(); } }
//剩下就自己操作bitmap了比如下面的保存
FileUtils.saveBitmap(bm, fileName);
//........
}
break;
this.mLoginHelper.onActivityResult(requestCode, resultCode, intent);
}
上面的是常用的調用方式,缺點就是圖片會被系統自己壓縮。
也不兜圈子,下面的方法就是保存原圖的方式。
1 private static final int TAKE_PICTURE = 0x000001; 2 private String fileName; 3 4 public void photo() { 5 Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 6 fileName = String.valueOf(System.currentTimeMillis()); 7 File photoFile = FileUtils.createPic(fileName); 8 openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); 9 startActivityForResult(openCameraIntent, TAKE_PICTURE); 10 }
返回調用
1 @Override 2 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {36 switch (requestCode) { 37 case TAKE_PICTURE: 38 if (Bimp.tempSelectBitmap.size() < 3 && resultCode == RESULT_OK) { 44 Uri originalUri = null; 45 File f=FileUtils.getPic(fileName); 46 try { 47 originalUri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(),f.getAbsolutePath(), null, null)); 48 } catch (FileNotFoundException e) { 49 e.printStackTrace(); 50 } 52 Bitmap bm = null; 53 ContentResolver resolver = getContentResolver(); 56 if (originalUri == null) { 58 bm = (Bitmap) intent.getExtras().get("data"); 59 } else { 60 try { 61 bm = BitmapFactory.decodeStream(resolver.openInputStream(originalUri)); 63 LogUtil.e(TAG, "originalBitmap photo width=" + bm.getWidth()); 65 bm = comp(bm); 66 } catch (FileNotFoundException e) { 67 e.printStackTrace(); 68 } 69 70 } 71 FileUtils.saveBitmap(bm, fileName); 72 73 //繼續操作78 79 } 80 break; 81 } 82 this.mLoginHelper.onActivityResult(requestCode, resultCode, intent); 83 }
下面是保存方法:
public static void saveBitmap(Bitmap bm, String picName) { try { if (!isFileExist("")) { File tempf = createSDDir(""); } File f = new File(SDPATH, picName + ".JPEG"); if (f.exists()) { f.delete(); } FileOutputStream out = new FileOutputStream(f); bm.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
下面是壓縮方法:
private Bitmap comp(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, baos); if (baos.toByteArray().length / 1024 > 1024) {// 判斷如果圖片大於1M,進行壓縮避免在生成圖片(BitmapFactory.decodeStream)時溢出 baos.reset();// 重置baos即清空baos image.compress(Bitmap.CompressFormat.JPEG, 50, baos);// 這里壓縮50%,把壓縮后的數據存放到baos中 } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); BitmapFactory.Options newOpts = new BitmapFactory.Options(); // 開始讀入圖片,此時把options.inJustDecodeBounds 設回true了 newOpts.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts); newOpts.inJustDecodeBounds = false; int w = newOpts.outWidth; int h = newOpts.outHeight; // 現在主流手機比較多是800*480分辨率,所以高和寬我們設置為 float hh = 1000f;// 這里設置高度為800f float ww = 1000f;// 這里設置寬度為480f // 縮放比。由於是固定比例縮放,只用高或者寬其中一個數據進行計算即可 int be = 1;// be=1表示不縮放 if (w > h && w > ww) {// 如果寬度大的話根據寬度固定大小縮放 be = (int) (newOpts.outWidth / ww); } else if (w < h && h > hh) {// 如果高度高的話根據寬度固定大小縮放 be = (int) (newOpts.outHeight / hh); } if (be <= 0) be = 1; newOpts.inSampleSize = be;// 設置縮放比例 // 重新讀入圖片,注意此時已經把options.inJustDecodeBounds 設回false了 isBm = new ByteArrayInputStream(baos.toByteArray()); bitmap = BitmapFactory.decodeStream(isBm, null, newOpts); return compressImage(bitmap);// 壓縮好比例大小后再進行質量壓縮 } private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 質量壓縮方法,這里100表示不壓縮,把壓縮后的數據存放到baos中 int options = 100; // while (baos.toByteArray().length / 1024 > 100) { // 循環判斷如果壓縮后圖片是否大於100kb,大於繼續壓縮 while (baos.toByteArray().length / 1024 > 2048) { // 循環判斷如果壓縮后圖片是否大於1024kb,大於繼續壓縮 baos.reset();// 重置baos即清空baos image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 這里壓縮options%,把壓縮后的數據存放到baos中 options -= 10;// 每次都減少10 } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把壓縮后的數據baos存放到ByteArrayInputStream中 Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream數據生成圖片 return bitmap; }