Android 拍照并对照片进行裁剪和压缩实例详解
Android拍照并对照片进行裁剪和压缩实例详解
本文主要介绍Android调用摄像头拍照并对照片进行裁剪和压缩,文中给出了主要步骤和关键代码。
调用摄像头拍照,对拍摄照片进行裁剪,代码如下。
/**
*调用摄像头拍照,对拍摄照片进行裁剪
*/
privatevoidshowCameraAction(){
//跳转到系统照相机
IntentcameraIntent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
if(cameraIntent.resolveActivity(this.getPackageManager())!=null){
//设置系统相机拍照后的输出路径
//创建临时文件
tempFile=newFile(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();
}
}
对拍摄照片进行裁剪,代码如下。
/**
*对拍摄照片进行裁剪
*/
privatevoidcrop(){
Intentintent=newIntent("com.android.camera.action.CROP");
intent.setDataAndType(Uri.fromFile(tempFile),"image/*");
intent.putExtra("crop","true");//这里必须设置为true拍照之后才会进行裁剪操作
//1.宽高和比例都不设置时,裁剪框可以自行调整(比例和大小都可以随意调整)
//2.只设置裁剪框宽高比(aspect)后,裁剪框比例固定不可调整,只能调整大小
//3.裁剪后生成图片宽高(output)的设置和裁剪框无关,只决定最终生成图片大小
//4.裁剪框宽高比例(aspect)可以和裁剪后生成图片比例(output)不同,此时,会以裁剪框的宽为准,
//按照裁剪宽高比例生成一个图片,该图和框选部分可能不同,不同的情况可能是截取框选的一部分,
//也可能超出框选部分,向下延伸补足
//aspectXaspectY是裁剪框宽高的比例
intent.putExtra("aspectX",358);
intent.putExtra("aspectY",441);
//outputXoutputY是裁剪后生成图片的宽高
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
protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){
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);
}
}
}
得到本地图片旋转压缩,图片质量压缩,代码如下。
/**
*得到本地图片旋转压缩
*@parampath
*@paramsize
*@return
*/
publicstaticBitmapgetLocalThumbImg(Stringpath,intsize){
BitmapFactory.OptionsnewOpts=newBitmapFactory.Options();
//开始读入图片,此时把options.inJustDecodeBounds设回true了
newOpts.inJustDecodeBounds=true;
Bitmapbitmap=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");//压缩好比例大小后再进行质量压缩
intdegree=readPictureDegree(path);
bitmap=rotaingImageView(degree,bitmap);
returnbitmap;
}
/**
*图片质量压缩
*
*@paramimage
*@return
*@size图片大小(kb)
*/
publicstaticBitmapcompressImage(Bitmapimage,intsize,StringimageType){
try{
ByteArrayOutputStreambaos=newByteArrayOutputStream();
if(imageType.equalsIgnoreCase("png")){
image.compress(Bitmap.CompressFormat.PNG,100,baos);
}else{
//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
image.compress(Bitmap.CompressFormat.JPEG,100,baos);
}
intoptions=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
}
FileOutputStreamout=newFileOutputStream(newFile(Constants.FILE_NAME));
image.compress(Bitmap.CompressFormat.JPEG,options,out);
//把压缩后的数据baos存放到ByteArrayInputStream中
ByteArrayInputStreamisBm=newByteArrayInputStream(baos.toByteArray());
//把ByteArrayInputStream数据生成图片
Bitmapbitmap=BitmapFactory.decodeStream(isBm,null,null);
returnbitmap;
}catch(Exceptione){
returnnull;
}
}
/**
*读取图片属性:旋转的角度
*
*@parampath图片绝对路径
*@returndegree旋转的角度
*/
publicstaticintreadPictureDegree(Stringpath){
intdegree=0;
try{
ExifInterfaceexifInterface=newExifInterface(path);
intorientation=exifInterface.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch(orientation){
caseExifInterface.ORIENTATION_ROTATE_90:
degree=90;
break;
caseExifInterface.ORIENTATION_ROTATE_180:
degree=180;
break;
caseExifInterface.ORIENTATION_ROTATE_270:
degree=270;
break;
}
}catch(IOExceptione){
e.printStackTrace();
}
returndegree;
}
/**
*旋转图片
*
*@paramangle
*@parambitmap
*@returnBitmap
*/
publicstaticBitmaprotaingImageView(intangle,Bitmapbitmap){
if(bitmap==null)
returnnull;
//旋转图片动作
Matrixmatrix=newMatrix();
matrix.postRotate(angle);
//创建新的图片
BitmapresizedBitmap=Bitmap.createBitmap(bitmap,0,0,
bitmap.getWidth(),bitmap.getHeight(),matrix,true);
returnresizedBitmap;
}
如有疑问请留言,或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
