Android 拍照并對照片進行裁剪和壓縮實例詳解
更新時間:2017年07月30日 14:29:18 投稿:lqh
這篇文章主要介紹了Android 拍照并對照片進行裁剪和壓縮實例詳解的相關資料,這里提供實例代碼,需要的朋友可以參考下
Android 拍照并對照片進行裁剪和壓縮實例詳解
本文主要介紹 Android 調用攝像頭拍照并對照片進行裁剪和壓縮,文中給出了主要步驟和關鍵代碼。
調用攝像頭拍照,對拍攝照片進行裁剪,代碼如下。
/** * 調用攝像頭拍照,對拍攝照片進行裁剪 */ private void showCameraAction() { // 跳轉到系統(tǒng)照相機 Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (cameraIntent.resolveActivity(this.getPackageManager()) != null) { // 設置系統(tǒng)相機拍照后的輸出路徑 // 創(chuàng)建臨時文件 tempFile = new File(Constants.FILE_NAME); //FileUtils.createTmpFile(this, Constants.FILE_NAME); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile)); startActivityForResult(cameraIntent, CAMERA_INTENT_REQUEST); } else { Toast.makeText(this, R.string.msg_no_camera, Toast.LENGTH_SHORT).show(); } }
對拍攝照片進行裁剪,代碼如下。
/** * 對拍攝照片進行裁剪 */ private void crop() { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(Uri.fromFile(tempFile), "image/*"); intent.putExtra("crop", "true"); // 這里必須設置為true拍照之后才會進行裁剪操作 // 1.寬高和比例都不設置時,裁剪框可以自行調整(比例和大小都可以隨意調整) // 2.只設置裁剪框寬高比(aspect)后,裁剪框比例固定不可調整,只能調整大小 // 3.裁剪后生成圖片寬高(output)的設置和裁剪框無關,只決定最終生成圖片大小 // 4.裁剪框寬高比例(aspect)可以和裁剪后生成圖片比例(output)不同,此時, 會以裁剪框的寬為準, // 按照裁剪寬高比例生成一個圖片,該圖和框選部分可能不同,不同的情況可能是截取框選的一部分, // 也可能超出框選部分, 向下延伸補足 // aspectX aspectY 是裁剪框寬高的比例 intent.putExtra("aspectX", 358); intent.putExtra("aspectY", 441); // outputX outputY 是裁剪后生成圖片的寬高 intent.putExtra("outputX", 358); intent.putExtra("outputY", 441); // return-data為true時,會直接返回bitmap數(shù)據(jù),但是大圖裁剪時會出現(xiàn)問題,推薦下面為false時的方式 // return-data為false時,不會返回bitmap,但需要指定一個MediaStore.EXTRA_OUTPUT保存圖片uri intent.putExtra("return-data", false); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile)); startActivityForResult(intent, ImageSelector.IMAGE_CROP_CODE); }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == CAMERA_INTENT_REQUEST) { crop(); } if (requestCode == ImageSelector.IMAGE_CROP_CODE) { if (tempFile.exists()) { //bitmap = BitmapFactory.decodeFile(tempFile.toString()); bitmap = ImageUtil.getLocalThumbImg(tempFile.toString(), 30); im_photo.setImageBitmap(bitmap); } } }
得到本地圖片旋轉壓縮,圖片質量壓縮,代碼如下。
/** * 得到本地圖片旋轉壓縮 * @param path * @param size * @return */ public static Bitmap getLocalThumbImg(String path, int size) { BitmapFactory.Options newOpts = new BitmapFactory.Options(); // 開始讀入圖片,此時把options.inJustDecodeBounds 設回true了 newOpts.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile(path, newOpts); // 此時返回bm為空 newOpts.inJustDecodeBounds = false; newOpts.inSampleSize = 1; // 設置縮放比例1表示不縮放 // 重新讀入圖片,注意此時已經(jīng)把options.inJustDecodeBounds 設回false了 bitmap = BitmapFactory.decodeFile(path, newOpts); bitmap = compressImage(bitmap, size, "jpg"); // 壓縮好比例大小后再進行質量壓縮 int degree = readPictureDegree(path); bitmap = rotaingImageView(degree, bitmap); return bitmap; } /** * 圖片質量壓縮 * * @param image * @return * @size 圖片大?。╧b) */ public static Bitmap compressImage(Bitmap image, int size, String imageType) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (imageType.equalsIgnoreCase("png")) { image.compress(Bitmap.CompressFormat.PNG, 100, baos); } else { // 質量壓縮方法,這里100表示不壓縮,把壓縮后的數(shù)據(jù)存放到baos中 image.compress(Bitmap.CompressFormat.JPEG, 100, baos); } int options = 100; // 循環(huán)判斷如果壓縮后圖片是否大于100kb,大于繼續(xù)壓縮 while (baos.toByteArray().length / 1024 > size) { baos.reset(); // 重置baos即清空baos if (imageType.equalsIgnoreCase("png")) { image.compress(Bitmap.CompressFormat.PNG, options, baos); } else { // 這里壓縮options%,把壓縮后的數(shù)據(jù)存放到baos中 image.compress(Bitmap.CompressFormat.JPEG, options, baos); } options -= 10; // 每次都減少10 } FileOutputStream out = new FileOutputStream(new File(Constants.FILE_NAME)); image.compress(Bitmap.CompressFormat.JPEG, options, out); // 把壓縮后的數(shù)據(jù)baos存放到ByteArrayInputStream中 ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); // 把ByteArrayInputStream數(shù)據(jù)生成圖片 Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null); return bitmap; } catch (Exception e) { return null; } } /** * 讀取圖片屬性:旋轉的角度 * * @param path 圖片絕對路徑 * @return degree旋轉的角度 */ public 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; } /** * 旋轉圖片 * * @param angle * @param bitmap * @return Bitmap */ public static Bitmap rotaingImageView(int angle, Bitmap bitmap) { if (bitmap == null) return null; // 旋轉圖片 動作 Matrix matrix = new Matrix(); matrix.postRotate(angle); // 創(chuàng)建新的圖片 Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); return resizedBitmap; }
如有疑問請留言,或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
您可能感興趣的文章:
- Android :okhttp+Springmvc文件解析器實現(xiàn)android向服務器上傳照片
- Android中實現(xiàn)長按照片彈出右鍵菜單功能的實例代碼
- Android WebView支持input file啟用相機/選取照片功能
- Android開發(fā)實現(xiàn)從相冊中選擇照片功能詳解
- android 實現(xiàn)在照片上繪制涂鴉的方法
- Android 選擇相冊照片并返回功能的實現(xiàn)代碼
- Android打開圖庫選擇照片功能代碼
- Android開發(fā)從相冊中選取照片的示例代碼
- 詳解Android WebView的input上傳照片的兼容問題
- Android 調用系統(tǒng)相冊選擇照片
相關文章
Android中使用LayoutInflater要注意的一些坑
LayoutInflater類在我們日常開發(fā)中經(jīng)常會用到,最近在使用中就遇到了一些問題,所有下面這篇文章主要給大家總結了關于Android中使用LayoutInflater要注意的一些坑,希望通過這篇能讓大家避免走一些彎路,需要的朋友可以參考學習,下面來一起看吧。2017-04-04Android?Camera+SurfaceView自動聚焦防止變形拉伸
這篇文章主要為大家介紹了Android自定義相機Camera+SurfaceView實現(xiàn)自動聚焦防止變形拉伸詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01Android基于廣播事件機制實現(xiàn)簡單定時提醒功能代碼
這篇文章主要介紹了Android基于廣播事件機制實現(xiàn)簡單定時提醒功能代碼,較為詳細的分析了Android廣播事件機制及提醒功能的相關實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10