Android 拍照并對照片進(jìn)行裁剪和壓縮實(shí)例詳解
Android 拍照并對照片進(jìn)行裁剪和壓縮實(shí)例詳解
本文主要介紹 Android 調(diào)用攝像頭拍照并對照片進(jìn)行裁剪和壓縮,文中給出了主要步驟和關(guān)鍵代碼。
調(diào)用攝像頭拍照,對拍攝照片進(jìn)行裁剪,代碼如下。
/** * 調(diào)用攝像頭拍照,對拍攝照片進(jìn)行裁剪 */ private void showCameraAction() { // 跳轉(zhuǎn)到系統(tǒng)照相機(jī) Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (cameraIntent.resolveActivity(this.getPackageManager()) != null) { // 設(shè)置系統(tǒng)相機(jī)拍照后的輸出路徑 // 創(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(); } }
對拍攝照片進(jìn)行裁剪,代碼如下。
/** * 對拍攝照片進(jìn)行裁剪 */ private void crop() { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(Uri.fromFile(tempFile), "image/*"); intent.putExtra("crop", "true"); // 這里必須設(shè)置為true拍照之后才會進(jìn)行裁剪操作 // 1.寬高和比例都不設(shè)置時,裁剪框可以自行調(diào)整(比例和大小都可以隨意調(diào)整) // 2.只設(shè)置裁剪框?qū)捀弑?aspect)后,裁剪框比例固定不可調(diào)整,只能調(diào)整大小 // 3.裁剪后生成圖片寬高(output)的設(shè)置和裁剪框無關(guān),只決定最終生成圖片大小 // 4.裁剪框?qū)捀弑壤?aspect)可以和裁剪后生成圖片比例(output)不同,此時, 會以裁剪框的寬為準(zhǔn), // 按照裁剪寬高比例生成一個圖片,該圖和框選部分可能不同,不同的情況可能是截取框選的一部分, // 也可能超出框選部分, 向下延伸補(bǔ)足 // aspectX aspectY 是裁剪框?qū)捀叩谋壤? 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); } } }
得到本地圖片旋轉(zhuǎn)壓縮,圖片質(zhì)量壓縮,代碼如下。
/** * 得到本地圖片旋轉(zhuǎn)壓縮 * @param path * @param size * @return */ public static Bitmap getLocalThumbImg(String path, int size) { BitmapFactory.Options newOpts = new BitmapFactory.Options(); // 開始讀入圖片,此時把options.inJustDecodeBounds 設(shè)回true了 newOpts.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile(path, newOpts); // 此時返回bm為空 newOpts.inJustDecodeBounds = false; newOpts.inSampleSize = 1; // 設(shè)置縮放比例1表示不縮放 // 重新讀入圖片,注意此時已經(jīng)把options.inJustDecodeBounds 設(shè)回false了 bitmap = BitmapFactory.decodeFile(path, newOpts); bitmap = compressImage(bitmap, size, "jpg"); // 壓縮好比例大小后再進(jìn)行質(zhì)量壓縮 int degree = readPictureDegree(path); bitmap = rotaingImageView(degree, bitmap); return bitmap; } /** * 圖片質(zhì)量壓縮 * * @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 { // 質(zhì)量壓縮方法,這里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; } } /** * 讀取圖片屬性:旋轉(zhuǎn)的角度 * * @param path 圖片絕對路徑 * @return degree旋轉(zhuǎn)的角度 */ 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; } /** * 旋轉(zhuǎn)圖片 * * @param angle * @param bitmap * @return Bitmap */ public static Bitmap rotaingImageView(int angle, Bitmap bitmap) { if (bitmap == null) return null; // 旋轉(zhuǎn)圖片 動作 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文件解析器實(shí)現(xiàn)android向服務(wù)器上傳照片
- Android中實(shí)現(xiàn)長按照片彈出右鍵菜單功能的實(shí)例代碼
- Android WebView支持input file啟用相機(jī)/選取照片功能
- Android開發(fā)實(shí)現(xiàn)從相冊中選擇照片功能詳解
- android 實(shí)現(xiàn)在照片上繪制涂鴉的方法
- Android 選擇相冊照片并返回功能的實(shí)現(xiàn)代碼
- Android打開圖庫選擇照片功能代碼
- Android開發(fā)從相冊中選取照片的示例代碼
- 詳解Android WebView的input上傳照片的兼容問題
- Android 調(diào)用系統(tǒng)相冊選擇照片
相關(guān)文章
Android中使用LayoutInflater要注意的一些坑
LayoutInflater類在我們?nèi)粘i_發(fā)中經(jīng)常會用到,最近在使用中就遇到了一些問題,所有下面這篇文章主要給大家總結(jié)了關(guān)于Android中使用LayoutInflater要注意的一些坑,希望通過這篇能讓大家避免走一些彎路,需要的朋友可以參考學(xué)習(xí),下面來一起看吧。2017-04-04Android 應(yīng)用中插入廣告詳解及簡單實(shí)例
這篇文章主要介紹了Android 應(yīng)用中插入廣告詳解及簡單實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-10-10Android?Camera+SurfaceView自動聚焦防止變形拉伸
這篇文章主要為大家介紹了Android自定義相機(jī)Camera+SurfaceView實(shí)現(xiàn)自動聚焦防止變形拉伸詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Android實(shí)現(xiàn)復(fù)制Assets文件到SD卡
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)復(fù)制Assets文件到SD卡,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12Android基于廣播事件機(jī)制實(shí)現(xiàn)簡單定時提醒功能代碼
這篇文章主要介紹了Android基于廣播事件機(jī)制實(shí)現(xiàn)簡單定時提醒功能代碼,較為詳細(xì)的分析了Android廣播事件機(jī)制及提醒功能的相關(guān)實(shí)現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10Android單項(xiàng)綁定MVVM項(xiàng)目模板的方法
這篇文章主要給大家介紹了關(guān)于Android單項(xiàng)綁定MVVM項(xiàng)目模板的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04