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);
}
@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表示不縮放
// 重新讀入圖片,注意此時已經把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表示不壓縮,把壓縮后的數據存放到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;
}
如有疑問請留言,或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。