国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 拍照并對照片進行裁剪和壓縮實例詳解

2019-10-22 18:31:22
字體:
來源:轉載
供稿:網友

Android 拍照并對照片進行裁剪和壓縮實例詳解

本文主要介紹 Android 調用攝像頭拍照并對照片進行裁剪和壓縮,文中給出了主要步驟和關鍵代碼。

調用攝像頭拍照,對拍攝照片進行裁剪,代碼如下。

/** * 調用攝像頭拍照,對拍攝照片進行裁剪 */private void showCameraAction() { // 跳轉到系統照相機 Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (cameraIntent.resolveActivity(this.getPackageManager()) != null) {  // 設置系統相機拍照后的輸出路徑  // 創建臨時文件  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數據,但是大圖裁剪時會出現問題,推薦下面為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);}
@Overrideprotected 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表示不縮放 // 重新讀入圖片,注意此時已經把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 圖片大小(kb) */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表示不壓縮,把壓縮后的數據存放到baos中   image.compress(Bitmap.CompressFormat.JPEG, 100, baos);  }  int options = 100;  // 循環判斷如果壓縮后圖片是否大于100kb,大于繼續壓縮  while (baos.toByteArray().length / 1024 > size) {   baos.reset(); // 重置baos即清空baos   if (imageType.equalsIgnoreCase("png")) {    image.compress(Bitmap.CompressFormat.PNG, options, baos);   } else {    // 這里壓縮options%,把壓縮后的數據存放到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);  // 把壓縮后的數據baos存放到ByteArrayInputStream中  ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());  // 把ByteArrayInputStream數據生成圖片  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); // 創建新的圖片 Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,             bitmap.getWidth(), bitmap.getHeight(), matrix, true); return resizedBitmap;}

如有疑問請留言,或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乌拉特中旗| 蒲江县| 广德县| 康马县| 方正县| 防城港市| 济宁市| 兴宁市| 炉霍县| 潍坊市| 阳新县| 邓州市| 乌拉特中旗| 阿鲁科尔沁旗| 南漳县| 乌恰县| 新乐市| 玉门市| 正镶白旗| 乌兰县| 大洼县| 孝义市| 建昌县| 宝兴县| 铜陵市| 定兴县| 陆丰市| 边坝县| 潮安县| 会东县| 博野县| 无为县| 库尔勒市| 西乡县| 武穴市| 义乌市| 依兰县| 西吉县| 临武县| 顺平县| 松滋市|