相机图片VS 图库图片的一些处理方法
在写小demo 时遇到的方法,也是copy的别人的!在这里贴出来,方便大家参考
public String getRealPathFromURI(Uri uri) {
String[] proj = { MediaStore.Images.Media.DATA };
// android多媒体数据库的封装接口
Cursor cursor = managedQuery(uri, proj, null, null, null);
// 获得用户选择的图片的索引值
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
// 将光标移至开头 ,这个很重要,不小心很容易引起越界
cursor.moveToFirst();
// 最后根据索引值获取图片路径
String path = cursor.getString(column_index);
return path;
}
/**
* 处理图片
* @param bm 所要转换的bitmap
* @param newWidth新的宽
* @param newHeight新的高
* @return 指定宽高的bitmap
*/
public static Bitmap zoomImg(Bitmap bm, int newWidth ,int newHeight){
// 获得图片的宽高
int width = bm.getWidth();
int height = bm.getHeight();
// 计算缩放比例
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
return newbm;
}
/**
* 转化为byte数组 ,压缩
* @param bitmap
* @return byte[]
*/
public byte[] toByteArray(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] byteArray = baos.toByteArray();
return byteArray;
}

浙公网安备 33010602011771号