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保存图片uriintent.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即清空baosif (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;}
(完)