Android 常用的拍照和裁剪,这里调用系统的相机和裁剪,获取到图片,这里是返回获取到资源的代码,在onActivityResult里,
if (requestCode == CHOOSE_COVER && resultCode == RESULT_OK) {
Uri data1 = data.getData(); //裁剪
ImageUtils.getInstance(this).cropImageUri(this, data1, 360, 202, CROP);
} else if (requestCode == CROP && resultCode == RESULT_OK) { //显示
Bitmap bitmap = null;
if(data.getData()!=null){
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStrea(data.getData()));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}else if(data.getExtras()!=null){
Bundle extras = data.getExtras();
bitmap = (Bitmap) extras.get("data");
}
/** * 图片裁剪
* @param context
* @param uri
* @param outputX
* @param outputY
* @param requestCode
*/
public void cropImageUri(Activity context, Uri uri, int outputX, int outputY, int requestCode{
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例//
intent.putExtra("aspectX", 16);//
intent.putExtra("aspectY", 9);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", outputX);
intent.putExtra("outputY", outputY);
intent.putExtra("return-data", true);//默认不输出
intent.putExtra("noFaceDetection", true);
context.startActivityForResult(intent, requestCode);
}
图片裁剪,直接调用系统的裁剪方法如下:
Intent intent1 = new Intent("com.android.camera.action.CROP");
intent1.setDataAndType(Uri.fromFile(new File(path)), "image/*");
intent1.putExtra("crop", "true");
newName = System.currentTimeMillis();
destDirs = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+ AppConstants.PATH+"/"+newName);
intent1.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destDirs));//
intent1.putExtra("outputFormat", Bitmap.CompressFormat.JPEG);
intent1.putExtra("scale", true);
intent1.putExtra("scaleUpIfNeeded", true);
intent1.putExtra("return-data", false);
startActivityForResult(intent1, 1);
获取到裁剪之后的图片资源
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1&&data!=null){
Bundle extras = data.getExtras();
Bitmap photo=null;
if(extras!=null){
photo = (Bitmap) extras.get("data");
}
if (photo!=null&&!photo.equals("null")) {
getCrop(photo); //获取到bitmap
}else if(data.getData()!=null){
Uri uri = data.getData();
if((data.getData()+"").startsWith("file")){
Bitmap bitmapCopy=BitmapFactory.decodeFile((data.getData()+"").substring(8,(data.getData()+"").length()),options).copy(Bitmap.Config.ARGB_4444,true);
getCrop(bitmapCopy); //获取到bitmap
FileUtil.deleteFile((data.getData()+"").substring(8,(data.getData()+"").length())); //删除文件
return;
}else {
if(data.getData()==null||getContentResolver().query(uri, null, null, null,null)==null){
imageViewCamera.setImageResource(R.mipmap.beijing_1);
Toast.makeText(MainActivity.this, "获取图片失败,请从相册选择!", Toast.LENGTH_SHORT).show();
return;
}
String imagePath = null;
Cursor cursor = getContentResolver().query(uri, null, null, null,null);
if (cursor != null && cursor.moveToFirst()) {
imagePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
cursor.close();
}
Bitmap bitmapCopy=BitmapFactory.decodeFile(imagePath,options).copy(Bitmap.Config.ARGB_4444,true);
getCrop(bitmapCopy); //获取到bitmap
FileUtil.deleteFile(imagePath); //删除文件
}
}
}
}
图片压缩大小,可以选择失真压缩,改变分辨率,也可以在不改变分辨率的情况下压缩,下面是在不改变图片质量的情况下压缩图片,在这里记录一下
public static Bitmap getSmallBitmap(String path){
//new 出来一个bitmap的参数
BitmapFactory.Options options=new BitmapFactory.Options();
//设置为true,不会生成bitmao对象,只是读取尺寸和类型信息
options.inJustDecodeBounds=true;
BitmapFactory.decodeFile(path, options);
//得到这个比例 并赋予option里面的inSampleSize
options.inSampleSize = calculateInSampleSizes(options, 320, 480);
//设置为false,即将要生成bitmap对象啦
options.inJustDecodeBounds = false;
//有了这个option,我们可以生成bitmap对象了
Bitmap bitmap=BitmapFactory.decodeFile(path, options);
return bitmap;
}
public static int calculateInSampleSizes(BitmapFactory.Options options,int reqHeight,int reqWidth){
//得到原始图片宽高
int height=options.outHeight;
int width=options.outWidth;
//默认设置为1,即不缩放
int inSampleSize=1;
//如果图片原始的高大于我们期望的高,或者图片的原始宽大于我们期望的宽,换句话意思就是,我们想让它变小一点
if (height > reqHeight || width > reqWidth) {
//原始的高除以期望的高,得到一个比例
final int heightRatio = Math.round((float) height/ (float) reqHeight);
//原始的宽除以期望的宽,得到一个比例
final int widthRatio = Math.round((float) width / (float) reqWidth);
//取上面两个比例中小的一个,返回这个比例
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
调用方法
在这里就可以获取到压缩之后的图片
Bitmap bitmap=ImageUtils.getSmallBitmap(path);//图片处理,压缩大小
网友评论