您的位置:首页 > 移动开发 > Android开发

Android 拍照并对照片进行裁剪和压缩实例详解

2017-07-30 14:29 781 查看

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 图片大小(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 拍照
相关文章推荐