標籤:拍照 圖片處理 ImageView Android
一、布局中顯示圖片
在布局的xml中布局圖片的時候用ImageView,用src去指定圖片所在位置。如下代碼所示,指定的就是工程目錄(/res/drawable)中檔案名稱為unknown.png的圖片。
這裡要注意的是Android Studio在布局時
只認png格式的圖片,即使是jpeg格式,僅把尾碼改為png也不行,編譯時間會不通過。
<ImageView android:id="@+id/iv_mytest" android:src="@drawable/unknown" />
但是,也不等於jpeg格式的圖片就不能顯示,我們可以通過如下代碼處理的方式來展示到介面上。
String imgPath = Environment.getExternalStorageDirectory() + "test.jpg";ImageView iv_mytest = (ImageView) findViewById(R.id.iv_mytest);iv_mytest.setVisibility(View.VISIBLE);if(!imgPath.equals("")) { Bitmap tempBitmap = BitmapFactory.decodeFile(imgPath); iv_mytest.setImageBitmap(tempBitmap);//顯示圖片}
二、拍照後顯示圖片
拍照流程為擷取緩衝圖片路徑->進入拍照介面->拍照介面拍照後自動存到緩衝圖片路徑中->進入回呼函數->對緩衝圖片進行處理(如旋轉縮放等)並儲存到自己指定位置->刪除緩衝路徑圖片。
具體代碼如下所示:
private String tmpfilename = "";//調用拍照介面private void photograph(){ try { // 跳轉至拍照介面 String sdStatus = Environment.getExternalStorageState(); if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 檢測sd是否可用 ToastUtil.showToastMsgError(MyTestActivity.this,"SD card is not avaiable/writeable right now."); return; } tmpfilename=getTempFilePath(); File out = new File(tmpfilename); Intent intentPhote = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intentPhote.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); Uri uri = Uri.fromFile(out); // 擷取拍照後未壓縮的原圖片,並儲存在uri路徑中 intentPhote.putExtra(MediaStore.EXTRA_OUTPUT, uri); startActivityForResult(intentPhote, 1); }catch (Exception e) { }}/** * 擷取原緩衝圖片儲存路徑 * @return */private String getTempFilePath() { // 照片全路徑 String fileName = ""; // 檔案夾路徑 String pathUrl = Environment.getExternalStorageDirectory()+"/tmp/"; imagename = "mytest.png"; File file = new File(pathUrl); file.mkdirs();// 建立檔案夾 fileName = pathUrl + imagename; return fileName;}//拍取照後的回調@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1 && resultCode == Activity.RESULT_OK) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; // 設定了此屬性一定要記得將值設定為false Bitmap bitmap = BitmapFactory.decodeFile(tmpfilename); // 防止OOM發生 options.inJustDecodeBounds = false; saveMyBitmap(imagename,bitmap); File old = new File(Environment.getExternalStorageDirectory() + "/tmp/"); FileUtil.deleteFile(old);//刪除緩衝圖片 String resultMsg = "圖片已儲存"; ToastUtil.showToastMsgSuccess(this, resultMsg); }}//將映像儲存到SD卡中public void saveMyBitmap(String bitName, Bitmap mBitmap) { String thisDate = formatter_date.format(new Date()); File f = FileUtil.getFilePath(Environment.getExternalStorageDirectory() + "/rfid/prehairpin/"+thisDate+"/", bitName); String realfilepath = f.getPath(); FileOutputStream fOut = null; try { fOut = new FileOutputStream(f); } catch (Exception e) { e.printStackTrace(); } Matrix matrix = new Matrix(); // 按照固定大小對圖片進行縮放 matrix.postScale(0.3f, 0.3f); System.out.println(mBitmap.getWidth() + mBitmap.getHeight()); if (mBitmap.getHeight() < mBitmap.getWidth()) { matrix.postRotate(90); //翻轉90度 } mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true); mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut); try { fOut.write(("my secret message").getBytes());//我在這邊偷偷給圖片結尾藏了一些資訊 fOut.flush(); } catch (IOException e) { e.printStackTrace(); } try { fOut.close(); } catch (IOException e) { e.printStackTrace(); }}
三、圖片的處理
旋轉、縮放等操作我們是通過Matrix來處理的,Matrix還有很多其他圖形處理的方法,可以另開一篇去講述。
Matrix matrix = new Matrix();// 按照固定大小對圖片進行縮放matrix.postScale(0.3f, 0.3f);if (mBitmap.getHeight() < mBitmap.getWidth()) { matrix.postRotate(90); //翻轉90度}mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
儲存圖片,並將字元存入圖片(有時候拍照後希望將一些資訊與圖片綁定起來,直接記錄檔案名稱又擔心被人篡改,就想到了這種在圖片檔案末尾記錄一些資訊的方式)
String thisDate = formatter_date.format(new Date());File f = FileUtil.getFilePath(Environment.getExternalStorageDirectory() + "/rfid/prehairpin/"+thisDate+"/", bitName);String realfilepath = f.getPath();FileOutputStream fOut = null;try { fOut = new FileOutputStream(f);} catch (Exception e) { e.printStackTrace();}//我在這邊偷偷給圖片結尾藏了一些資訊try { fOut.write(("my secret message").getBytes()); fOut.flush();} catch (IOException e) { e.printStackTrace();}try { fOut.close();} catch (IOException e) { e.printStackTrace();}
四、圖片格式的差異
png、jpeg因為格式的差異,在內部添加字元資訊時會不一樣,比如png格式結尾處就是圖片資訊,所以添加的話直接在末尾添加就可以;而jpeg不行,jpeg末尾是有固定格式資訊的,直接載入末尾雖然不影響圖片顯示,但是在解析時就會因為位置位移解析出來的字元資訊就不對了。
這一塊內容還有待去深入研究下,當時也只是實驗了兩種格式,發現了這一問題。
Android關於圖片的處理