android調用系統拍照那些事

來源:互聯網
上載者:User

標籤:計算   迴圈   float   extra   固定   factory   array   寬度   ack   

小時候都知道每天寫日記是個好習慣,慢慢發現自己忘記了這些習慣,好久沒有給這乾枯的部落格添加一點新意了,這回也冒出個小芽來重新整理一下部落格。

今天寫的一點也不複雜,就是回顧一些老的知識而已,也算是記一個筆記,好了閑話不多說了,開始今天的主題吧。

關於拍照,這裡不是自己實現拍照,是調用系統拍照,很簡單的,可是有些時候我也遇到一個問題,就是我沒有主動壓縮,系統卻自動幫我壓縮了,可是我需要這些高清的圖片,解決方式網上也有說,但是我做的是自己的筆記,所以也不在乎贅餘,最起碼我是經過驗證後,才寫我筆記的。

下面是系統預設壓縮圖片的調用方式

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;    }

 

android調用系統拍照那些事

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.