Android 拍照并對(duì)照片進(jìn)行裁剪和壓縮實(shí)例詳解
Android 拍照并對(duì)照片進(jìn)行裁剪和壓縮實(shí)例詳解
本文主要介紹 Android 調(diào)用攝像頭拍照并對(duì)照片進(jìn)行裁剪和壓縮,文中給出了主要步驟和關(guān)鍵代碼。
調(diào)用攝像頭拍照,對(duì)拍攝照片進(jìn)行裁剪,代碼如下。
/**
* 調(diào)用攝像頭拍照,對(duì)拍攝照片進(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)建臨時(shí)文件
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();
}
}
對(duì)拍攝照片進(jìn)行裁剪,代碼如下。
/**
* 對(duì)拍攝照片進(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拍照之后才會(huì)進(jìn)行裁剪操作
// 1.寬高和比例都不設(shè)置時(shí),裁剪框可以自行調(diào)整(比例和大小都可以隨意調(diào)整)
// 2.只設(shè)置裁剪框?qū)捀弑?aspect)后,裁剪框比例固定不可調(diào)整,只能調(diào)整大小
// 3.裁剪后生成圖片寬高(output)的設(shè)置和裁剪框無(wú)關(guān),只決定最終生成圖片大小
// 4.裁剪框?qū)捀弑壤?aspect)可以和裁剪后生成圖片比例(output)不同,此時(shí), 會(huì)以裁剪框的寬為準(zhǔn),
// 按照裁剪寬高比例生成一個(gè)圖片,該圖和框選部分可能不同,不同的情況可能是截取框選的一部分,
// 也可能超出框選部分, 向下延伸補(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時(shí),會(huì)直接返回bitmap數(shù)據(jù),但是大圖裁剪時(shí)會(huì)出現(xiàn)問(wèn)題,推薦下面為false時(shí)的方式
// return-data為false時(shí),不會(huì)返回bitmap,但需要指定一個(gè)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();
// 開始讀入圖片,此時(shí)把options.inJustDecodeBounds 設(shè)回true了
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(path, newOpts); // 此時(shí)返回bm為空
newOpts.inJustDecodeBounds = false;
newOpts.inSampleSize = 1; // 設(shè)置縮放比例1表示不縮放
// 重新讀入圖片,注意此時(shí)已經(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 圖片大?。╧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 {
// 質(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 圖片絕對(duì)路徑
* @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)圖片 動(dòng)作
Matrix matrix = new Matrix();
matrix.postRotate(angle);
// 創(chuàng)建新的圖片
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return resizedBitmap;
}
如有疑問(wèn)請(qǐng)留言,或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Android :okhttp+Springmvc文件解析器實(shí)現(xiàn)android向服務(wù)器上傳照片
- Android中實(shí)現(xiàn)長(zhǎng)按照片彈出右鍵菜單功能的實(shí)例代碼
- Android WebView支持input file啟用相機(jī)/選取照片功能
- Android開發(fā)實(shí)現(xiàn)從相冊(cè)中選擇照片功能詳解
- android 實(shí)現(xiàn)在照片上繪制涂鴉的方法
- Android 選擇相冊(cè)照片并返回功能的實(shí)現(xiàn)代碼
- Android打開圖庫(kù)選擇照片功能代碼
- Android開發(fā)從相冊(cè)中選取照片的示例代碼
- 詳解Android WebView的input上傳照片的兼容問(wèn)題
- Android 調(diào)用系統(tǒng)相冊(cè)選擇照片
相關(guān)文章
Listview加載的性能優(yōu)化是如何實(shí)現(xiàn)的
在android開發(fā)中Listview是一個(gè)很重要的組件,它以列表的形式根據(jù)數(shù)據(jù)的長(zhǎng)自適應(yīng)展示具體內(nèi)容,用戶可以自由的定義listview每一列的布局,接下來(lái)通過(guò)本文給大家介紹Listview加載的性能優(yōu)化是如何實(shí)現(xiàn)的,對(duì)listview性能優(yōu)化相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-01-01
android開發(fā)教程之framework增加字符串資源和圖片等resource資源
這篇文章主要介紹了android開發(fā)中framework增加字符串資源和圖片等resource資源方法,需要的朋友可以參考下2014-02-02
Android實(shí)現(xiàn)二維碼掃描和生成的簡(jiǎn)單方法
這篇文章主要介紹了Android實(shí)現(xiàn)二維碼掃描和生成的簡(jiǎn)單方法的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
Android Touch事件分發(fā)過(guò)程詳解
這篇文章主要介紹了Android Touch事件分發(fā)過(guò)程,詳細(xì)描述了Android Touch事件的主要處理流程,有助于深入理解Android程序設(shè)計(jì),需要的朋友可以參考下2014-09-09
Android如何在root設(shè)備上開啟ViewServer詳解
這篇文章主要給大家介紹了關(guān)于Android中如何在root設(shè)備上開啟ViewServer的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-12-12
Android中AnimationDrawable使用的簡(jiǎn)單實(shí)例
這篇文章介紹了Android中AnimationDrawable使用的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-10-10

